Skip to content

Commit d048750

Browse files
allow ghost to shrink
1 parent 7f9f9c3 commit d048750

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

cachelib/allocator/MMS3FIFO.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ class MMS3FIFO {
386386
}
387387

388388
// As the cache grows, the ghost queue may need to be resized
389-
void maybeGrowGhostLocked() noexcept;
389+
void maybeResizeGhostLocked() noexcept;
390390

391391
// Returns the hash of node's key
392392
static size_t hashNode(const T& node) noexcept {
@@ -454,19 +454,21 @@ MMS3FIFO::Container<T, HookPtr>::Container(serialization::MMS3FIFOObject object,
454454
}
455455

456456
template <typename T, MMS3FIFO::Hook<T> T::* HookPtr>
457-
void MMS3FIFO::Container<T, HookPtr>::maybeGrowGhostLocked() noexcept {
458-
size_t capacity = lru_.size();
457+
void MMS3FIFO::Container<T, HookPtr>::maybeResizeGhostLocked() noexcept {
458+
size_t lruSize = lru_.size();
459459

460-
// Only consider growing ghost when lru size doubles.
461-
// Right now we don't shrink ghost queue.
462-
if (2 * capacity_ > capacity || capacity == 0) {
460+
// Grow when size doubled, shrink when halved
461+
const bool shouldGrow = lruSize >= 2 * capacity_;
462+
const bool shouldShrink = lruSize <= capacity_ / 2;
463+
464+
if (!shouldGrow && !shouldShrink) {
463465
return;
464466
}
465467

466468
size_t expectedGhostSize =
467-
static_cast<size_t>(capacity * config_.ghostSizePercent / 100);
469+
static_cast<size_t>(lruSize * config_.ghostSizePercent / 100);
468470
ghostQueue_.resize(expectedGhostSize);
469-
capacity_ = capacity;
471+
capacity_ = lruSize;
470472
}
471473

472474
// We have no notion of "reconfiguring lock"
@@ -623,7 +625,7 @@ template <typename T, MMS3FIFO::Hook<T> T::* HookPtr>
623625
typename MMS3FIFO::Container<T, HookPtr>::LockedIterator
624626
MMS3FIFO::Container<T, HookPtr>::getEvictionIterator() noexcept {
625627
LockHolder l(*lruMutex_);
626-
maybeGrowGhostLocked();
628+
maybeResizeGhostLocked();
627629
rebalanceForEviction();
628630
// Cache is full now so we know it's max size
629631
return LockedIterator{std::move(l), *this};

0 commit comments

Comments
 (0)