perf(label_table): add TryGetIdByLabel to avoid exception overhead#1740
perf(label_table): add TryGetIdByLabel to avoid exception overhead#1740
Conversation
Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a TryGetIdByLabel method to avoid exception overhead, which is a good performance optimization. The implementation is clean and GetIdByLabel is refactored to use the new method. I have a couple of suggestions: one to further improve performance by avoiding a potential double map lookup, and another for a minor grammar fix in an error message.
| if (this->label_remap_.count(label) == 0) { | ||
| throw std::runtime_error(fmt::format("label {} is not exists", label)); | ||
| return {false, 0}; | ||
| } | ||
| return this->label_remap_.at(label); | ||
| return {true, this->label_remap_.at(label)}; |
There was a problem hiding this comment.
To improve performance, you can use find to avoid a potential double lookup in the map. Using count and then at might perform two lookups. The if with initializer statement (C++17) can make the code more concise here.
| if (this->label_remap_.count(label) == 0) { | |
| throw std::runtime_error(fmt::format("label {} is not exists", label)); | |
| return {false, 0}; | |
| } | |
| return this->label_remap_.at(label); | |
| return {true, this->label_remap_.at(label)}; | |
| if (auto it = this->label_remap_.find(label); it != this->label_remap_.end()) { | |
| return {true, it->second}; | |
| } | |
| return {false, 0}; |
| GetIdByLabel(LabelType label) const { | ||
| auto [success, inner_id] = TryGetIdByLabel(label); | ||
| if (not success) { | ||
| throw std::runtime_error(fmt::format("label {} is not exists", label)); |
c015e2e to
758e4f5
Compare
Codecov Report❌ Patch coverage is @@ Coverage Diff @@
## 0.16 #1740 +/- ##
==========================================
- Coverage 92.22% 92.12% -0.10%
==========================================
Files 295 295
Lines 15708 15714 +6
==========================================
- Hits 14486 14477 -9
- Misses 1222 1237 +15
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Cherry-pick of PR #1701 to 0.16 branch with adaptations for the different LabelTable structure in this branch. Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
Summary
TryGetIdByLabelmethod to avoid exception overhead when checking label existenceGetIdByLabelto useTryGetIdByLabelinternallyChanges
The 0.16 branch has a different
LabelTablestructure compared to main, so the changes were adapted accordingly:TryGetIdByLabelmethod that returnsstd::pair<bool, InnerIdType>instead of throwing exceptionsnoexceptto avoid exception handling overheadGetIdByLabelnow delegates toTryGetIdByLabelfor code reuseOriginal PR: #1701