Skip to content

perf(label_table): add TryGetIdByLabel to avoid exception overhead#1701

Merged
inabao merged 1 commit intomainfrom
feature/try-get-id-by-label
Mar 20, 2026
Merged

perf(label_table): add TryGetIdByLabel to avoid exception overhead#1701
inabao merged 1 commit intomainfrom
feature/try-get-id-by-label

Conversation

@inabao
Copy link
Copy Markdown
Collaborator

@inabao inabao commented Mar 16, 2026

close: #1729

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
@inabao inabao self-assigned this Mar 16, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 focuses on a performance enhancement by modifying the label lookup mechanism. It introduces a non-throwing alternative for retrieving inner IDs by label, which is then integrated into core algorithm components. This change is designed to reduce the computational cost associated with exception handling, particularly in scenarios where label lookups might frequently fail or involve removed labels, leading to a more efficient system overall.

Highlights

  • Performance Optimization: Introduced a new method, TryGetIdByLabel, to avoid the overhead of exception handling in performance-critical label lookup operations. This method returns a std::pair<bool, InnerIdType> indicating success or failure, rather than throwing an exception.
  • Refactored GetIdByLabel: The existing GetIdByLabel method was refactored to internally use TryGetIdByLabel and only throw an exception if TryGetIdByLabel indicates a lookup failure, maintaining its original behavior for callers expecting exceptions.
  • Integration into Algorithms: Updated InnerIndexInterface::cal_distance_by_id and SparseIndex::CalDistanceById to utilize the new TryGetIdByLabel method, replacing try-catch blocks with conditional checks for improved performance.

🧠 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.

Changelog
  • src/algorithm/inner_index_interface.cpp
    • Replaced exception-based GetIdByLabel call with TryGetIdByLabel and a conditional check.
    • Updated error handling logic to check the success flag from TryGetIdByLabel.
  • src/algorithm/sparse_index.cpp
    • Replaced exception-based GetIdByLabel call with TryGetIdByLabel and a conditional check.
    • Updated error handling logic to check the success flag from TryGetIdByLabel.
  • src/impl/label_table.cpp
    • Implemented TryGetIdByLabel to return a std::pair<bool, InnerIdType> indicating lookup success and the inner ID.
    • Modified TryGetIdByLabel to handle INVALID_ID and removed labels by returning false instead of throwing exceptions.
    • Refactored GetIdByLabel to call TryGetIdByLabel and throw a VsagException only if the lookup fails.
  • src/impl/label_table.h
    • Added the declaration for the new TryGetIdByLabel method.
  • src/impl/label_table_test.cpp
    • Added a new test section for TryGetIdByLabel covering successful lookups, non-existent labels, and removed labels.
    • Included tests for TryGetIdByLabel with return_even_removed parameter.
    • Added tests for TryGetIdByLabel functionality when the reverse map is not used.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request successfully refactors the GetIdByLabel functionality by introducing a new TryGetIdByLabel method. This change effectively replaces exception-based error handling with a return value for expected "not found" scenarios, which is a good practice for performance optimization in C++ by avoiding the overhead associated with exceptions. The new method is correctly integrated into the existing GetIdByLabel and other call sites, and comprehensive unit tests have been added to cover its behavior, including cases for successful lookups, non-existent labels, and removed labels.

Copy link
Copy Markdown
Collaborator

@LHT129 LHT129 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@inabao inabao merged commit cafb34b into main Mar 20, 2026
47 of 50 checks passed
@inabao inabao deleted the feature/try-get-id-by-label branch March 20, 2026 02:54
@wxyucs
Copy link
Copy Markdown
Collaborator

wxyucs commented Mar 23, 2026

@inabao this pull request cannot cherry-pick the branch 0.16 (CONFLICTS) please create a new pull request to the branch 0.16 .

inabao added a commit that referenced this pull request Mar 23, 2026
Cherry-pick of PR #1701 to 0.16 branch with adaptations for the different
LabelTable structure in this branch.
inabao added a commit that referenced this pull request Mar 23, 2026
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>
inabao added a commit that referenced this pull request Mar 23, 2026
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>
@wxyucs
Copy link
Copy Markdown
Collaborator

wxyucs commented Mar 25, 2026

@inabao this pull request cannot cherry-pick to the branch 0.18 (CONFLICTS), please create a nrew pull request to the branch 0.18 .

inabao added a commit that referenced this pull request Mar 26, 2026
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>
inabao added a commit that referenced this pull request Mar 26, 2026
…1701)

Add TryGetIdByLabel method that returns std::pair<bool, InnerIdType> instead
of throwing exceptions. This improves performance in hot paths by avoiding
the overhead of exception handling.

Changes:
- Add TryGetIdByLabel() method to LabelTable class
- Refactor GetIdByLabel() to use TryGetIdByLabel() internally
- Update cal_distance_by_id() to use TryGetIdByLabel() instead of try-catch
- Update SparseIndex::CalDistanceById() to use TryGetIdByLabel()

Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
inabao added a commit that referenced this pull request Mar 26, 2026
…1701) (#1758)

Add TryGetIdByLabel method that returns std::pair<bool, InnerIdType> instead
of throwing exceptions. This improves performance in hot paths by avoiding
the overhead of exception handling.

Changes:
- Add TryGetIdByLabel() method to LabelTable class
- Refactor GetIdByLabel() to use TryGetIdByLabel() internally
- Update cal_distance_by_id() to use TryGetIdByLabel() instead of try-catch
- Update SparseIndex::CalDistanceById() to use TryGetIdByLabel()

Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
inabao added a commit that referenced this pull request Mar 26, 2026
…1740)

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>

---------

Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
Co-authored-by: Claude <noreply@anthropic.com>
wxyucs pushed a commit that referenced this pull request Mar 30, 2026
…1701)

Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit cafb34b)
wxyucs pushed a commit that referenced this pull request Mar 30, 2026
…1701)

Signed-off-by: inabao <jinjiabao.jjb@antgroup.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit cafb34b)
wxyucs pushed a commit that referenced this pull request Mar 31, 2026
…1701)

Signed-off-by: inabao <jinjiabao.jjb@antgroup.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit cafb34b)
@wxyucs
Copy link
Copy Markdown
Collaborator

wxyucs commented Mar 31, 2026

patch to 0.15 in #1769

wxyucs pushed a commit that referenced this pull request Mar 31, 2026
…1701)

Backport the non-throwing label lookup helper to 0.15 and update the CalDistanceById call paths to avoid exception-driven control flow.
Keep the 0.15 branch shape intact by only applying the compatible caller changes on this branch.

Signed-off-by: inabao <jinjiabao.jjb@antgroup.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit cafb34b)
wxyucs added a commit that referenced this pull request Mar 31, 2026
…1701) (#1769)

Backport the non-throwing label lookup helper to 0.15 and update the CalDistanceById call paths to avoid exception-driven control flow.

(cherry picked from commit cafb34b)

Signed-off-by: inabao <jinjiabao.jjb@antgroup.com>
Co-authored-by: inabao <jinjiabao.jjb@antgroup.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/improvement Code improvements (variable/function renaming, refactoring, etc. ) needs-cherry-pick-release-0.17 size/M version/1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize the performance of InnerIndexInterface::cal_distance_by_id in scenarios where id does not exist

3 participants