Skip to content

Commit

Permalink
[#204] Size is now computed on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
VitoCastellana committed Mar 31, 2023
1 parent 3111dba commit 686ced5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
17 changes: 8 additions & 9 deletions include/shad/data_structures/local_multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class LocalMultimap {
buckets_array_(numInitBuckets),
deleter_array_(numInitBuckets),
inserter_array_(numInitBuckets),
size_(0),
numberKeys_(0) {
for (uint64_t i = 0; i < numInitBuckets; i++) {
deleter_array_[i] = 0;
Expand All @@ -103,7 +102,14 @@ class LocalMultimap {

/// @brief Size of the multimap (number of entries).
/// @return the size of the multimap.
size_t Size() const { return size_.load(); }
size_t Size() {
size_t size = 0;

for (auto itr = key_begin(); itr != key_end(); ++ itr)
size += (* itr).second.size();

return size;
};

/// @brief Number of keys in the multimap.
/// @return the number of keys in the multimap.
Expand Down Expand Up @@ -145,7 +151,6 @@ class LocalMultimap {

/// @brief Clear the content of the multimap.
void Clear() {
size_ = 0;
buckets_array_.clear();
numberKeys_ = 0;
buckets_array_ = std::vector<Bucket>(numBuckets_);
Expand Down Expand Up @@ -486,7 +491,6 @@ class LocalMultimap {

KeyCompare KeyComp_;
size_t numBuckets_;
std::atomic<size_t> size_;
std::atomic<size_t> numberKeys_;
std::vector<Bucket> buckets_array_;
std::vector<std::atomic<uint32_t>> deleter_array_;
Expand Down Expand Up @@ -922,7 +926,6 @@ void LocalMultimap<KTYPE, VTYPE, KEY_COMPARE>::Erase(const KTYPE &key) {
if (KeyComp_(&entry->key, &key) != 0) continue;

// Key found
size_ -= entry->value.size();
numberKeys_ -= 1;

// find last USED entry in this bucket and swap with this entry
Expand Down Expand Up @@ -1018,7 +1021,6 @@ LocalMultimap<KTYPE, VTYPE, KEY_COMPARE>::Insert(const KTYPE &key,
entry->key = std::move(key);
entry->value.push_back(value);

size_ += 1;
numberKeys_ += 1;
entry->state = USED;
release_inserter(bucketIdx);
Expand All @@ -1039,7 +1041,6 @@ LocalMultimap<KTYPE, VTYPE, KEY_COMPARE>::Insert(const KTYPE &key,
}
entry->value.push_back(value);

size_ += 1;
entry->state = USED;
release_inserter(bucketIdx);
return std::make_pair(
Expand Down Expand Up @@ -1199,7 +1200,6 @@ LocalMultimap<KTYPE, VTYPE, KEY_COMPARE>::Insert(const KTYPE &key,
entry->key = std::move(key);
entry->value.push_back(value);

size_ += 1;
numberKeys_ += 1;
entry->state = USED;
release_inserter(bucketIdx);
Expand All @@ -1219,7 +1219,6 @@ LocalMultimap<KTYPE, VTYPE, KEY_COMPARE>::Insert(const KTYPE &key,
}
entry->value.push_back(value);

size_ += 1;
entry->state = USED;
release_inserter(bucketIdx);
iterator itr(this, bucketIdx, i, bucket, entry,
Expand Down
18 changes: 4 additions & 14 deletions include/shad/data_structures/multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,8 @@ class Multimap : public AbstractDataStructure< Multimap<KTYPE, VTYPE, KEY_COMPAR
auto uniqueLambda = [](const KTYPE & key, std::vector<VTYPE> & value,
ObjectID & oid) {
auto mapPtr = HmapT::GetPtr(oid);
uint64_t orig_size = value.size();
std::sort(value.begin(), value.end());
value.erase(std::unique(value.begin(), value.end()), value.end());
uint64_t new_size = value.size();
mapPtr->localMultimap_.size_ -= (orig_size - new_size);
};
HmapT::GetPtr(oid_)->ForEachEntry(uniqueLambda, oid_);
}
Expand All @@ -394,11 +391,8 @@ class Multimap : public AbstractDataStructure< Multimap<KTYPE, VTYPE, KEY_COMPAR
auto uniqueLambda = [](rt::Handle &, const KTYPE & key,
std::vector<VTYPE> & value, ObjectID & oid) {
auto mapPtr = HmapT::GetPtr(oid);
uint64_t orig_size = value.size();
std::sort(value.begin(), value.end());
value.erase(std::unique(value.begin(), value.end()), value.end());
uint64_t new_size = value.size();
mapPtr->localMultimap_.size_ -= (orig_size - new_size);
};
HmapT::GetPtr(oid_)->AsyncForEachEntry(handle, uniqueLambda, oid_);
}
Expand Down Expand Up @@ -472,19 +466,15 @@ class Multimap : public AbstractDataStructure< Multimap<KTYPE, VTYPE, KEY_COMPAR

template <typename KTYPE, typename VTYPE, typename KEY_COMPARE>
inline size_t Multimap<KTYPE, VTYPE, KEY_COMPARE>::Size() const {
size_t size = localMultimap_.size_.load();
size_t remoteSize;
size_t remoteSize, size = 0;

auto sizeLambda = [](const ObjectID &oid, size_t *res) {
auto mapPtr = HmapT::GetPtr(oid);
*res = mapPtr->localMultimap_.size_.load();
auto sizeLambda = [](const ObjectID & oid, size_t * res) {
* res = HmapT::GetPtr(oid)->GetLocalMultimap()->Size();
};

for (auto tgtLoc : rt::allLocalities()) {
if (tgtLoc != rt::thisLocality()) {
rt::executeAtWithRet(tgtLoc, sizeLambda, oid_, &remoteSize);
rt::executeAtWithRet(tgtLoc, sizeLambda, oid_, & remoteSize);
size += remoteSize;
}
}

return size;
Expand Down

0 comments on commit 686ced5

Please sign in to comment.