From 603a48bec2ce43aa0ac922705ba8d9ae188d2be0 Mon Sep 17 00:00:00 2001 From: RobinTF <83676088+RobinTF@users.noreply.github.com> Date: Fri, 2 Aug 2024 19:01:29 +0200 Subject: [PATCH] Adjust Cache to simplify code a bit --- src/util/Cache.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/util/Cache.h b/src/util/Cache.h index 5d59069aa..2de3a307a 100644 --- a/src/util/Cache.h +++ b/src/util/Cache.h @@ -237,7 +237,7 @@ class FlexibleCache { return; } auto newSize = _valueSizeGetter(*(*this)[key]); - auto& oldSize = _sizeMap.at(key); + auto& sizeInMap = _sizeMap.at(key); // Entry has grown too big to completely keep within the cache or we can't // fit it in the cache if (_maxSizeSingleEntry < newSize || @@ -246,12 +246,12 @@ class FlexibleCache { return; } - if (newSize >= oldSize) { - _totalSizeNonPinned += newSize - oldSize; - } else { - _totalSizeNonPinned -= oldSize - newSize; - } - oldSize = newSize; + // `MemorySize` type does not allow for negative values, but they are safe + // here, so we do it in size_t instead and convert back. + _totalSizeNonPinned = + MemorySize::bytes(_totalSizeNonPinned.getBytes() - + sizeInMap.getBytes() + newSize.getBytes()); + sizeInMap = newSize; if (_totalSizePinned <= _maxSize) { makeRoomIfFits(0_B); }