From 0e7151975213b29e4ae9e5dd7687b4b714582010 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Thu, 29 Mar 2018 16:04:19 +0300 Subject: [PATCH] Some lru cache optimization. --- base/base_tests/lru_cache_test.cpp | 18 +++++++++--------- base/lru_cache.hpp | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/base/base_tests/lru_cache_test.cpp b/base/base_tests/lru_cache_test.cpp index 56674cbbe4..3eaae90cd0 100644 --- a/base/base_tests/lru_cache_test.cpp +++ b/base/base_tests/lru_cache_test.cpp @@ -33,7 +33,7 @@ public: size_t GetAge() const { return m_keyAge.m_age; } std::map const & GetAgeToKey() const { return m_keyAge.m_ageToKey; }; - std::map const & GetKeyToAge() const { return m_keyAge.m_keyToAge; }; + std::unordered_map const & GetKeyToAge() const { return m_keyAge.m_keyToAge; }; private: typename LruCache::KeyAge m_keyAge; @@ -42,7 +42,7 @@ private: template void TestAge(LruCacheKeyAgeTest const & keyAge, size_t expectedAge, std::map const & expectedAgeToKey, - std::map const & expectedKeyToAge) + std::unordered_map const & expectedKeyToAge) { TEST(keyAge.IsValid(), ()); TEST_EQUAL(keyAge.GetAge(), expectedAge, ()); @@ -63,49 +63,49 @@ UNIT_TEST(LruCacheAgeTest) age.InsertKey(10); { std::map const expectedAgeToKey({{1 /* age */, 10 /* key */}}); - std::map const expectedKeyToAge({{10 /* key */, 1 /* age */}}); + std::unordered_map const expectedKeyToAge({{10 /* key */, 1 /* age */}}); TestAge(age, 1 /* cache age */, expectedAgeToKey, expectedKeyToAge); } age.InsertKey(9); { std::map const expectedAgeToKey({{1, 10}, {2, 9}}); - std::map const expectedKeyToAge({{10, 1}, {9, 2}}); + std::unordered_map const expectedKeyToAge({{10, 1}, {9, 2}}); TestAge(age, 2 /* cache age */, expectedAgeToKey, expectedKeyToAge); } age.RemoveLru(); { std::map const expectedAgeToKey({{2, 9}}); - std::map const expectedKeyToAge({{9, 2}}); + std::unordered_map const expectedKeyToAge({{9, 2}}); TestAge(age, 2 /* cache age */, expectedAgeToKey, expectedKeyToAge); } age.InsertKey(11); { std::map const expectedAgeToKey({{2, 9}, {3, 11}}); - std::map const expectedKeyToAge({{9, 2}, {11, 3}}); + std::unordered_map const expectedKeyToAge({{9, 2}, {11, 3}}); TestAge(age, 3 /* cache age */, expectedAgeToKey, expectedKeyToAge); } age.UpdateAge(9); { std::map const expectedAgeToKey({{4, 9}, {3, 11}}); - std::map const expectedKeyToAge({{9, 4}, {11, 3}}); + std::unordered_map const expectedKeyToAge({{9, 4}, {11, 3}}); TestAge(age, 4 /* cache age */, expectedAgeToKey, expectedKeyToAge); } age.RemoveLru(); { std::map const expectedAgeToKey({{4, 9}}); - std::map const expectedKeyToAge({{9, 4}}); + std::unordered_map const expectedKeyToAge({{9, 4}}); TestAge(age, 4 /* cache age */, expectedAgeToKey, expectedKeyToAge); } age.InsertKey(12); { std::map const expectedAgeToKey({{4, 9}, {5, 12}}); - std::map const expectedKeyToAge({{9, 4}, {12, 5}}); + std::unordered_map const expectedKeyToAge({{9, 4}, {12, 5}}); TestAge(age, 5 /* cache age */, expectedAgeToKey, expectedKeyToAge); } } diff --git a/base/lru_cache.hpp b/base/lru_cache.hpp index 62b3ad6412..e9bdcaf73b 100644 --- a/base/lru_cache.hpp +++ b/base/lru_cache.hpp @@ -97,7 +97,7 @@ private: CHECK_EQUAL(removed, 1, ()); // Putting new age. m_ageToKey[m_age] = key; - m_keyToAge[key] = m_age; + keyToAgeIt->second = m_age; } /// \returns Least recently used key without updating the age. @@ -154,7 +154,7 @@ private: private: size_t m_age = 0; std::map m_ageToKey; - std::map m_keyToAge; + std::unordered_map m_keyToAge; }; size_t const m_maxCacheSize;