Skip to content

Commit 32fd6c1

Browse files
byahn0996facebook-github-bot
authored andcommitted
Applying minor improvements to SparseMapIndex too
Summary: With D73965289 reviewed, I fixed couple things on FixedSizeIndex and want to apply same things to SparseMapIndex. * Using shared lock for compute functions * Using LookupResult constructor rather than having implementation class modify private fields directly. Reviewed By: alikhtarov Differential Revision: D73965271 fbshipit-source-id: c08443dc7256d70d1449f3679912f08e39ef90bc
1 parent 941cd88 commit 32fd6c1

2 files changed

Lines changed: 19 additions & 24 deletions

File tree

cachelib/navy/block_cache/Index.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ class Index {
102102
private:
103103
ItemRecord record_;
104104
bool found_{false};
105-
106-
friend class SparseMapIndex;
107105
};
108106

109107
struct MemFootprintRange {

cachelib/navy/block_cache/SparseMapIndex.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,53 +46,51 @@ void SparseMapIndex::setHits(uint64_t key,
4646
}
4747

4848
Index::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

6363
Index::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

7674
Index::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

9896
bool SparseMapIndex::replaceIfMatch(uint64_t key,
@@ -119,19 +117,18 @@ void SparseMapIndex::trackRemove(uint8_t totalHits) {
119117
}
120118

121119
Index::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

137134
bool SparseMapIndex::removeIfMatch(uint64_t key, uint32_t address) {
@@ -158,7 +155,7 @@ void SparseMapIndex::reset() {
158155
size_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

Comments
 (0)