fmatvec  0.0.0
fmatvec::LRUCache< In, Out, InHash, InKeyEqual, UnorderedMapAllocator, ListAllocator > Class Template Reference

#include <lrucache.h>

Public Member Functions

 LRUCache (size_t size_, size_t smallSize=45, double bucketSizeFactor=1.5)
 Create a LRU cache of at most size items.
 
 ~LRUCache ()
 Prints the cache hit rate in debug builds to the stream Atom::Debug.
 
std::pair< Out &, bool > operator() (const In &in)
 
double getCacheHitRate () const
 
void setName (const std::string &name_)
 

Private Attributes

std::list< std::pair< const In &, Out >, ListAllocator > itemList
 
std::unordered_map< In, decltype(itemList.begin()), InHash, InKeyEqual, UnorderedMapAllocator > itemMap
 
size_t size
 
size_t allCalls { 0 }
 
size_t cacheHits { 0 }
 
std::string name
 
bool useSmallSizeCode
 
std::vector< size_t > timeCache
 
std::vector< In > inCache
 
std::vector< Out > outCache
 

Detailed Description

template<class In, class Out, class InHash = boost::hash<In>, class InKeyEqual = std::equal_to<In>, class UnorderedMapAllocator = std::allocator<std::pair<const In, typename std::list<std::pair<const In&,Out>>::iterator>>, class ListAllocator = std::allocator<std::pair<const In&, Out>>>
class fmatvec::LRUCache< In, Out, InHash, InKeyEqual, UnorderedMapAllocator, ListAllocator >

A generic LRU cache. For a size <=smallSize an internal array is used as cache which is searched linearly. Its performance is O(smallSize) but smallSize is fixed and small. For a size >smallSize a hash map implementation is used. Its performance is O(1) but it may be slower than the first for small size.

The cache size can be override globally with the envvar FMATVEC_LRUCACHE_SIZE. This is useful together with the Atom::Debug message stream which print the cache hit rate in the dtor to check if a higher cache size will influence the cache hit rate of a program.

Member Function Documentation

◆ operator()()

template<class In , class Out , class InHash , class InKeyEqual , class UnorderedMapAllocator , class ListAllocator >
std::pair< Out &, bool > fmatvec::LRUCache< In, Out, InHash, InKeyEqual, UnorderedMapAllocator, ListAllocator >::operator() ( const In &  in)
inline

Add the key "in" to the cache. Ensures that no more then size entries exist in the cache. Returns a reference to the cache value and a bool which indicates if a new entry was created. If a new entry was created, than the reference is a uninizialized new value which must be set. If a existing entry is returned, than the reference is a reference to the existing cache entry.


The documentation for this class was generated from the following file: