Skip to content

Commit 782e18b

Browse files
committed
Unconditionally destroy toReleaseHandle
in findEviction, and rely only of recount being zero when releasing items back to allocator.
1 parent 6df4223 commit 782e18b

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,60 +1259,43 @@ CacheAllocator<CacheTrait>::findEviction(PoolId pid, ClassId cid) {
12591259
// for chained items, the ownership of the parent can change. We try to
12601260
// evict what we think as parent and see if the eviction of parent
12611261
// recycles the child we intend to.
1262-
auto toReleaseHandle = evictNormalItem(*candidate);
1263-
auto ref = candidate->unmarkMoving();
1262+
{
1263+
auto toReleaseHandle = evictNormalItem(*candidate);
1264+
// destroy toReleseHandle. The item won't be release to allocator
1265+
// since we marked it as moving.
1266+
}
1267+
const auto ref = candidate->unmarkMoving();
12641268

1265-
if (toReleaseHandle || ref == 0u) {
1269+
if (ref == 0u) {
1270+
// recycle the item. it's safe to do so, even if toReleaseHandle was
1271+
// NULL. If `ref` == 0 then it means that we are the last holder of
1272+
// that item.
12661273
if (candidate->hasChainedItem()) {
12671274
(*stats_.chainedItemEvictions)[pid][cid].inc();
12681275
} else {
12691276
(*stats_.regularItemEvictions)[pid][cid].inc();
12701277
}
12711278

12721279
if (auto eventTracker = getEventTracker()) {
1273-
eventTracker->record(
1274-
AllocatorApiEvent::DRAM_EVICT, toReleaseHandle->getKey(),
1275-
AllocatorApiResult::DRAM_EVICTED, toReleaseHandle->getSize(),
1276-
toReleaseHandle->getConfiguredTTL().count());
1280+
eventTracker->record(AllocatorApiEvent::DRAM_EVICT, candidate->getKey(),
1281+
AllocatorApiResult::DRAM_EVICTED,
1282+
candidate->getSize(),
1283+
candidate->getConfiguredTTL().count());
12771284
}
1278-
} else {
1279-
if (candidate->hasChainedItem()) {
1280-
stats_.evictFailParentAC.inc();
1281-
} else {
1282-
stats_.evictFailAC.inc();
1283-
}
1284-
}
1285-
1286-
if (toReleaseHandle) {
1287-
XDCHECK(toReleaseHandle.get() == candidate);
1288-
XDCHECK(toRecycle == candidate || toRecycle->isChainedItem());
1289-
XDCHECK_EQ(1u, toReleaseHandle->getRefCount());
1290-
1291-
// We manually release the item here because we don't want to
1292-
// invoke the Item Handle's destructor which will be decrementing
1293-
// an already zero refcount, which will throw exception
1294-
auto& itemToRelease = *toReleaseHandle.release();
1295-
1296-
// Decrementing the refcount because we want to recycle the item
1297-
ref = decRef(itemToRelease);
1298-
XDCHECK_EQ(0u, ref);
12991285

13001286
// check if by releasing the item we intend to, we actually
13011287
// recycle the candidate.
1302-
if (ReleaseRes::kRecycled ==
1303-
releaseBackToAllocator(itemToRelease, RemoveContext::kEviction,
1304-
/* isNascent */ false, toRecycle)) {
1305-
return toRecycle;
1306-
}
1307-
} else if (ref == 0u) {
1308-
// it's safe to recycle the item here as there are no more
1309-
// references and the item could not been marked as moving
1310-
// by other thread since it's detached from MMContainer.
13111288
if (ReleaseRes::kRecycled ==
13121289
releaseBackToAllocator(*candidate, RemoveContext::kEviction,
13131290
/* isNascent */ false, toRecycle)) {
13141291
return toRecycle;
13151292
}
1293+
} else {
1294+
if (candidate->hasChainedItem()) {
1295+
stats_.evictFailParentAC.inc();
1296+
} else {
1297+
stats_.evictFailAC.inc();
1298+
}
13161299
}
13171300
}
13181301
return nullptr;

0 commit comments

Comments
 (0)