Skip to content

refactor(hnsw,io): correct error type for memory allocation (cherry-pick to 0.18)#1748

Merged
inabao merged 1 commit into
0.18from
refactor-memory-error-0.18
Mar 26, 2026
Merged

refactor(hnsw,io): correct error type for memory allocation (cherry-pick to 0.18)#1748
inabao merged 1 commit into
0.18from
refactor-memory-error-0.18

Conversation

@inabao
Copy link
Copy Markdown
Collaborator

@inabao inabao commented Mar 25, 2026

Summary

Cherry-pick PR #1658 to 0.18 branch.

Replace std::runtime_error with VsagException(ErrorType::NO_ENOUGH_MEMORY) for memory allocation failures in HNSW algorithm and MemoryBlockIO.

Changes

  • Modified src/algorithm/hnswlib/hnswalg.cpp:

    • Added #include "vsag_exception.h"
    • Added using declarations for ErrorType and VsagException
    • Replaced 12 std::runtime_error throws with VsagException(ErrorType::NO_ENOUGH_MEMORY)
  • Modified src/io/memory_block_io.cpp:

    • Added #include "vsag_exception.h"
    • Modified check_and_realloc() to throw VsagException when allocation fails

@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 refactors memory allocation error handling across the HNSW algorithm and MemoryBlockIO components. By transitioning from standard runtime errors to a custom VsagException with a specific NO_ENOUGH_MEMORY error type, the change enhances the clarity and specificity of error reporting, facilitating more robust and precise error management within the system.

Highlights

  • Error Handling Standardization: Replaced generic std::runtime_error exceptions with the more specific VsagException(ErrorType::NO_ENOUGH_MEMORY) for memory allocation failures.
  • Affected Components: The error handling update was applied to both the HNSW algorithm implementation and the MemoryBlockIO utility.
  • Cherry-Pick: This change is a cherry-pick from PR refactor(hnsw,io): correct error type for memory allocation #1658 to the 0.18 branch, ensuring consistency across versions.

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

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

This pull request refactors error handling in hnswlib and MemoryBlockIO by replacing std::runtime_error with VsagException for memory allocation failures. It introduces vsag_exception.h and uses VsagException(ErrorType::NO_ENOUGH_MEMORY, ...) for all memory allocation-related errors. A review comment suggests moving the using declarations for ErrorType and VsagException inside the hnswlib namespace for better code clarity and to avoid global namespace pollution.

Comment on lines +26 to 29
using vsag::ErrorType;
using vsag::VsagException;

namespace hnswlib {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To improve code clarity and avoid polluting the global namespace, it's recommended to place using declarations within the namespace where they are utilized. This change moves the using declarations for ErrorType and VsagException inside the hnswlib namespace.

namespace hnswlib {
using vsag::ErrorType;
using vsag::VsagException;

@inabao inabao force-pushed the refactor-memory-error-0.18 branch 2 times, most recently from 6a16a64 to 7832f7b Compare March 25, 2026 07:16
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 25, 2026

Codecov Report

❌ Patch coverage is 19.23077% with 21 lines in your changes missing coverage. Please review.

❌ Your patch status has failed because the patch coverage (19.23%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

@@            Coverage Diff             @@
##             0.18    #1748      +/-   ##
==========================================
- Coverage   91.21%   91.16%   -0.06%     
==========================================
  Files         328      328              
  Lines       19327    19336       +9     
==========================================
- Hits        17630    17627       -3     
- Misses       1697     1709      +12     
Flag Coverage Δ
cpp 91.16% <19.23%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
common 85.81% <ø> (ø)
datacell 93.29% <75.00%> (+0.16%) ⬆️
index 91.05% <9.09%> (+0.01%) ⬆️
simd 100.00% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 06129c5...4091ae3. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Replace std::runtime_error with VsagException(ErrorType::NO_ENOUGH_MEMORY)

for memory allocation failures in HNSW algorithm and MemoryBlockIO.

Cherry-picked from PR #1658 to 0.18 branch.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
@inabao inabao force-pushed the refactor-memory-error-0.18 branch from 7832f7b to 4091ae3 Compare March 26, 2026 03:35
@inabao inabao self-assigned this Mar 26, 2026
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

Copy link
Copy Markdown
Collaborator

@wxyucs wxyucs 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 33e8c02 into 0.18 Mar 26, 2026
31 of 34 checks passed
@inabao inabao deleted the refactor-memory-error-0.18 branch March 26, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants