@@ -46,53 +46,51 @@ void SparseMapIndex::setHits(uint64_t key,
4646}
4747
4848Index::LookupResult SparseMapIndex::lookup (uint64_t key) {
49- LookupResult lr;
5049 auto & map = getMap (key);
5150 auto lock = std::lock_guard{getMutex (key)};
5251
5352 auto it = map.find (subkey (key));
5453 if (it != map.end ()) {
55- lr.found_ = true ;
56- lr.record_ = it->second ;
57- it.value ().totalHits = safeInc (lr.record_ .totalHits );
58- it.value ().currentHits = safeInc (lr.record_ .currentHits );
54+ LookupResult lr{true , it->second };
55+
56+ it.value ().totalHits = safeInc (it.value ().totalHits );
57+ it.value ().currentHits = safeInc (it.value ().currentHits );
58+ return lr;
5959 }
60- return lr ;
60+ return {} ;
6161}
6262
6363Index::LookupResult SparseMapIndex::peek (uint64_t key) const {
64- LookupResult lr;
6564 const auto & map = getMap (key);
6665 auto lock = std::shared_lock{getMutex (key)};
6766
6867 auto it = map.find (subkey (key));
6968 if (it != map.end ()) {
70- lr.found_ = true ;
71- lr.record_ = it->second ;
69+ return LookupResult (true , it->second );
7270 }
73- return lr ;
71+ return {} ;
7472}
7573
7674Index::LookupResult SparseMapIndex::insert (uint64_t key,
7775 uint32_t address,
7876 uint16_t sizeHint) {
79- LookupResult lr;
8077 auto & map = getMap (key);
8178 auto lock = std::lock_guard{getMutex (key)};
8279 auto it = map.find (subkey (key));
8380 if (it != map.end ()) {
84- lr. found_ = true ;
85- lr. record_ = it-> second ;
81+ LookupResult lr{ true , it-> second } ;
82+
8683 trackRemove (it->second .totalHits );
8784 // tsl::sparse_map's `it->second` is immutable, while it.value() is mutable
8885 it.value ().address = address;
8986 it.value ().currentHits = 0 ;
9087 it.value ().totalHits = 0 ;
9188 it.value ().sizeHint = sizeHint;
92- } else {
93- map.try_emplace (key, address, sizeHint);
89+ return lr;
9490 }
95- return lr;
91+ map.try_emplace (subkey (key), address, sizeHint);
92+
93+ return {};
9694}
9795
9896bool SparseMapIndex::replaceIfMatch (uint64_t key,
@@ -119,19 +117,18 @@ void SparseMapIndex::trackRemove(uint8_t totalHits) {
119117}
120118
121119Index::LookupResult SparseMapIndex::remove (uint64_t key) {
122- LookupResult lr;
123120 auto & map = getMap (key);
124121 auto lock = std::lock_guard{getMutex (key)};
125122
126123 auto it = map.find (subkey (key));
127124 if (it != map.end ()) {
128- lr.found_ = true ;
129- lr.record_ = it->second ;
125+ LookupResult lr{true , it->second };
130126
131127 trackRemove (it->second .totalHits );
132128 map.erase (it);
129+ return lr;
133130 }
134- return lr ;
131+ return {} ;
135132}
136133
137134bool SparseMapIndex::removeIfMatch (uint64_t key, uint32_t address) {
@@ -158,7 +155,7 @@ void SparseMapIndex::reset() {
158155size_t SparseMapIndex::computeSize () const {
159156 size_t size = 0 ;
160157 for (uint32_t i = 0 ; i < kNumBuckets ; i++) {
161- auto lock = std::lock_guard {getMutexOfBucket (i)};
158+ auto lock = std::shared_lock {getMutexOfBucket (i)};
162159 size += buckets_[i].size ();
163160 }
164161 return size;
@@ -182,7 +179,7 @@ Index::MemFootprintRange SparseMapIndex::computeMemFootprintRange() const {
182179 sizeof (std::pair<typename Map::key_type, typename Map::value_type>);
183180
184181 for (uint32_t i = 0 ; i < kNumBuckets ; i++) {
185- auto lock = std::lock_guard {getMutexOfBucket (i)};
182+ auto lock = std::shared_lock {getMutexOfBucket (i)};
186183
187184 // add the size of fixed mem used for sparse_map's member (sparse_hash)
188185 size_t bucketMemUsed = sizeof (buckets_[i]);
0 commit comments