Skip to content

refactor(smart_pointer): replace shared_ptr with unique_ptr for better performance#1570

Merged
LHT129 merged 1 commit intoantgroup:mainfrom
LHT129:unique_ptr
Feb 10, 2026
Merged

refactor(smart_pointer): replace shared_ptr with unique_ptr for better performance#1570
LHT129 merged 1 commit intoantgroup:mainfrom
LHT129:unique_ptr

Conversation

@LHT129
Copy link
Collaborator

@LHT129 LHT129 commented Feb 9, 2026

  • Convert IndexNode children storage from shared_ptr to unique_ptr in pyramid
  • Change root_ from shared_ptr to unique_ptr in Pyramid class
  • Update GetChild and add_one_point methods to use raw pointers
  • Replace shared_ptr<int8_t[]> with unique_ptr<int8_t[]> in hnsw.cpp
  • Replace shared_ptr<float[]> with vector in sparse_vector_datacell_test.cpp

This refactoring eliminates unnecessary reference counting overhead and improves memory efficiency. The ownership semantics remain clear with unique_ptr providing exclusive ownership.

…r performance

- Convert IndexNode children storage from shared_ptr to unique_ptr in pyramid
- Change root_ from shared_ptr to unique_ptr in Pyramid class
- Update GetChild and add_one_point methods to use raw pointers
- Replace shared_ptr<int8_t[]> with unique_ptr<int8_t[]> in hnsw.cpp
- Replace shared_ptr<float[]> with vector<float> in sparse_vector_datacell_test.cpp

This refactoring eliminates unnecessary reference counting overhead and improves memory efficiency.
The ownership semantics remain clear with unique_ptr providing exclusive ownership.

Signed-off-by: LHT129 <tianlan.lht@antgroup.com>
@LHT129 LHT129 self-assigned this Feb 9, 2026
@LHT129 LHT129 added version/1.0 kind/improvement Code improvements (variable/function renaming, refactoring, etc. ) and removed size/M labels Feb 9, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @LHT129, 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 the codebase to optimize memory management and performance by transitioning from std::shared_ptr to std::unique_ptr or std::vector where exclusive ownership or simpler array management is appropriate. The changes primarily affect the Pyramid indexing structure and HNSW-related components, aiming to eliminate unnecessary reference counting overhead and clarify ownership semantics throughout the code.

Highlights

  • Pyramid IndexNode Children and Root Ownership: The children_ map within IndexNode and the root_ member of the Pyramid class have been converted from std::shared_ptr<IndexNode> to std::unique_ptr<IndexNode>, ensuring exclusive ownership and reducing reference counting overhead.
  • Method Signature Updates for Raw Pointers: The IndexNode::GetChild method now returns a raw IndexNode* instead of a std::shared_ptr<IndexNode>, and Pyramid::add_one_point now accepts an IndexNode* parameter. This reflects the change to unique ownership and avoids unnecessary smart pointer wrapping for temporary access.
  • HNSW Dynamic Array Management: In hnsw.cpp, dynamic arrays for base_data, topk_data, and generated_data have been updated from std::shared_ptr<int8_t[]> to std::unique_ptr<int8_t[]>, providing clear ownership and improved performance.
  • Test Code Memory Management Simplification: In sparse_vector_datacell_test.cpp and hnsw_test.cpp, std::shared_ptr<float[]> and std::shared_ptr<int8_t[]> instances used for temporary data storage in tests have been replaced with std::vector<float> and std::vector<int8_t> respectively, simplifying memory management in test contexts.

🧠 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/pyramid.cpp
    • Changed IndexNode::AddChild to use std::make_unique for new children.
    • Updated IndexNode::GetChild return type to IndexNode* and adjusted return values to .get().
    • Modified Pyramid::search_impl, Pyramid::Add, Pyramid::Build to use raw IndexNode* from root_.get().
    • Updated Pyramid::add_one_point function signature to accept IndexNode*.
  • src/algorithm/pyramid.h
    • Added #include <memory>.
    • Changed IndexNode::children_ map value type to std::unique_ptr<IndexNode>.
    • Updated IndexNode::GetChild declaration to return IndexNode*.
    • Changed Pyramid::root_ member type to std::unique_ptr<IndexNode> and its initialization in the constructor.
    • Updated Pyramid::add_one_point declaration to accept IndexNode*.
  • src/datacell/sparse_vector_datacell_test.cpp
    • Replaced std::shared_ptr<float[]> with std::vector<float> for dist arrays in test cases.
    • Updated array access from .get() to .data() for std::vector.
  • src/index/hnsw.cpp
    • Replaced std::shared_ptr<int8_t[]> with std::unique_ptr<int8_t[]> for base_data, topk_data, and generated_data.
  • src/index/hnsw_test.cpp
    • Replaced std::shared_ptr<int8_t[]> with std::vector<int8_t> for base_data in test cases.
    • Updated array access from .get() to .data() for std::vector.
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.

@mergify mergify bot added the module/index label Feb 9, 2026
Copy link

@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 effectively refactors the codebase to replace std::shared_ptr with std::unique_ptr where appropriate, which should yield performance benefits by reducing reference counting overhead. The changes in the pyramid implementation correctly establish unique ownership semantics for IndexNodes. The move to std::vector in test files is also a commendable improvement for safety and idiomatic C++.

I have one suggestion to apply the std::vector pattern consistently in hnsw.cpp for managing dynamic arrays, which would further improve code quality. Overall, this is a well-executed and beneficial refactoring.

Comment on lines +1022 to +1025
std::unique_ptr<int8_t[]> base_data(new int8_t[data_size]);
std::unique_ptr<int8_t[]> topk_data(new int8_t[data_size]);

std::shared_ptr<int8_t[]> generated_data(new int8_t[data_size]);
std::unique_ptr<int8_t[]> generated_data(new int8_t[data_size]);

Choose a reason for hiding this comment

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

medium

For consistency with other changes in this PR (e.g., in hnsw_test.cpp and sparse_vector_datacell_test.cpp), consider using std::vector<int8_t> instead of std::unique_ptr<int8_t[]>. std::vector is generally safer and more idiomatic for managing dynamic arrays in C++.

Please note that applying this suggestion will require you to change the subsequent .get() calls on these variables to .data().

    std::vector<int8_t> base_data(data_size);
    std::vector<int8_t> topk_data(data_size);

    std::vector<int8_t> generated_data(data_size);

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@            Coverage Diff             @@
##             main    #1570      +/-   ##
==========================================
- Coverage   91.24%   91.13%   -0.11%     
==========================================
  Files         329      329              
  Lines       19396    19396              
==========================================
- Hits        17697    17676      -21     
- Misses       1699     1720      +21     
Flag Coverage Δ
cpp 91.13% <100.00%> (-0.11%) ⬇️

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

Components Coverage Δ
common 85.81% <ø> (ø)
datacell 92.78% <ø> (-1.00%) ⬇️
index 90.67% <100.00%> (+0.05%) ⬆️
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 3dd2923...f171b54. Read the comment docs.

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

@LHT129 LHT129 marked this pull request as ready for review February 9, 2026 09:55
Copy link
Collaborator

@inabao inabao 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
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

@LHT129 LHT129 merged commit 082f0af into antgroup:main Feb 10, 2026
30 checks passed
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. ) module/index version/1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants