Some lru cache optimization.

This commit is contained in:
Vladimir Byko-Ianko 2018-03-29 16:04:19 +03:00 committed by mpimenov
parent cf7e192f97
commit 0e71519752
2 changed files with 11 additions and 11 deletions

View file

@ -33,7 +33,7 @@ public:
size_t GetAge() const { return m_keyAge.m_age; }
std::map<size_t, Key> const & GetAgeToKey() const { return m_keyAge.m_ageToKey; };
std::map<Key, size_t> const & GetKeyToAge() const { return m_keyAge.m_keyToAge; };
std::unordered_map<Key, size_t> const & GetKeyToAge() const { return m_keyAge.m_keyToAge; };
private:
typename LruCache<Key, Value>::KeyAge m_keyAge;
@ -42,7 +42,7 @@ private:
template <typename Key, typename Value>
void TestAge(LruCacheKeyAgeTest<Key, Value> const & keyAge,
size_t expectedAge, std::map<size_t, Key> const & expectedAgeToKey,
std::map<Key, size_t> const & expectedKeyToAge)
std::unordered_map<Key, size_t> const & expectedKeyToAge)
{
TEST(keyAge.IsValid(), ());
TEST_EQUAL(keyAge.GetAge(), expectedAge, ());
@ -63,49 +63,49 @@ UNIT_TEST(LruCacheAgeTest)
age.InsertKey(10);
{
std::map<size_t, Key> const expectedAgeToKey({{1 /* age */, 10 /* key */}});
std::map<Key, size_t> const expectedKeyToAge({{10 /* key */, 1 /* age */}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{10 /* key */, 1 /* age */}});
TestAge(age, 1 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.InsertKey(9);
{
std::map<size_t, Key> const expectedAgeToKey({{1, 10}, {2, 9}});
std::map<Key, size_t> const expectedKeyToAge({{10, 1}, {9, 2}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{10, 1}, {9, 2}});
TestAge(age, 2 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.RemoveLru();
{
std::map<size_t, Key> const expectedAgeToKey({{2, 9}});
std::map<Key, size_t> const expectedKeyToAge({{9, 2}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 2}});
TestAge(age, 2 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.InsertKey(11);
{
std::map<size_t, Key> const expectedAgeToKey({{2, 9}, {3, 11}});
std::map<Key, size_t> const expectedKeyToAge({{9, 2}, {11, 3}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 2}, {11, 3}});
TestAge(age, 3 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.UpdateAge(9);
{
std::map<size_t, Key> const expectedAgeToKey({{4, 9}, {3, 11}});
std::map<Key, size_t> const expectedKeyToAge({{9, 4}, {11, 3}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 4}, {11, 3}});
TestAge(age, 4 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.RemoveLru();
{
std::map<size_t, Key> const expectedAgeToKey({{4, 9}});
std::map<Key, size_t> const expectedKeyToAge({{9, 4}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 4}});
TestAge(age, 4 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.InsertKey(12);
{
std::map<size_t, Key> const expectedAgeToKey({{4, 9}, {5, 12}});
std::map<Key, size_t> const expectedKeyToAge({{9, 4}, {12, 5}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 4}, {12, 5}});
TestAge(age, 5 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
}

View file

@ -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<size_t, Key> m_ageToKey;
std::map<Key, size_t> m_keyToAge;
std::unordered_map<Key, size_t> m_keyToAge;
};
size_t const m_maxCacheSize;