![]() |
femtolisp: lazy function bodies and the caches of compiled code | ![]() |
A boot of TeXmacs defines about 9000 functions and calls about 1500 of them. Expanding and compiling the others is lost time, and it expands macros earlier than the code of TeXmacs expects (see the modules). So the body of a function is expanded and compiled when the function is first called.
A form of a loaded file is expanded by %expand-top, not by the expand of femtolisp. It expands the macros as expand does, but it does not enter a lambda: each lambda it reaches (whose free variables are therefore global) is replaced by a call which makes a stub,
(%make-lazy '(lambda (x) body…) 'name '(the module) "TM%progs%…") |
So the macros of the top level (tm-define, menu-bind, define-preferences…) still run when the file is loaded, and the tables they fill are ready; the bodies wait. The arguments are the lambda, the name of the function (or #f), the name of its module (#f for the user module) and the name of the cache of its file (#f without the caches).
A lambda inside a let at the top level does not wait. let expands to the call of a lambda: that lambda is made a stub, which is called at once, so all it contains is expanded and compiled when the file is loaded. This is the case of the overloaded definitions of tm-define, which are inside (let ((former …)) …): their bodies are compiled at the load, as before.
%expand-top follows the cases of expand in system.lsp (a bound global name hides a macro of the same name, curried definitions, let-syntax, quoted data). If the expander of femtolisp changes, %expand-top must follow.
A stub is a function made with the constructor function of femtolisp from three things:
the code of %lazy-template, shared by all the stubs: (lambda args (apply (%lazy-force! '%lazy-record) args)), where %make-lazy puts the record of the stub in the place of the constant %lazy-record;
its own constants: a record #(lambda module stub forced? file) and, last, (%source . lambda);
the name of the function.
The last constant is where procedure-source reads the source of a function, so procedure-source, procedure-name and procedure-arity are right for a function which was never called. (All this relies on *keep-source*, which
r5rs-femtolisp.scm
turns on: the compiler then keeps the source of each function as its last constant.)
%lazy-force! expands and compiles the lambda, with *current-module* bound to the module where it was loaded, then calls %function-become! (in
fl_core.c
): the stub takes the code, the constants, the environment and the name of the compiled function. The stub is now the compiled function: whatever kept it (a hook, a menu, the former definitions of tm-define, a table) calls the compiled code, and eq? still holds.
Two things make this safe, and must stay true:
After %lazy-force! returns, the stub only makes a tail call. The interpreter keeps a pointer into the code which runs, and the constants of the stub were just replaced. The code of the template after the call is loada0, tapply, which read neither. The code itself stays alive and in place: all the stubs share it, and femtolisp does not move code. Check it after a change of the compiler with (disassemble %lazy-template).
A function hashes as its source (patch 0022). femtolisp hashed a function by its code, which changes at the first call: the table of the names of the functions of tm-define, whose keys are functions, lost them. The compiled function is given the source of its stub (%with-source-of), the lambda before its expansion.
A macro which runs while a body is compiled may call the very function being compiled. The record has a flag for this: the inner call compiles the body and makes the stub become it; the outer compilation then finds the flag set, drops its own result and uses the stub.
Compiling is the larger part of the work of loading a file, so the compiled code is kept in $TEXMACS_HOME_PATH/system/cache/femtolisp/. For the source file whose name in the cache is N:
For each form, the fingerprint of its expansion and its compiled code. It starts with a key (the compiler, the version of TeXmacs, the format of the cache) and the private names of the module, which the compiled code depends on. %eval-forms-cached reads it in step with the forms: a form is still expanded, since the macros may have effects, and its compiled code is taken from the cache when the fingerprints agree. The file is written again when a form was compiled.
For each body compiled so far, the fingerprint of its expansion, together with its module and the private names of that module, and its compiled function. It is read at the first call of a function of the file (%lazy-file-table), and each compilation adds a line to it. It starts again (%lazy-file-changed!) at the first form of the file which can be cached and is not found in N.flc: the file changed, or it had no valid cache. The forms are compared in their order, so a form added to a file also compiles again those after it.
The name N is the path of the file with /, \, : and the spaces replaced by %; a file of TeXmacs is named by its path inside $TEXMACS_PATH (TM%progs%…), so that a cache made elsewhere fits. A distribution, or the page of the browser version, can ship the compiled code in $TEXMACS_PATH/cache/femtolisp/: its N.flc is read when the home has no valid one, and its N.lazy is read before the one of the home.
A fingerprint is 128 bits of hash of the structure of a value, as 32 hexadecimal digits (%fingerprint, in
fl_core.c
). It is #f when the value holds a closure, an uninterned symbol, a table or an object of TeXmacs, or is very large: the form is then compiled at each load. So is a form whose compiled code holds an uninterned symbol, a table, an object of TeXmacs or a closure with an environment (%cache-writable?); the cache of a file with such a form is written again at each load.
When to change the format. The key does not see a change of
boot-femtolisp.scm
. After a change which makes the same expansion compile to other code (the arguments of %make-lazy, the late calls…), increase %cache-format (it has two numbers, one with lazy bodies and one for TEXMACS_FL_EAGER): the old caches are then ignored and written again. A change of system.lsp or compiler.lsp changes the boot image, which is part of the key.
TEXMACS_FL_NO_CACHE=1 runs without the caches. They are worth 0.2 s at a boot, and 0.4 s at each start in the browser.