Title: | In-Memory Caching of Repeated Computations (Memoization) |
---|---|
Description: | A simple in-memory, LRU cache that can be wrapped around any function to memoize it. The cache is keyed on a hash of the input data (using 'digest') or on pointer equivalence. |
Authors: | Peter Meilstrup <[email protected]> |
Maintainer: | Peter Meilstrup <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.1 |
Built: | 2024-11-23 04:01:53 UTC |
Source: | https://github.com/crowding/memo |
The 'memo' package implements a cache that can be used to avoid repeated computations of functions. The cache lookup is based on object identity (i.e. pointer equivalence) which is suited for functions like accessors or other functions that are called repeatedly on the same object.
Peter Meilstrup
Report cache statistics.
cache_stats(fn)
cache_stats(fn)
fn |
A memoized function that was created by |
A list with labels "size", "used", "hits", "misses", "expired" counting the number of slots in the cache, the number of slots currently used, the number of times a previous result was recalled, a new result was recorded, and a result was dropped.
[hashmap()] constructs a hashmap, which is an object that behaves like an [environment] but can key on arbitrary objects rather than just characters.
hashmap() ## S3 method for class 'hashmap' x[...] ## S3 replacement method for class 'hashmap' x[...] <- value ## S3 method for class 'hashmap' x[[...]] ## S3 replacement method for class 'hashmap' x[[...]] <- value keys(x, ...) values(x, ...) to_pairs(x, ...) from_pairs(pairs) hasKey(x, ...) dropKey(x, ...)
hashmap() ## S3 method for class 'hashmap' x[...] ## S3 replacement method for class 'hashmap' x[...] <- value ## S3 method for class 'hashmap' x[[...]] ## S3 replacement method for class 'hashmap' x[[...]] <- value keys(x, ...) values(x, ...) to_pairs(x, ...) from_pairs(pairs) hasKey(x, ...) dropKey(x, ...)
x |
a hashmap object. |
... |
Any number of indices. |
value |
A replacement value for ‘[['; for ’[', a sequence of replacement values. |
pairs |
A list of pairs, the first element is treated as key and the second as value. |
You can use multiple indices in a hashmap; the effect is similar to indexing on a list containing all keys.
Type is significant; for instance, float '1' and integer '1L' are considered distinct indices. It is also permitted to index on NULL, NA, or the empty string.
The 'memo' package hashmap has a performance optimization over other implementations of this concept, in that the md5 digest is memoized on scalar and pointer values. That means that if you lookup using keys that are pointer-identical to previously seen keys, it will skip computing the digest a second time. Indexing using scalar values will also bypass the md5 hash.
'hashmap()' returns a newly constructed hashmap.
'pairs(x)' extracts from a hashmap a list of pairs, each pair being of the form 'list(key=, val=)'.
'hasKey(x)' returns TRUE if there is a key with the same digest as '...' that compares [identical()]
Peter Meilstrup
Memoize a function.
memo(fn, cache = lru_cache(5000), key = hybrid_key, ...)
memo(fn, cache = lru_cache(5000), key = hybrid_key, ...)
fn |
A function to wrap. It should be a pure function (i.e. it should not cause side effects, and should not depend on any variables that may change.) It should not be a nonstandard-evaluating function. All arguments will be forced by the wrapper. |
cache |
A cache to use. Defaults to a new instance of
|
key |
A hashing strategy. The default " |
... |
Further arguments passed on to key. |
'basic_cache' makes a cache that does not expire old entries. It should be used in situations where you know the number of things to remember is bounded.
Construct a cache with least-recently-used policy.
permanent_cache() lru_cache(size = 10000)
permanent_cache() lru_cache(size = 10000)
size |
The maximum number of results to keep. |
A function f(key, value) which takes a string in the first parameter and a lazily evaluated value in the second. 'f' will use the string key to retrieve a value from the cache, or return the matching item from the cache, or force the second argument and return that, remembering the result on future calls.
When the number of entries in the cache exceeds size
, the least
recently accessed entries are removed.
The function memo
accepts an argument 'key' which
specifies the keying strategy.
digest_key(fn, cache, digest = digest::digest) pointer_key(fn, cache) hybrid_key(fn, cache, digest = function(x) digest::digest(x, "md5"))
digest_key(fn, cache, digest = digest::digest) pointer_key(fn, cache) hybrid_key(fn, cache, digest = function(x) digest::digest(x, "md5"))
fn |
A function whose results should be cached. |
cache |
A cache object. |
digest |
A digest function to use. |
A memoized function.