Skip to content

Commit

Permalink
fix: remove DenseSet::IteratorBase::TraverseApply (#4170)
Browse files Browse the repository at this point in the history
Signed-off-by: kostas <[email protected]>
  • Loading branch information
kostasrim authored Nov 23, 2024
1 parent b8c2dd8 commit a012539
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 47 deletions.
18 changes: 0 additions & 18 deletions src/core/dense_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -840,22 +840,4 @@ size_t DenseSet::SizeSlow() {
return size_;
}

size_t DenseSet::IteratorBase::TraverseApply(DensePtr* ptr, std::function<void(DensePtr*)> fun) {
size_t links_traversed = 0;
while (ptr->IsLink()) {
DenseLinkKey* link = ptr->AsLink();
fun(link);
ptr = &link->next;
++links_traversed;
}

// The last ptr in the link always returns ptr->IsLink() = false
DCHECK(!ptr->IsEmpty());
DCHECK(ptr->IsObject());
fun(ptr);
++links_traversed;

return links_traversed;
}

} // namespace dfly
5 changes: 0 additions & 5 deletions src/core/dense_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ class DenseSet {

void Advance();

// If ptr is a link, it calls fun on all links in the chain.
// Otherwise it calls it only once on the object.
// ptr must be non empty.
size_t TraverseApply(DensePtr* ptr, std::function<void(DensePtr*)> fun);

DenseSet* owner_;
ChainVectorIterator curr_list_;
DensePtr* curr_entry_;
Expand Down
32 changes: 17 additions & 15 deletions src/core/score_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,25 @@ pair<sds, bool> DuplicateEntryIfFragmented(void* obj, float ratio) {
} // namespace

bool ScoreMap::iterator::ReallocIfNeeded(float ratio, std::function<void(sds, sds)> cb) {
bool reallocated = false;
auto body = [ratio, &cb, &reallocated](auto* ptr) {
auto* obj = ptr->GetObject();
auto [new_obj, duplicate] = DuplicateEntryIfFragmented(obj, ratio);
if (duplicate) {
if (cb) {
cb((sds)obj, (sds)new_obj);
}
sdsfree((sds)obj);
ptr->SetObject(new_obj);
}
reallocated |= duplicate;
};
auto* ptr = curr_entry_;

if (ptr->IsLink()) {
ptr = ptr->AsLink();
}

TraverseApply(curr_entry_, body);
DCHECK(!ptr->IsEmpty());
DCHECK(ptr->IsObject());

return reallocated;
auto* obj = ptr->GetObject();
auto [new_obj, realloced] = DuplicateEntryIfFragmented(obj, ratio);
if (realloced) {
if (cb) {
cb((sds)obj, (sds)new_obj);
}
sdsfree((sds)obj);
ptr->SetObject(new_obj);
}
return realloced;
}

} // namespace dfly
20 changes: 11 additions & 9 deletions src/core/string_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,19 @@ detail::SdsPair StringMap::iterator::BreakToPair(void* obj) {
}

bool StringMap::iterator::ReallocIfNeeded(float ratio) {
bool reallocated = false;
auto body = [this, ratio, &reallocated](auto* ptr) {
auto* obj = ptr->GetObject();
auto [new_obj, realloc] = static_cast<StringMap*>(owner_)->ReallocIfNeeded(obj, ratio);
ptr->SetObject(new_obj);
reallocated |= realloc;
};
auto* ptr = curr_entry_;
if (ptr->IsLink()) {
ptr = ptr->AsLink();
}

DCHECK(!ptr->IsEmpty());
DCHECK(ptr->IsObject());

TraverseApply(curr_entry_, body);
auto* obj = ptr->GetObject();
auto [new_obj, realloced] = static_cast<StringMap*>(owner_)->ReallocIfNeeded(obj, ratio);
ptr->SetObject(new_obj);

return reallocated;
return realloced;
}

} // namespace dfly

0 comments on commit a012539

Please sign in to comment.