Skip to content

Commit 63ef637

Browse files
committed
[Perf](common): optimize cache promotion and demotion
Signed-off-by: zztaki <[email protected]>
1 parent 3516d53 commit 63ef637

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

src/common/lru_cache.h

+5-19
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class LRUCache : public LRUCacheInterface<K, V> {
204204
void Remove(const K &key) override;
205205

206206
/*
207-
* @brief Get the first key that $value = value
207+
* @brief Get the first key that $value = value from lru tail to head
208208
*
209209
* @param[in] value
210210
* @param[out] key
@@ -429,10 +429,7 @@ void LRUCache<K, V, KeyTraits, ValueTraits>::RemoveLocked(const K &key) {
429429
template <typename K, typename V, typename KeyTraits, typename ValueTraits>
430430
void LRUCache<K, V, KeyTraits, ValueTraits>::MoveToFront(
431431
const typename std::list<Item>::iterator &elem) {
432-
Item duplica{elem->key, elem->value};
433-
ll_.erase(elem);
434-
ll_.push_front(duplica);
435-
cache_[*(duplica.key)] = ll_.begin();
432+
ll_.splice(ll_.begin(), ll_, elem);
436433
}
437434

438435
template <typename K, typename V, typename KeyTraits, typename ValueTraits>
@@ -699,16 +696,8 @@ bool SglLRUCache<K, KeyTraits>::MoveBack(const K &key) {
699696
if (iter == cache_.end()) {
700697
return false;
701698
}
702-
// delete the old value
703-
RemoveElement(iter->second);
704-
// put new value at tail
705-
ll_.push_back(key);
706-
cache_[key] = --ll_.end();
707-
size_++;
708-
if (cacheMetrics_ != nullptr) {
709-
cacheMetrics_->UpdateAddToCacheCount();
710-
cacheMetrics_->UpdateAddToCacheBytes(KeyTraits::CountBytes(key));
711-
}
699+
// move key to list tail
700+
ll_.splice(ll_.end(), ll_, iter->second);
712701
return true;
713702
}
714703

@@ -806,10 +795,7 @@ void SglLRUCache<K, KeyTraits>::RemoveLocked(const K &key) {
806795
template <typename K, typename KeyTraits>
807796
void SglLRUCache<K, KeyTraits>::MoveToFront(
808797
const typename std::list<K>::iterator &elem) {
809-
K tmp = *elem;
810-
ll_.erase(elem);
811-
ll_.emplace_front(tmp);
812-
cache_[tmp] = ll_.begin();
798+
ll_.splice(ll_.begin(), ll_, elem);
813799
}
814800

815801
template <typename K, typename KeyTraits>

0 commit comments

Comments
 (0)