From ad6b87c60da5b7b8422ad0cb4e828897d5eb8090 Mon Sep 17 00:00:00 2001 From: tylerjenningsw Date: Sun, 25 Oct 2026 14:21:26 -0400 Subject: [PATCH] Expressions: the kept layers are bounded like the runtime's scripts --- crates/expr/src/prelude.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/crates/expr/src/prelude.js b/crates/expr/src/prelude.js index 5671d3ec2..2fdcba110 100644 --- a/crates/expr/src/prelude.js +++ b/crates/expr/src/prelude.js @@ -31,6 +31,7 @@ function __begin(t, v, c, l, path, uid, idx, fd, ti, tt, sv) { __seed = 0; __timeless = false; __projectInfo = null; + __layersTrim(); } function __finish(r) { @@ -563,13 +564,25 @@ function Layer(c, l) { // Layer `l` of comp `c`, made once per runtime: a callable Layer is dearer to make than a plain // object, so it is kept, and its info (`__info`) is read again in each evaluation. -// The kept layers are out of an expression's reach. -var __layer = (function () { - var layers = {}; - return function (c, l) { +// The kept layers are out of an expression's reach, and bounded like the runtime's scripts: once +// `max` are kept, the layers after them aren't, and the next evaluation starts over +// (`__layersTrim`, so a layer stays the same object for a whole evaluation). +var __layer, __layersTrim; +(function () { + var layers = {}, kept = 0, max = 4096; + __layer = function (c, l) { // By comp, then layer: a `c + ':' + l` key costs more to build than the lookup saves. - var m = layers[c] || (layers[c] = {}); - return m[l] || (m[l] = new Layer(c, l)); + var m = layers[c], f = m && m[l]; + if (f) return f; + f = new Layer(c, l); + if (kept < max) { + (m || (layers[c] = {}))[l] = f; + kept++; + } + return f; + }; + __layersTrim = function () { + if (kept >= max) { layers = {}; kept = 0; } }; })();