From a4ed5923ae010478d9a03a5b52134ffc5f045fd4 Mon Sep 17 00:00:00 2001 From: vng Date: Sun, 14 Aug 2011 17:49:38 +0300 Subject: [PATCH] [by familom]: Try to erase least recently used element only for non-empty list in MRUCache::Add(). --- base/mru_cache.hpp | 48 ++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/base/mru_cache.hpp b/base/mru_cache.hpp index d0f3a8b7f5..2546f3c336 100644 --- a/base/mru_cache.hpp +++ b/base/mru_cache.hpp @@ -111,38 +111,36 @@ namespace my if (HasElem(key)) Remove(key); - typename list::iterator it = (++m_list.rbegin()).base(); - - while (m_curWeight + weight > m_maxWeight) + if (!m_list.empty()) { - if (m_list.empty()) - return; - - KeyT k = *it; - - /// erasing only unlocked elements - if (m_map[k].m_lockCount == 0) + typename list::iterator it = (++m_list.rbegin()).base(); + while (m_curWeight + weight > m_maxWeight) { - m_curWeight -= m_map[k].m_weight; - ValueTraitsT::Evict(m_map[k].m_value); - m_map.erase(k); + KeyT k = *it; - typename list::iterator nextIt = it; - if (nextIt != m_list.begin()) + /// erasing only unlocked elements + if (m_map[k].m_lockCount == 0) { - --nextIt; - m_list.erase(it); - it = nextIt; + m_curWeight -= m_map[k].m_weight; + ValueTraitsT::Evict(m_map[k].m_value); + m_map.erase(k); + + typename list::iterator nextIt = it; + if (nextIt != m_list.begin()) + { + --nextIt; + m_list.erase(it); + it = nextIt; + } + else + { + m_list.erase(it); + break; + } } else - { - m_list.erase(it); - break; - } + --it; } - else - --it; - } ASSERT(m_curWeight + weight <= m_maxWeight, ());