mirror of
https://github.com/libretro/pcsx2.git
synced 2024-11-23 17:29:46 +00:00
Common: Add LRUCache
This commit is contained in:
parent
ef0fde8615
commit
cbcfe37e28
@ -67,6 +67,7 @@ target_sources(common PRIVATE
|
||||
FileSystem.h
|
||||
General.h
|
||||
HashCombine.h
|
||||
LRUCache.h
|
||||
MemcpyFast.h
|
||||
MemsetFast.inl
|
||||
MD5Digest.h
|
||||
|
137
common/LRUCache.h
Normal file
137
common/LRUCache.h
Normal file
@ -0,0 +1,137 @@
|
||||
/* PCSX2 - PS2 Emulator for PCs
|
||||
* Copyright (C) 2002-2022 PCSX2 Dev Team
|
||||
*
|
||||
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU Lesser General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with PCSX2.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
|
||||
template <class K, class V>
|
||||
class LRUCache
|
||||
{
|
||||
using CounterType = std::uint64_t;
|
||||
|
||||
struct Item
|
||||
{
|
||||
V value;
|
||||
CounterType last_access;
|
||||
};
|
||||
|
||||
using MapType = std::map<K, Item>;
|
||||
|
||||
public:
|
||||
LRUCache(std::size_t max_capacity = 16, bool manual_evict = false)
|
||||
: m_max_capacity(max_capacity)
|
||||
, m_manual_evict(manual_evict)
|
||||
{
|
||||
}
|
||||
~LRUCache() = default;
|
||||
|
||||
std::size_t GetSize() const { return m_items.size(); }
|
||||
std::size_t GetMaxCapacity() const { return m_max_capacity; }
|
||||
|
||||
void Clear() { m_items.clear(); }
|
||||
|
||||
void SetMaxCapacity(std::size_t capacity)
|
||||
{
|
||||
m_max_capacity = capacity;
|
||||
if (m_items.size() > m_max_capacity)
|
||||
Evict(m_items.size() - m_max_capacity);
|
||||
}
|
||||
|
||||
template<typename KeyT>
|
||||
V* Lookup(const KeyT& key)
|
||||
{
|
||||
auto iter = m_items.find(key);
|
||||
if (iter == m_items.end())
|
||||
return nullptr;
|
||||
|
||||
iter->second.last_access = ++m_last_counter;
|
||||
return &iter->second.value;
|
||||
}
|
||||
|
||||
V* Insert(K key, V value)
|
||||
{
|
||||
ShrinkForNewItem();
|
||||
|
||||
auto iter = m_items.find(key);
|
||||
if (iter != m_items.end())
|
||||
{
|
||||
iter->second.value = std::move(value);
|
||||
iter->second.last_access = ++m_last_counter;
|
||||
return &iter->second.value;
|
||||
}
|
||||
else
|
||||
{
|
||||
Item it;
|
||||
it.last_access = ++m_last_counter;
|
||||
it.value = std::move(value);
|
||||
auto ip = m_items.emplace(std::move(key), std::move(it));
|
||||
return &ip.first->second.value;
|
||||
}
|
||||
}
|
||||
|
||||
void Evict(std::size_t count = 1)
|
||||
{
|
||||
while (m_items.size() >= count)
|
||||
{
|
||||
typename MapType::iterator lowest = m_items.end();
|
||||
for (auto iter = m_items.begin(); iter != m_items.end(); ++iter)
|
||||
{
|
||||
if (lowest == m_items.end() || iter->second.last_access < lowest->second.last_access)
|
||||
lowest = iter;
|
||||
}
|
||||
m_items.erase(lowest);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename KeyT>
|
||||
bool Remove(const KeyT& key)
|
||||
{
|
||||
auto iter = m_items.find(key);
|
||||
if (iter == m_items.end())
|
||||
return false;
|
||||
|
||||
m_items.erase(iter);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SetManualEvict(bool block)
|
||||
{
|
||||
m_manual_evict = block;
|
||||
if (!m_manual_evict)
|
||||
ManualEvict();
|
||||
}
|
||||
|
||||
void ManualEvict()
|
||||
{
|
||||
// evict if we went over
|
||||
while (m_items.size() > m_max_capacity)
|
||||
Evict(m_items.size() - (m_max_capacity - 1));
|
||||
}
|
||||
|
||||
private:
|
||||
void ShrinkForNewItem()
|
||||
{
|
||||
if (m_items.size() < m_max_capacity)
|
||||
return;
|
||||
|
||||
Evict(m_items.size() - (m_max_capacity - 1));
|
||||
}
|
||||
|
||||
MapType m_items;
|
||||
CounterType m_last_counter = 0;
|
||||
std::size_t m_max_capacity = 0;
|
||||
bool m_manual_evict = false;
|
||||
};
|
@ -135,6 +135,7 @@
|
||||
<ClInclude Include="GL\StreamBuffer.h" />
|
||||
<ClInclude Include="FileSystem.h" />
|
||||
<ClInclude Include="HashCombine.h" />
|
||||
<ClInclude Include="LRUCache.h" />
|
||||
<ClInclude Include="MD5Digest.h" />
|
||||
<ClInclude Include="ProgressCallback.h" />
|
||||
<ClInclude Include="ScopedGuard.h" />
|
||||
@ -208,4 +209,4 @@
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
</Project>
|
@ -429,6 +429,9 @@
|
||||
<ClInclude Include="StackWalker.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="LRUCache.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
|
Loading…
Reference in New Issue
Block a user