Template Class CacheManager

Nested Relationships

Nested Types

Class Documentation

template<typename TreeNode, bool force_hash_collisions>
class CacheManager

This class implements a cache manager useful for the cases when the number of times the cached objects will be accessed is known.

Template Parameters:
  • TreeNode – The evaluation tree node type used as the cache key.

  • force_hash_collisions – If true, forces all hash values to 0 (for testing collision safety).

Public Types

using key_type = TreeNode
using custom_evaluator_type = std::function<ResultPtr(key_type const&, CacheManager&)>

A custom evaluator type. evaluate() consults the cache’s custom evaluator (if set) before applying its standard recursive scheme to each non-leaf node, invoking it as custom_evaluator(node, cache):

  • a non-null ResultPtr means “I evaluated this subtree myself” (e.g. blocked over a contracted index to bound peak memory) and is used (and cached) as-is;

  • a null result means “decline”, and the standard scheme evaluates the node (its children are in turn consulted via the threaded cache). The leaf evaluator is captured by the callable. A custom evaluator that (re)evaluates the subtree by the standard scheme on a transformed operand set should do so on a scratch cache (e.g. CacheManager::empty()) to avoid both re-interception and polluting this cache with partial results.

Public Functions

inline void set_custom_evaluator(custom_evaluator_type fn) noexcept

Sets the custom evaluator (see custom_evaluator_type). Pass an empty std::function to clear it.

inline custom_evaluator_type const &custom_evaluator() const noexcept
Returns:

the custom evaluator (empty if none is set).

template<typename Iterable, typename PersistencePred = all_non_persistent>
inline explicit CacheManager(Iterable &&decaying, PersistencePred is_persistent = {}) noexcept

Note

P nodes should be registered with whatever use-count is convenient (it is not consulted for P entries) and may have a count of 1 even though NP caching only registers nodes repeated min_repeats times.

Parameters:
  • decaying – iterable of (node, use-count) pairs to register for caching.

  • is_persistent – predicate classifying each node as persistent (P, never released on access, survives reset()) or non-persistent (NP, released after its last use and by reset()). Defaults to all-NP.

inline void reset() noexcept

Resets all cached data.

inline size_t note_working_set(size_t current_bytes) noexcept

Fold the per-op live working set current_bytes into the running high-water mark and return the updated mark. Reported as hw= in the per-op eval trace; monotonically non-decreasing until reset().

inline size_t working_set_hwmark() const noexcept

Current running high-water mark (bytes) of the live working set.

inline ResultPtr access(key_type const &key) noexcept

Access cached data.

Parameters:

key – The key that identifies the cached data.

Returns:

ResultPtr to Result

inline ResultPtr store(key_type const &key, ResultPtr data) noexcept
Parameters:
  • key – The key to identify the cached data.

  • data – The data to be cached.

Returns:

Pointer to the stored data. Implictly accesses the stored data, hence, decays the lifetime if the key accesses a decaying cache entry. Passing key that was not present during construction of this CacheManager object, stores nothing, but still returns a valid pointer to data.

inline bool exists(key_type const &key) const noexcept

Check if the key exists in the database: does not check if cache exists.

inline int life(key_type const &key) const noexcept

if the key exists in the database, return the current lifetime count of the cached data otherwise return -1

inline int max_life(key_type const &key) const noexcept

if the key exists in the database, return the maximum lifetime count of the cached data that implies the maximum number of accesses allowed for this key before the cache is released. This value was set by the c’tor.

inline bool alive(key_type const &key) const noexcept
Returns:

true iff the key is registered for caching and currently holds stored data (i.e. has been stored and not yet drained by its final access).

inline bool persistent(key_type const &key) const noexcept
Returns:

true iff the key is registered for caching and classified persistent (P: never released on access, survives reset()).

inline size_t entry_size_in_bytes(key_type const &key) const noexcept
Returns:

size in bytes of the data currently held for key, or 0 if the key is not registered or no data is currently stored.

inline size_t alive_count() const noexcept
Returns:

The number of entries with life_count greater than zero.

inline size_t size_in_bytes() const noexcept
Returns:

Returns the sum of Result::size_in_bytes of alive entries.

Public Static Functions

static inline CacheManager empty() noexcept

Get an empty cache manager.

Friends

friend struct access_by
template<typename T>
struct access_by
struct all_non_persistent

Default persistence classifier: every entry is non-persistent (NP).

Public Functions

inline bool operator()(key_type const&) const noexcept