20#ifndef SHARDED_CACHE_H
21#define SHARDED_CACHE_H
24#include <shared_mutex>
25#include <unordered_map>
39template <
typename KEY,
typename VALUE, std::
size_t SHARDS = 256>
44 bool Get(
const KEY& aKey,
VALUE& aValue )
const
47 std::shared_lock<std::shared_mutex> lock( shard.
mutex );
49 auto it = shard.
map.find( aKey );
51 if( it == shard.
map.end() )
58 void Set(
const KEY& aKey,
const VALUE& aValue )
61 std::unique_lock<std::shared_mutex> lock( shard.
mutex );
63 shard.
map[aKey] = aValue;
77 std::unique_lock<std::shared_mutex> lock( shard.mutex );
86 tp.submit_loop( std::size_t( 0 ), SHARDS,
87 [
this]( std::size_t aShard )
89 std::unique_lock<std::shared_mutex> lock(
m_shards[aShard].mutex );
99 std::shared_lock<std::shared_mutex> lock( shard.mutex );
101 if( !shard.map.empty() )
115 std::size_t total = 0;
119 std::shared_lock<std::shared_mutex> lock( shard.mutex );
120 total += shard.map.size();
129 std::unordered_map<KEY, VALUE>
map;
134 return m_shards[std::hash<KEY>{}( aKey ) % SHARDS];
139 return m_shards[std::hash<KEY>{}( aKey ) % SHARDS];
A concurrent key/value cache split into independently locked shards.
const SHARD & shardFor(const KEY &aKey) const
std::array< SHARD, SHARDS > m_shards
bool Get(const KEY &aKey, VALUE &aValue) const
Look up a key. Returns false on a miss and leaves aValue untouched.
std::size_t Size() const
Only Clear() needs this; an exact concurrent size has no sound external use, so private.
void Set(const KEY &aKey, const VALUE &aValue)
SHARD & shardFor(const KEY &aKey)
static constexpr std::size_t PARALLEL_CLEAR_THRESHOLD
Entry count above which Clear() fans its per-shard deallocation across the worker pool.
std::unordered_map< KEY, VALUE > map
@ VALUE
Field Value of part, i.e. "3.3K".
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
BS::priority_thread_pool thread_pool