Skip to main content

ExpansionCache

Struct ExpansionCache 

Source
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

Source

pub fn get(&self, macro_id: u64, ns: &str) -> Option<Arc<Form>>

The cached expansion, if one was made by macro_id in ns.

Source

pub fn store(&self, macro_id: u64, ns: Arc<str>, form: Form) -> Arc<Form> ⓘ

Record form as the expansion made by macro_id in ns, replacing any previous entry. Returns the stored Arc so the caller evaluates the very form the cache holds.

Source

pub fn clear(&self)

Drop any cached expansion (for every form sharing this slot).

Trait Implementations§

Source§

impl Debug for ExpansionCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ExpansionCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.