From 63ef6376d68d81f1846bbefc96a512b14998981e Mon Sep 17 00:00:00 2001 From: zztaki Date: Wed, 27 Dec 2023 21:42:33 +0800 Subject: [PATCH] [Perf](common): optimize cache promotion and demotion Signed-off-by: zztaki --- src/common/lru_cache.h | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/common/lru_cache.h b/src/common/lru_cache.h index 484e166a6b..7345af89c7 100644 --- a/src/common/lru_cache.h +++ b/src/common/lru_cache.h @@ -204,7 +204,7 @@ class LRUCache : public LRUCacheInterface { void Remove(const K &key) override; /* - * @brief Get the first key that $value = value + * @brief Get the first key that $value = value from lru tail to head * * @param[in] value * @param[out] key @@ -429,10 +429,7 @@ void LRUCache::RemoveLocked(const K &key) { template void LRUCache::MoveToFront( const typename std::list::iterator &elem) { - Item duplica{elem->key, elem->value}; - ll_.erase(elem); - ll_.push_front(duplica); - cache_[*(duplica.key)] = ll_.begin(); + ll_.splice(ll_.begin(), ll_, elem); } template @@ -699,16 +696,8 @@ bool SglLRUCache::MoveBack(const K &key) { if (iter == cache_.end()) { return false; } - // delete the old value - RemoveElement(iter->second); - // put new value at tail - ll_.push_back(key); - cache_[key] = --ll_.end(); - size_++; - if (cacheMetrics_ != nullptr) { - cacheMetrics_->UpdateAddToCacheCount(); - cacheMetrics_->UpdateAddToCacheBytes(KeyTraits::CountBytes(key)); - } + // move key to list tail + ll_.splice(ll_.end(), ll_, iter->second); return true; } @@ -806,10 +795,7 @@ void SglLRUCache::RemoveLocked(const K &key) { template void SglLRUCache::MoveToFront( const typename std::list::iterator &elem) { - K tmp = *elem; - ll_.erase(elem); - ll_.emplace_front(tmp); - cache_[tmp] = ll_.begin(); + ll_.splice(ll_.begin(), ll_, elem); } template