@@ -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
456456template <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>
623625typename MMS3FIFO::Container<T, HookPtr>::LockedIterator
624626MMS3FIFO::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