pub struct ExpansionCache(/* private fields */);Expand description
Per-call-site slot holding the last macro expansion of a Form.
The tree-walking evaluator otherwise re-expands every macro call on every
evaluation (converting the forms to values, running the macro, converting
the result back), which dominated the cost of e.g. a when inside a hot
loop. Clojure itself expands each call site once, at compile time, so
reusing the expansion matches its semantics.
Clones of a list form share the slot (see Form::clone), so a function
body copied into many closures expands each call site once in total. The
shared cell is allocated lazily, on first store or first clone, so a form
that is parsed and evaluated once pays nothing beyond an empty word.
A form whose kind is mutated in place after cloning must not be
evaluated as a macro call afterwards, since it would share its source’s
expansion; no evaluator path does this (rewrites build fresh forms).
Implementations§
Source§impl ExpansionCache
impl ExpansionCache
Sourcepub fn get(&self, macro_id: u64, ns: &str) -> Option<Arc<Form>>
pub fn get(&self, macro_id: u64, ns: &str) -> Option<Arc<Form>>
The cached expansion, if one was made by macro_id in ns.