forked from antgroup/vsag
-
Notifications
You must be signed in to change notification settings - Fork 0
reduce attr bitset memory #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LHT129
wants to merge
1
commit into
main
Choose a base branch
from
memory_reduce
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,11 @@ namespace vsag { | |
| MultiBitsetManager::MultiBitsetManager(Allocator* allocator, | ||
| uint64_t count, | ||
| ComputableBitsetType bitset_type) | ||
| : allocator_(allocator), count_(count), bitsets_(allocator), bitset_type_(bitset_type) { | ||
| bitsets_.resize(count, nullptr); | ||
| : allocator_(allocator), | ||
| count_(count), | ||
| bitsets_(allocator), | ||
| bitset_map_(count, -1, allocator), | ||
| bitset_type_(bitset_type) { | ||
| } | ||
|
|
||
| MultiBitsetManager::MultiBitsetManager(Allocator* allocator, uint64_t count) | ||
|
|
@@ -43,53 +46,62 @@ MultiBitsetManager::SetNewCount(uint64_t new_count) { | |
| return; | ||
| } | ||
| this->count_ = new_count; | ||
| this->bitsets_.resize(new_count, nullptr); | ||
| this->bitset_map_.resize(new_count, -1); | ||
| } | ||
|
|
||
| ComputableBitset* | ||
| MultiBitsetManager::GetOneBitset(uint64_t id) const { | ||
| if (id >= count_) { | ||
| return nullptr; | ||
| } | ||
| return this->bitsets_[id]; | ||
| auto inner_id = bitset_map_[id]; | ||
| if (inner_id == -1) { | ||
| return nullptr; | ||
| } | ||
| return this->bitsets_[inner_id]; | ||
| } | ||
|
|
||
| void | ||
| MultiBitsetManager::InsertValue(uint64_t id, uint64_t offset, bool value) { | ||
| if (id >= count_) { | ||
| this->SetNewCount(id + 1); | ||
| } | ||
| if (this->bitsets_[id] == nullptr) { | ||
| this->bitsets_[id] = | ||
| ComputableBitset::MakeRawInstance(this->bitset_type_, this->allocator_); | ||
| auto inner_id = bitset_map_[id]; | ||
| if (inner_id == -1) { | ||
| inner_id = static_cast<int16_t>(bitsets_.size()); | ||
| bitset_map_[id] = inner_id; | ||
| bitsets_.emplace_back( | ||
|
Comment on lines
+71
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Potential for int16_t overflow if bitsets_ grows large. If creating more than 32,767 bitsets is possible, switch to a larger integer type for bitset_map_ to prevent overflow issues. |
||
| ComputableBitset::MakeRawInstance(this->bitset_type_, this->allocator_)); | ||
| } | ||
| this->bitsets_[id]->Set(static_cast<int64_t>(offset), value); | ||
| this->bitsets_[inner_id]->Set(static_cast<int64_t>(offset), value); | ||
| } | ||
|
|
||
| void | ||
| MultiBitsetManager::Serialize(StreamWriter& writer) { | ||
| StreamWriter::WriteObj(writer, count_); | ||
| for (auto id : bitset_map_) { | ||
| StreamWriter::WriteObj(writer, id); | ||
| } | ||
| uint64_t size = bitsets_.size(); | ||
| StreamWriter::WriteObj(writer, size); | ||
| for (auto* bitset : bitsets_) { | ||
| if (bitset == nullptr) { | ||
| StreamWriter::WriteObj(writer, false); | ||
| } else { | ||
| StreamWriter::WriteObj(writer, true); | ||
| bitset->Serialize(writer); | ||
| } | ||
| bitset->Serialize(writer); | ||
| } | ||
| } | ||
|
|
||
| void | ||
| MultiBitsetManager::Deserialize(lvalue_or_rvalue<StreamReader> reader) { | ||
| StreamReader::ReadObj(reader, count_); | ||
| this->bitsets_.resize(count_, nullptr); | ||
| this->bitset_map_.resize(count_, -1); | ||
| for (uint64_t i = 0; i < count_; i++) { | ||
| bool has_bitset; | ||
| StreamReader::ReadObj(reader, has_bitset); | ||
| if (has_bitset) { | ||
| bitsets_[i] = ComputableBitset::MakeRawInstance(bitset_type_, allocator_); | ||
| bitsets_[i]->Deserialize(reader); | ||
| } | ||
| StreamReader::ReadObj(reader, this->bitset_map_[i]); | ||
| } | ||
| uint64_t size; | ||
| StreamReader::ReadObj(reader, size); | ||
| bitsets_.resize(size, nullptr); | ||
| for (uint64_t i = 0; i < size; i++) { | ||
| bitsets_[i] = ComputableBitset::MakeRawInstance(bitset_type_, allocator_); | ||
| bitsets_[i]->Deserialize(reader); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Potential for int16_t overflow in inner_id assignment.
Casting bitsets_.size() to int16_t may overflow if the number of bitsets exceeds int16_t's range. Use a larger integer type or add an overflow check.