refactor(smart_pointer): replace shared_ptr with unique_ptr for better performance#1570
refactor(smart_pointer): replace shared_ptr with unique_ptr for better performance#1570LHT129 merged 1 commit intoantgroup:mainfrom
Conversation
…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>
Summary of ChangesHello @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 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. Changelog
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. 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
|
There was a problem hiding this comment.
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.
| 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]); |
There was a problem hiding this comment.
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 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
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:
|
This refactoring eliminates unnecessary reference counting overhead and improves memory efficiency. The ownership semantics remain clear with unique_ptr providing exclusive ownership.