Skip to main content

Module memo

Module memo 

Source
Expand description

ContentMemo + the thread_local_content_memo! macro — a byte-neutral, content-keyed memo of a pure function (the extracted shape hand-rolled at the NAR-hash / referenced-idents / overlay-flatten memo sites). ContentMemo — a byte-neutral, content-keyed memo of a PURE function.

§The load-bearing invariant (why this is safe on a byte-parity path)

The memoized value is a pure function of its content-key, so a cache hit is byte-identical to a recompute. That is exactly what makes memoization safe on sui’s byte-parity-critical eval path: a memoized nix evaluation must produce the identical drvPath whether the value was freshly computed or served from the memo. The key IS the content address; the value is fully determined by it.

RISK — do not violate: never memoize a value that depends on anything other than its key — wall-clock (currentTime), environment (getEnv), mutable filesystem reads, or eval-order-sensitive identity. Those are not pure functions of the key, so a hit would NOT equal a recompute and could change a drvPath. If the value can vary for a fixed key, it is not a ContentMemo candidate.

§Why it’s a primitive

This exact shape — a thread-local Map<ContentKey, Rc<Value>> of a pure function, cleared at a natural boundary — was hand-rolled three times before this module (the sui-compat NAR-hash memo, the sui-eval referenced-idents memo, the sui-eval overlay-flatten cache). This is the extracted, packaged shape: declare one with thread_local_content_memo! and get a memoized accessor + a clear fn for free.

Single-threaded by construction (Rc + RefCell), matching sui’s single-threaded evaluator.

Structs§

ContentKey
A content address derived by construction from a value of type T.
ContentMemo
A single-threaded content-keyed memo. K is the content address; the value Rc<V> is a pure function of K (see the module invariant). A hit is a cheap Rc::clone.