folly::WeightedEvictingCacheMap

A variant of EvictingCacheMap that tracks weights for entries and evicts entries in LRU order to ensure the total weight of all entries stays below some set maximum. Weights are stored as a size_t with each entry.

Synopsis

Declared in <folly/container/WeightedEvictingCacheMap.h>

template<
    class _TKey,
    class _TValue,
    class _THash = HeterogeneousAccessHash<TKey>,
    class _TKeyEqual = HeterogeneousAccessEqualTo<TKey>>
class WeightedEvictingCacheMap;

Description

Example usage: if TKey is std::string and TValue is some large, complex object type, the weight could be the estimated memory size of the key and complex object. Tracking the weight explicitly minimizes costly recomputation of the estimated memory size. Thus, the total weight of all entries approximates the total memory usage.

TValue must be either movable or copyable. TKey must be copyable.

IMPORTANT NOTES: * Returned references, pointers, or iterators are potentially invalid after any pruning operation (set, insert, etc. that might increase total weight), or any set/insert on the same key (which are allowed to create a new entry or modify an existing entry becoming obsolete). * This is NOT a thread-safe structure. * For simplicity, functions taking a key implicitly inherit type constraints from EvictingCacheMap. (Must either match TKey or EligibleForHeterogeneousFind/Insert.)

This implementation has not been highly optimized.

Types

NameDescription
ValueAndWeight A stored value together with its tracked weight.

Type Aliases

NameDescription
PruneHookCall Callback type invoked on eviction with the key, value, and weight.
const_iterator Const iterator over key and ValueAndWeight pairs.
const_reverse_iterator Const reverse iterator over key and ValueAndWeight pairs.
iterator Mutable iterator that dereferences to a key and ValueAndWeight pair.
reverse_iterator Reverse mutable iterator over key and ValueAndWeight pairs.

Member Functions

NameDescription
WeightedEvictingCacheMap [constructor]Constructors
operator= Assignment operators
begin begin overloads
cbegin Returns a const iterator to the most recently used entry.
cend Returns a const iterator past the least recently used entry.
clear Clear the cache to an empty state.
crbegin Returns a const reverse iterator to the least recently used entry.
crend Returns a const reverse iterator past the most recently used entry.
empty Typical empty function
end end overloads
erase Erases any entry with given key or iterator.
exists Check for existence of a specific key in the map. This operation has no effect on LRU order.
find Get the iterator associated with a specific key. This function always promotes a found value to the head of the LRU. Although values can be modified through iterators, weights are const. See updateWeight().
findWithoutPromotion findWithoutPromotion overloads
get Get the value associated with a specific key. This function always promotes a found value to the head of the LRU. The TValue can be modified in place through the reference, keeping in mind the reference can easily be invalidated (IMPORTANT NOTES above).
getCurrentTotalWeight Returns total weight of all entries currently in the cache.
getMaxTotalWeight Returns the maximum allowed total weight of all entries in the cache.
getWithoutPromotion getWithoutPromotion overloads
rbegin rbegin overloads
rend rend overloads
set set overloads
setMaxTotalWeight Sets the maximum allowed total weight of all entries in the cache, evicting entries as needed for the new limit.
setPruneHook Set the prune hook, which is the function invoked on the key and value on each eviction. An operation will throw if the pruneHook throws. Note that this prune hook is not automatically called on entries explicitly erase()ed nor on remaining entries at destruction time.
size Get the number of elements in the dictionary
updateWeight updateWeight overloads

Static Data Members

NameDescription
kApproximateEntryMemUsage Approximate memory used by each cache entry.