Logo GenPRES

Memoization Module

Wraps a function in a thread-safe cache, so a repeated call with an equal argument returns the stored result.

Nested modules

Modules Description

Tests

Holds the Unquote tests of the memoize function.

Functions and values

Function or value Description

memoize f

Full Usage: memoize f

Parameters:
    f : 'a -> 'b - The function to memoize

Returns: 'a -> 'b
Type parameters: 'a, 'b

Memoize a function f according to its parameter x

- the memoization is based on a thread-safe ConcurrentDictionary - the cache is not cleared - safe under concurrent access: the Lazy wrapper guarantees that f x is evaluated AT MOST ONCE per distinct x, even when many threads request the same uncached key simultaneously. (A bare GetOrAdd(x, f) can run the factory on several threads before one wins; wrapping the value in Lazy means only the stored instance is ever forced, so a cold-cache stampede cannot re-run an expensive loader.) - the key constraint is equality (was comparison for the old Map-based version); every comparable key is also an equality key, so all existing call sites remain valid. - null keys are supported (the old F# Map tolerated them but a ConcurrentDictionary throws on a null key), cached in a dedicated cell so the null-key result is also computed at most once.

f : 'a -> 'b

The function to memoize

Returns: 'a -> 'b

memoize2Int f

Full Usage: memoize2Int f

Parameters:
    f : 'a -> 'b -> 'c

Returns: 'a -> 'b -> 'c
Type parameters: 'a, 'b, 'c (requires equality and equality)

Thread-safe memoization of a two-argument function, keyed on the pair of argument hash codes.

- thread-safe (ConcurrentDictionary + Lazy: f runs at most once per key) - the key is a value tuple of hash codes, so it can never be null - hash collisions share a slot (the first pair to hash to a given key wins) — unchanged from the original implementation

f : 'a -> 'b -> 'c
Returns: 'a -> 'b -> 'c

memoizeBounded maxCount f

Full Usage: memoizeBounded maxCount f

Parameters:
    maxCount : int
    f : 'a -> 'b

Returns: 'a -> 'b
Type parameters: 'a, 'b (requires equality)

Memoizes f for at most maxCount distinct arguments, compared by structural equality. A new argument that finds maxCount entries first removes the entries whose value is built; the entries still being built stay, so that concurrent calls with one argument run f once, and the cache can hold more than maxCount entries while they are built. When f raises an exception, the entry is removed so that the next call runs f again. The argument must not be null.

maxCount : int
f : 'a -> 'b
Returns: 'a -> 'b

memoizeOne f

Full Usage: memoizeOne f

Parameters:
    f : 'a -> 'b

Returns: 'a -> 'b
Type parameters: 'a, 'b

Thread-safe single-argument memoization. Behaves exactly like memoize (ConcurrentDictionary + Lazy: f runs at most once per key, null keys supported); kept as a separate name for source compatibility.

f : 'a -> 'b
Returns: 'a -> 'b

Type something to start searching.