Skip to content

Commit 45ca54e

Browse files
byahn0996meta-codesync[bot]
authored andcommitted
Removing meaningless 'slotSize' return value from Allocator::allocate()
Summary: Currently Allocator::allocate() is returning allocated size as one of the return values, but there's no logic to allocate different size from the given 'size' parameter and it will always be the same value with the given size. (Unless it failed and returns error) Removing this from the return values since it's confusing and whenever this part of codes is looked up, it needs another look to confirm it should be the same 'size' Reviewed By: AlnisM Differential Revision: D88243894 fbshipit-source-id: 3e82430c683c77f89786e016201d915f2126e3d8
1 parent 27f6643 commit 45ca54e

File tree

5 files changed

+50
-55
lines changed

5 files changed

+50
-55
lines changed

cachelib/navy/block_cache/Allocator.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ Allocator::Allocator(RegionManager& regionManager,
4848
}
4949
}
5050

51-
std::tuple<RegionDescriptor, uint32_t, RelAddress> Allocator::allocate(
52-
uint32_t size, uint16_t priority, bool canWait, uint64_t keyHash) {
51+
std::pair<RegionDescriptor, RelAddress> Allocator::allocate(uint32_t size,
52+
uint16_t priority,
53+
bool canWait,
54+
uint64_t keyHash) {
5355
XDCHECK_LT(priority, allocators_.size());
5456
if (size == 0 || size > regionManager_.regionSize()) {
55-
return std::make_tuple(RegionDescriptor{OpenStatus::Error}, size,
56-
RelAddress());
57+
return std::make_pair(RegionDescriptor{OpenStatus::Error}, RelAddress());
5758
}
5859
RegionAllocator* ra =
5960
&allocators_[priority][keyHash % allocators_[priority].size()];
@@ -66,7 +67,7 @@ std::tuple<RegionDescriptor, uint32_t, RelAddress> Allocator::allocate(
6667
// have to wait. Every time we take a region from the clean list, we schedule
6768
// new reclaim job to refill it. Caller must close the region after data
6869
// written to the slot.
69-
std::tuple<RegionDescriptor, uint32_t, RelAddress> Allocator::allocateWith(
70+
std::pair<RegionDescriptor, RelAddress> Allocator::allocateWith(
7071
RegionAllocator& ra, uint32_t size, bool canWait) {
7172
std::unique_lock lock{ra.getLock()};
7273
RegionId rid = ra.getAllocationRegion();
@@ -75,7 +76,7 @@ std::tuple<RegionDescriptor, uint32_t, RelAddress> Allocator::allocateWith(
7576
auto [desc, addr] = region.openAndAllocate(size);
7677
XDCHECK_NE(OpenStatus::Retry, desc.status());
7778
if (desc.isReady()) {
78-
return std::make_tuple(std::move(desc), size, addr);
79+
return std::make_pair(std::move(desc), addr);
7980
}
8081
XDCHECK_EQ(OpenStatus::Error, desc.status());
8182
// Buffer has been fully allocated. Release the region by scheduling an
@@ -101,7 +102,7 @@ std::tuple<RegionDescriptor, uint32_t, RelAddress> Allocator::allocateWith(
101102
allocRetryWaits_.inc();
102103
waiter->baton_.wait();
103104
}
104-
return std::make_tuple(RegionDescriptor{status}, size, RelAddress{});
105+
return std::make_pair(RegionDescriptor{status}, RelAddress{});
105106
}
106107
XDCHECK(status == OpenStatus::Ready);
107108
XDCHECK(!waiter);
@@ -114,7 +115,7 @@ std::tuple<RegionDescriptor, uint32_t, RelAddress> Allocator::allocateWith(
114115
ra.setAllocationRegion(rid);
115116
auto [desc, addr] = region.openAndAllocate(size);
116117
XDCHECK_EQ(OpenStatus::Ready, desc.status());
117-
return std::make_tuple(std::move(desc), size, addr);
118+
return std::make_pair(std::move(desc), addr);
118119
}
119120

120121
void Allocator::close(RegionDescriptor&& desc) {

cachelib/navy/block_cache/Allocator.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,19 @@ class Allocator {
9595
// @param keyHash Hash of the key to allocate for. Used for deciding the
9696
// allocator if there's more than one for the priority.
9797
//
98-
// Returns a tuple containing region descriptor, allocated slotSize and
99-
// allocated address
98+
// Returns a pair containing region descriptor and allocated address
10099
// The region descriptor contains region id, open mode and status,
101100
// which is one of the following
102-
// - Ready Fills @addr and @slotSize
101+
// - Ready Fills @addr
103102
// - Retry Retry later, reclaim is running
104103
// - Error Can't allocate this size even later (hard failure)
105104
// When allocating with a priority, the priority must NOT exceed the
106105
// max priority which is (@numPriorities - 1) specified when constructing
107106
// this allocator.
108-
std::tuple<RegionDescriptor, uint32_t, RelAddress> allocate(uint32_t size,
109-
uint16_t priority,
110-
bool canWait,
111-
uint64_t keyHash);
107+
std::pair<RegionDescriptor, RelAddress> allocate(uint32_t size,
108+
uint16_t priority,
109+
bool canWait,
110+
uint64_t keyHash);
112111

113112
// Closes the region.
114113
void close(RegionDescriptor&& rid);
@@ -134,9 +133,10 @@ class Allocator {
134133
void flushAndReleaseRegionFromRALocked(RegionAllocator& ra, bool flushAsync);
135134

136135
// Allocates @size bytes in region allocator @ra. If succeed (enough space),
137-
// returns region descriptor, size and address.
138-
std::tuple<RegionDescriptor, uint32_t, RelAddress> allocateWith(
139-
RegionAllocator& ra, uint32_t size, bool wait);
136+
// returns region descriptor and address.
137+
std::pair<RegionDescriptor, RelAddress> allocateWith(RegionAllocator& ra,
138+
uint32_t size,
139+
bool wait);
140140

141141
RegionManager& regionManager_;
142142
// Multiple allocators when we use priority-based allocation

cachelib/navy/block_cache/BlockCache.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ Status BlockCache::insert(HashedKey hk, BufferView value) {
213213
}
214214

215215
// All newly inserted items are assigned with the lowest priority
216-
auto [desc, slotSize, addr] = allocator_.allocate(
217-
size, kDefaultItemPriority, true /* canWait */, hk.keyHash());
216+
auto [desc, addr] = allocator_.allocate(size, kDefaultItemPriority,
217+
true /* canWait */, hk.keyHash());
218218

219219
switch (desc.status()) {
220220
case OpenStatus::Error:
@@ -231,11 +231,11 @@ Status BlockCache::insert(HashedKey hk, BufferView value) {
231231

232232
// After allocation a region is opened for writing. Until we close it, the
233233
// region would not be reclaimed and index never gets an invalid entry.
234-
const auto status = writeEntry(addr, slotSize, hk, value);
235-
auto newObjSizeHint = encodeSizeHint(slotSize);
234+
const auto status = writeEntry(addr, size, hk, value);
235+
auto newObjSizeHint = encodeSizeHint(size);
236236
if (status == Status::Ok) {
237237
const auto lr = index_->insert(
238-
hk.keyHash(), encodeRelAddress(addr.add(slotSize)), newObjSizeHint);
238+
hk.keyHash(), encodeRelAddress(addr.add(size)), newObjSizeHint);
239239
// We replaced an existing key in the index
240240
uint64_t newObjSize = decodeSizeHint(newObjSizeHint);
241241
uint64_t oldObjSize = 0;
@@ -811,7 +811,7 @@ AllocatorApiResult BlockCache::reinsertOrRemoveItem(
811811
: std::min<uint16_t>(lr.currentHits(), numPriorities_ - 1);
812812

813813
uint32_t size = serializedSize(hk.key().size(), value.size());
814-
auto [desc, slotSize, addr] =
814+
auto [desc, addr] =
815815
allocator_.allocate(size, priority, false /* canWait */, hk.keyHash());
816816

817817
switch (desc.status()) {
@@ -838,18 +838,17 @@ AllocatorApiResult BlockCache::reinsertOrRemoveItem(
838838

839839
// After allocation a region is opened for writing. Until we close it, the
840840
// region would not be reclaimed and index never gets an invalid entry.
841-
const auto status = writeEntry(addr, slotSize, hk, value);
841+
const auto status = writeEntry(addr, size, hk, value);
842842
if (status != Status::Ok) {
843843
recordEvent(hk.key(), AllocatorApiEvent::NVM_REINSERT,
844844
AllocatorApiResult::FAILED, entrySize);
845845
reinsertionErrorCount_.inc();
846846
return removeItem(false);
847847
}
848848

849-
const auto replaced =
850-
index_->replaceIfMatch(hk.keyHash(),
851-
encodeRelAddress(addr.add(slotSize)),
852-
encodeRelAddress(currAddr));
849+
const auto replaced = index_->replaceIfMatch(hk.keyHash(),
850+
encodeRelAddress(addr.add(size)),
851+
encodeRelAddress(currAddr));
853852
if (!replaced) {
854853
recordEvent(hk.key(), AllocatorApiEvent::NVM_REINSERT,
855854
AllocatorApiResult::INVALIDATED, entrySize);
@@ -866,13 +865,13 @@ AllocatorApiResult BlockCache::reinsertOrRemoveItem(
866865
}
867866

868867
Status BlockCache::writeEntry(RelAddress addr,
869-
uint32_t slotSize,
868+
uint32_t size,
870869
HashedKey hk,
871870
BufferView value) {
872-
XDCHECK_LE(addr.offset() + slotSize, regionManager_.regionSize());
873-
XDCHECK_EQ(slotSize % allocAlignSize_, 0ULL)
874-
<< folly::sformat(" alignSize={}, size={}", allocAlignSize_, slotSize);
875-
auto buffer = Buffer(slotSize);
871+
XDCHECK_LE(addr.offset() + size, regionManager_.regionSize());
872+
XDCHECK_EQ(size % allocAlignSize_, 0ULL)
873+
<< folly::sformat(" alignSize={}, size={}", allocAlignSize_, size);
874+
auto buffer = Buffer(size);
876875

877876
// Copy descriptor and the key to the end
878877
size_t descOffset = buffer.size() - sizeof(EntryDesc);
@@ -945,7 +944,7 @@ Status BlockCache::readEntry(const RegionDescriptor& readDesc,
945944
return Status::NotFound;
946945
}
947946

948-
// Update slot size to actual, defined by key and value size
947+
// Update the size to actual, defined by key and value size
949948
uint32_t size = serializedSize(desc.keySize, desc.valueSize);
950949
if (buffer.size() > size) {
951950
// Read more than actual size. Trim the invalid data in the beginning

cachelib/navy/block_cache/BlockCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ class BlockCache final : public Engine {
267267
// the performance point of view, but makes sense to track them for perf:
268268
// especially portion on CPU time spent in std::memcpy.
269269
// @param addr Address to write this entry into
270-
// @param slotSize Number of bytes this entry will take up on the device
270+
// @param size Number of bytes this entry will take up on the device
271271
// @param hk Key of the entry
272272
// @param value Payload of the entry
273273
Status writeEntry(RelAddress addr,
274-
uint32_t slotSize,
274+
uint32_t size,
275275
HashedKey hk,
276276
BufferView value);
277277
// @param readDesc Descriptor for reading. This must be valid

cachelib/navy/block_cache/tests/AllocatorTest.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ TEST(Allocator, RegionSyncInMemBuffers) {
6262

6363
// Write to 3 regions
6464
RelAddress addr;
65-
uint32_t slotSize = 0;
6665

6766
// First allocation will fail with kicking a reclaim
6867
{
6968
RegionDescriptor desc{OpenStatus::Retry};
70-
std::tie(desc, slotSize, addr) =
69+
std::tie(desc, addr) =
7170
allocator.allocate(1024, kNoPriority, false, kKeyHash);
7271
EXPECT_EQ(OpenStatus::Retry, desc.status());
7372
// Reclaim should have been started; complete the reclaim
@@ -79,7 +78,7 @@ TEST(Allocator, RegionSyncInMemBuffers) {
7978
// tracks the current region that is full region if present.
8079
{
8180
RegionDescriptor desc{OpenStatus::Retry};
82-
std::tie(desc, slotSize, addr) =
81+
std::tie(desc, addr) =
8382
allocator.allocate(1024, kNoPriority, false, kKeyHash);
8483
EXPECT_TRUE(desc.isReady());
8584

@@ -99,7 +98,7 @@ TEST(Allocator, RegionSyncInMemBuffers) {
9998
// 15 allocs exhaust region's space,but no reclaims scheduled.
10099
for (uint32_t j = 0; j < 15; j++) {
101100
RegionDescriptor desc{OpenStatus::Retry};
102-
std::tie(desc, slotSize, addr) =
101+
std::tie(desc, addr) =
103102
allocator.allocate(1024, kNoPriority, false, kKeyHash);
104103
EXPECT_TRUE(desc.isReady());
105104
EXPECT_EQ(RegionId{i}, addr.rid());
@@ -120,7 +119,7 @@ TEST(Allocator, RegionSyncInMemBuffers) {
120119
// of region 0 again
121120
{
122121
RegionDescriptor desc{OpenStatus::Retry};
123-
std::tie(desc, slotSize, addr) =
122+
std::tie(desc, addr) =
124123
allocator.allocate(1024, kNoPriority, false, kKeyHash);
125124
EXPECT_TRUE(desc.isReady());
126125
EXPECT_EQ(RegionId{3}, addr.rid());
@@ -161,10 +160,9 @@ TEST(Allocator, TestInMemBufferStates) {
161160
injectPauseSet("pause_flush_done");
162161

163162
RelAddress addr;
164-
uint32_t slotSize = 0;
165163
{
166164
RegionDescriptor desc{OpenStatus::Retry};
167-
std::tie(desc, slotSize, addr) =
165+
std::tie(desc, addr) =
168166
allocator.allocate(1024, kNoPriority, false, kKeyHash);
169167
EXPECT_EQ(OpenStatus::Retry, desc.status());
170168
}
@@ -177,7 +175,7 @@ TEST(Allocator, TestInMemBufferStates) {
177175
{
178176
RegionDescriptor wdesc{OpenStatus::Retry};
179177
// There should be clean region available
180-
std::tie(wdesc, slotSize, addr) =
178+
std::tie(wdesc, addr) =
181179
allocator.allocate(1024, kNoPriority, false, kKeyHash);
182180
EXPECT_TRUE(wdesc.isReady());
183181
EXPECT_EQ(0, wdesc.id().index());
@@ -190,7 +188,7 @@ TEST(Allocator, TestInMemBufferStates) {
190188
// Fill the remaining space in the first region
191189
for (uint32_t j = 0; j < 15; j++) {
192190
RegionDescriptor desc{OpenStatus::Retry};
193-
std::tie(desc, slotSize, addr) =
191+
std::tie(desc, addr) =
194192
allocator.allocate(1024, kNoPriority, false, kKeyHash);
195193
EXPECT_TRUE(desc.isReady());
196194
EXPECT_EQ(0, desc.id().index());
@@ -202,7 +200,7 @@ TEST(Allocator, TestInMemBufferStates) {
202200
// Do another allocation to flush/close the first region.
203201
// A reclaim will also be triggered
204202
RegionDescriptor desc{OpenStatus::Retry};
205-
std::tie(desc, slotSize, addr) =
203+
std::tie(desc, addr) =
206204
allocator.allocate(1024, kNoPriority, false, kKeyHash);
207205
EXPECT_EQ(OpenStatus::Ready, desc.status());
208206
EXPECT_EQ(1, desc.id().index());
@@ -252,15 +250,14 @@ TEST(Allocator, UsePriorities) {
252250
injectPauseSet("pause_reclaim_done");
253251

254252
// Allocate to make sure a reclaim is triggered
255-
auto [desc, slotSize, addr] = allocator.allocate(1024, 0, false, kKeyHash);
253+
auto [desc, addr] = allocator.allocate(1024, 0, false, kKeyHash);
256254
EXPECT_EQ(OpenStatus::Retry, desc.status());
257255
EXPECT_TRUE(injectPauseWait("pause_reclaim_done"));
258256

259257
// Allocate one item from each priortiy, we should see each allocation
260258
// results in a new region being allocated for its priority
261259
for (uint16_t pri = 0; pri < 3; pri++) {
262-
std::tie(desc, slotSize, addr) =
263-
allocator.allocate(1024, pri, false, kKeyHash);
260+
std::tie(desc, addr) = allocator.allocate(1024, pri, false, kKeyHash);
264261
EXPECT_TRUE(desc.isReady());
265262
EXPECT_EQ(RegionId{pri}, addr.rid());
266263
EXPECT_EQ(pri, rm->getRegion(addr.rid()).getPriority());
@@ -299,7 +296,7 @@ TEST(Allocator, UseDifferentAllocatorsForPriorities) {
299296
injectPauseSet("pause_reclaim_done");
300297

301298
// Allocate to make sure a reclaim is triggered
302-
auto [desc, slotSize, addr] = allocator.allocate(1024, 0, false, kKeyHash);
299+
auto [desc, addr] = allocator.allocate(1024, 0, false, kKeyHash);
303300
EXPECT_EQ(OpenStatus::Retry, desc.status());
304301
EXPECT_TRUE(injectPauseWait("pause_reclaim_done"));
305302

@@ -310,8 +307,7 @@ TEST(Allocator, UseDifferentAllocatorsForPriorities) {
310307
// For each priority, allocate one item per allocator
311308
for (uint32_t index = 0; index < allocatorsPerPriority[pri]; index++) {
312309
// Use index as keyhash to allocate via a specific allocator.
313-
std::tie(desc, slotSize, addr) =
314-
allocator.allocate(1024, pri, false, index);
310+
std::tie(desc, addr) = allocator.allocate(1024, pri, false, index);
315311
EXPECT_TRUE(desc.isReady());
316312
EXPECT_EQ(RegionId{allocatedRegion++}, addr.rid());
317313
EXPECT_EQ(pri, rm->getRegion(addr.rid()).getPriority());
@@ -325,8 +321,7 @@ TEST(Allocator, UseDifferentAllocatorsForPriorities) {
325321
// reclaim.
326322
for (uint32_t index = 0; index < allocatorsPerPriority[pri]; index++) {
327323
// Use index as keyhash to allocate via a specific allocator.
328-
std::tie(desc, slotSize, addr) =
329-
allocator.allocate(1024, pri, false, index);
324+
std::tie(desc, addr) = allocator.allocate(1024, pri, false, index);
330325
EXPECT_TRUE(desc.isReady());
331326
EXPECT_EQ(pri, rm->getRegion(addr.rid()).getPriority());
332327
// The allocation should be from an existing region as the second item

0 commit comments

Comments
 (0)