![]() |
Persistent environments and memoization | ![]() |
This page describes
src/src/Style/Environment/
and
src/src/Style/Memorizer/
. The design summary in the experimental evaluator lists the classes; the emphasis here is on how they cooperate and on what is actually memoized.
Everything in the rewriter is keyed on identity.
environment.hpp
defines
inline int weak_hash (tree t) { return hash ((void*) t.rep); }
inline bool weak_equal (tree t1, tree t2) { return t1.rep == t2.rep; } |
(and the same for environment and std_environment). Two computations are considered the same if and only if they receive the very same tree object and the very same environment object. This makes lookups cheap and is sound as long as trees and environments are never modified in place, which is why the rewriter works on the clean copy of the document and only ever builds new environments.
Keys are integers: variable names are converted with make_tree_label, so that the same table serves for built-in and user-defined names.
An array of assoc_node (key, value) of fixed size, filled with raw_write (i, key, val). It is the representation of the bindings of one assign, one with or one macro application.
An open hash table of hash_node (key, value, start, next) whose capacity is a power of two; it supports multiple_insert, multiple_write, multiple_remove and resize.
A linked list of basic environments, searched from the head. Each node counts its lookup misses; when they exceed the combined size of the node and its successor, raw_read calls compress, which merges the successor into a new hash table and unlinks it. This modifies the node in place, but without changing the set of bindings it represents, so identity-based memoization is not affected.
The environment of the evaluator (
std_environment.cpp
). Fields: pure (true for the environment opened by a with), env (the local bindings), next (the enclosing environment), accel (a list environment with all bindings visible here, used for lookups) and args (macro argument frames, only used by the classical macro expansion). Reads go through accel.
The evaluator never writes into an environment. It replaces the current environment (the global std_env) by the result of one of the following operations:
Builds a fresh top-level environment from a hash map of variables (each value is copied). Used once per environment change by the editor.
Adds the bindings local in front of the accel list. If the current environment is pure (we are directly inside a with), the result is a new impure environment on top of it; otherwise the local bindings are prepended to those of the current impure environment.
Pushes a new pure environment whose bindings are local.
Leaves a with. If no assignment happened inside it, the environment below the pure one is restored. Otherwise the assignments made inside the with survive: end_with_environment flattens them, removes the variables bound by the with itself, and re-applies the remaining patch to the enclosing environment with assign_environment.
Push and pop macro argument frames. They are only compiled with CLASSICAL_MACRO_EXPANSION, which is commented out in
environment.hpp
in favour of ALTERNATIVE_MACRO_EXPANSION (expansion by substitution).
Each operation is wrapped in a memorizer (assign_memorizer_rep, begin_with_memorizer_rep, end_with_memorizer_rep, …) with the pattern
void
assign (environment& env, assoc_environment local) {
memorizer mem= tm_new<assign_memorizer_rep> (env, local);
if (!is_memorized (mem)) mem->compute ();
env= mem->get_environment ();
} |
The memorizers of assign and begin_with are keyed on the pointers of both arguments. As explained in the pitfalls of this chapter, the evaluator creates a new assoc_environment for every evaluation of an assign or a with, so these memorizers never find an earlier result; only end_with, keyed on the environment alone, can.
Style/Memorizer/memorizer.hpp
defines the abstract memorizer_rep with the virtual methods type, hash, equal, print, compute, the accessors get_tree/set_tree and get_environment/set_environment, and get_children/set_children (implemented by compound_memorizer_rep, which stores an array of child memorizers). The types are numbered: MEMORIZE_EVALUATE (0), MEMORIZE_REWRITE (1), MEMORIZE_INACTIVE (2) for tree computations, and MEMORIZE_ASSIGN (10) to MEMORIZE_MACRO_UP (15) for environment operations.
The tree memorizers are compound: for instance evaluate_memorizer_rep (
Style/Evaluate/evaluate_main.cpp
) stores the input environment and tree, the output environment and tree, and the memorizers of all sub-evaluations, so that the result of an evaluation is a tree of memorizers mirroring the computation.
The handle class memorizer is not an ordinary reference counted pointer. Its constructor from a memorizer_rep* (
memorizer.cpp:288
) first looks the new object up in a global hash table (bigmem_insert: buckets indexed by hash () & mask, compared with type and equal, the number of buckets doubling as the table grows). If an equal memorizer already exists, the new object is deleted and the existing one is used. The destructor and the assignment operator remove a memorizer from the table (bigmem_remove) when its reference count drops to zero.
The constructor also records the memorizer on a stack, which is how the children of a computation are collected without passing them around:
Allocates the stack (and prints "Memorize initialize").
Opens a new level whose first free slot is the current position.
Writes the memorizer into the next slot of the current level, taking one reference for the handle and one for the slot (unless the slot already held the same memorizer). If the slot held another memorizer, that one loses its slot reference.
Closes the level and gives the memorizers created in it to the memorizer just below the level, as its children (set_children).
Returns the memorizer in slot 0, the top-level computation, and frees the stack.
evaluate (environment env, tree t) brackets the whole computation with memorize_initialize and memorize_finalize; each recursive evaluate (tree) creates its memorizer in the current level and then brackets its own sub-computations with memorize_start and memorize_end.
Whether a memorizer is “already computed” is decided by its reference count:
inline friend bool is_memorized (const memorizer& mem) {
return mem.rep->ref_count >= 3; } |
A memorizer just created holds two references (stack slot and handle). If the hash-consing found an existing object, that object is also referenced from somewhere else, typically from the children array of the previous evaluation, which the editor keeps alive in its field mem; its count is then at least three. The test is therefore a heuristic for “this computation is part of a result still in use”, and it relies on the previous result being kept until the new one has been built. When it succeeds, evaluate returns the stored tree and restores the stored output environment without descending into the subtree.