-
Notifications
You must be signed in to change notification settings - Fork 919
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
chore: add defrag logic for zsets #3836
base: main
Are you sure you want to change the base?
Changes from all commits
e2f7657
f1406b2
32993e8
3c2804a
3ec329f
265e754
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,4 +146,31 @@ detail::SdsScorePair ScoreMap::iterator::BreakToPair(void* obj) { | |
return detail::SdsScorePair(f, GetValue(f)); | ||
} | ||
|
||
bool ScoreMap::iterator::ReallocIfNeeded(float ratio) { | ||
// Unwrap all links to correctly call SetObject() | ||
auto* ptr = curr_entry_; | ||
while (ptr->IsLink()) | ||
ptr = ptr->AsLink(); | ||
|
||
auto* obj = ptr->GetObject(); | ||
auto [new_obj, realloced] = static_cast<ScoreMap*>(owner_)->ReallocIfNeeded(obj, ratio); | ||
ptr->SetObject(new_obj); | ||
|
||
return realloced; | ||
} | ||
|
||
pair<sds, bool> ScoreMap::ReallocIfNeeded(void* obj, float ratio) { | ||
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. I can extra this to seperate function. It's almost identical as to |
||
sds key = (sds)obj; | ||
size_t key_len = sdslen(key); | ||
|
||
if (!zmalloc_page_is_underutilized(key, ratio)) | ||
return {key, false}; | ||
|
||
sds newkey = AllocSdsWithSpace(key_len, 8); | ||
memcpy(newkey, key, key_len + 8 + 1); | ||
sdsfree(key); | ||
|
||
return {newkey, true}; | ||
} | ||
|
||
} // namespace dfly |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -769,5 +769,16 @@ SortedMap* SortedMap::FromListPack(PMR_NS::memory_resource* res, const uint8_t* | |
return zs; | ||
} | ||
|
||
bool SortedMap::DefragIfNeeded(float ratio) { | ||
bool realloced = false; | ||
|
||
for (auto it = score_map->begin(); it != score_map->end(); ++it) { | ||
realloced |= it.ReallocIfNeeded(ratio); | ||
// Not sure if it's worth going over B+ tree cause lgn to delete + lgn to insert | ||
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. Yeah, I don't think it's really worth going over the B+ tree because we would need to delete + insert. We can add this in the future if we wish to + it's really boilerplate.... |
||
} | ||
|
||
return realloced; | ||
} | ||
|
||
} // namespace detail | ||
} // namespace dfly |
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.
Also //todo for me, mock this and test for sanity