Skip to content
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: ClearInternal now can clear partially #3867

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/core/dense_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,17 @@ void* DenseSet::PopDataFront(DenseSet::ChainVectorIterator it) {
return ret;
}

void DenseSet::ClearInternal() {
uint32_t DenseSet::ClearInternal(uint32_t start, uint32_t count) {
constexpr unsigned kArrLen = 32;
ClearItem arr[kArrLen];
unsigned len = 0;

for (auto it = entries_.begin(); it != entries_.end(); ++it) {
if (it->IsEmpty())
size_t end = min<size_t>(entries_.size(), start + count);
for (size_t i = start; i < end; ++i) {
DensePtr ptr = entries_[i];
if (ptr.IsEmpty())
continue;

DensePtr ptr = *it;
auto& dest = arr[len++];
dest.has_ttl = ptr.HasTtl();

Expand All @@ -208,11 +209,13 @@ void DenseSet::ClearInternal() {

ClearBatch(len, arr);

entries_.clear();
num_used_buckets_ = 0;
num_links_ = 0;
size_ = 0;
expiration_used_ = false;
if (size_ == 0) {
entries_.clear();
num_used_buckets_ = 0;
num_links_ = 0;
expiration_used_ = false;
}
return end;
}

bool DenseSet::Equal(DensePtr dptr, const void* ptr, uint32_t cookie) const {
Expand Down Expand Up @@ -277,6 +280,7 @@ void DenseSet::ClearBatch(unsigned len, ClearItem* items) {
auto& src = items[i];
if (src.obj) {
ObjDelete(src.obj, src.has_ttl);
--size_;
src.obj = nullptr;
}

Expand All @@ -285,6 +289,7 @@ void DenseSet::ClearBatch(unsigned len, ClearItem* items) {

if (src.ptr.IsObject()) {
ObjDelete(src.ptr.Raw(), src.has_ttl);
--size_;
} else {
auto& dest = items[dest_id++];
DenseLinkKey* link = src.ptr.AsLink();
Expand Down
6 changes: 5 additions & 1 deletion src/core/dense_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ class DenseSet {
explicit DenseSet(MemoryResource* mr = PMR_NS::get_default_resource());
virtual ~DenseSet();

void Clear() {
ClearInternal(0, entries_.size());
}

// Returns the number of elements in the map. Note that it might be that some of these elements
// have expired and can't be accessed.
size_t UpperBoundSize() const {
Expand Down Expand Up @@ -300,7 +304,7 @@ class DenseSet {
// Note this does not free any dynamic allocations done by derived classes, that a DensePtr
// in the set may point to. This function only frees the allocated DenseLinkKeys created by
// DenseSet. All data allocated by a derived class should be freed before calling this
void ClearInternal();
uint32_t ClearInternal(uint32_t start, uint32_t count);

void IncreaseMallocUsed(size_t delta) {
obj_malloc_used_ += delta;
Expand Down
4 changes: 0 additions & 4 deletions src/core/score_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ class ScoreMap : public DenseSet {
return FindInternal(ele, Hash(ele, 0), 0);
}

void Clear() {
ClearInternal();
}

iterator begin() {
return iterator{this, false};
}
Expand Down
6 changes: 1 addition & 5 deletions src/core/string_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pair<sds, uint64_t> CreateEntry(string_view field, string_view value, uint32_t t
} // namespace

StringMap::~StringMap() {
ClearInternal();
Clear();
}

bool StringMap::AddOrUpdate(string_view field, string_view value, uint32_t ttl_sec) {
Expand Down Expand Up @@ -101,10 +101,6 @@ bool StringMap::Contains(string_view field) const {
return FindInternal(&field, hashcode, 1) != nullptr;
}

void StringMap::Clear() {
ClearInternal();
}

optional<pair<sds, sds>> StringMap::RandomPair() {
// Iteration may remove elements, and so we need to loop if we happen to reach the end
while (true) {
Expand Down
2 changes: 0 additions & 2 deletions src/core/string_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ class StringMap : public DenseSet {
return iterator{FindIt(&member, 1)};
}

void Clear();

iterator begin() {
return iterator{this};
}
Expand Down
4 changes: 0 additions & 4 deletions src/core/string_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class StringSet : public DenseSet {
return FindInternal(&s1, Hash(&s1, 1), 1) != nullptr;
}

void Clear() {
ClearInternal();
}

std::optional<std::string> Pop();

class iterator : private IteratorBase {
Expand Down
Loading