Repository navigation
Description
Parent: #493
Related: #928 (whose benchmark could not see this), #931 (where it surfaced)
Status: measured end to end, root cause not yet located. No fix proposed.
Found while confirming that the #931 fix did not introduce a quadratic. It did not — but the shape it was tested on already was one, in the binary from before that fix.
Evidence
Median of 5 runs of test262 --eval, which covers process start, parse and function instantiation. Each shape nests to the given depth; the inside-a-function shapes wrap everything in function __never(){ … }, which is never called, so nothing executes.
| shape | depth 1000 | depth 2000 | depth 4000 | growth |
|---|---|---|---|---|
destructuring catch ([p]), inside a function, with vars |
123.2 ms | 481.8 ms | 1865.3 ms | x3.91, x3.87 |
destructuring catch ([p]), inside a function, no vars |
122.8 ms | 475.9 ms | 1863.3 ms | x3.88, x3.92 |
plain catch (p), inside a function |
7.4 ms | 8.3 ms | 10.4 ms | linear |
destructuring catch ([p]), top level, with vars |
8.6 ms | 10.5 ms | 16.0 ms | linear |
| plain blocks with vars, inside a function | 14.8 ms | 36.0 ms | 145.8 ms | x2.44, x4.05 |
The same measurement on the destructuring-with-vars shape gives nearly identical numbers before and after #931 (1871 ms vs 1977 ms at depth 4000), so this predates that fix.
What the isolation rules out
- Not the vars: removing every
varchanges nothing (1865 vs 1863 ms). - Not catch semantics in general: plain
catch (p)is linear in the same position. - Not the parser's handling of this syntax as such: the identical nesting is linear at script top level.
- Not Parser: destructuring catch parameters allow a same-named var #931's new check: present before it, and the fix adds only a few percent.
What remains is the conjunction: destructuring catch patterns, inside a function body. Something that processes function bodies does per-level work proportional to the nested destructuring patterns below it. Plausible places, unverified: a per-function pass that re-scans nested catch patterns at each level (scanSuperAndArgs does visit catch_param via scanSuperAndArgsInPattern), function-body eligibility or scope analysis at instantiation, or binding collection for catch patterns.
A caveat this exposes about #928
The #928 frozen workloads — like #926's — parse top-level scripts. They measured and fixed a real quadratic there, and that result stands. But a benchmark built on top-level programs cannot see an effect that only occurs inside function bodies, and the last row above suggests one exists for plain nested blocks too, still steepening at depth 4000. So "#928 removed the nesting quadratic" should be read as scoped to top-level code until function-body shapes are measured.
Suggested order
- Add function-body variants to the frozen growth runner — each Frontend: private-name use validation copies every inherited name per nested class #926/Frontend: lexical/var early-error scan rebuilds the subtree var set at every nested block #928 shape wrapped in a function, plus the destructuring-catch shape here — so the effect is measured by the owned runner and not only by
--evalwall time. - Sample a depth-4000 run (
sample <pid> 3) to locate the hot frames before guessing. - Only then propose a fix.
Activity
glennmichael123 commentedon Sep 16, 2026
Fixed in 2688dc9
Root cause — located by sampling, not guessed
A depth-10000 run sampled with
sampleput essentially all the time in the bytecode compiler, not the parser:compileStmt(recursing through each try/catch) →compileCatchPattern→compileArrayPattern→emitLoad→resolvewith top-of-stack
compiler.Compiler.resolve(969 samples),hash.wyhash.Wyhash.hash(431) andcompiler.Compiler.withDepthToResolution(339). That explains the whole isolation table from the original report: an uncalled function's body is still compiled, while top-level code in these probes was not.The cost was in
FnScope.get:Lexical scopes are a stack inside one
FnScope, not separate entries in theparentchain, soresolve's outer loop runs once butgetsearches the whole stack. Activation temps are added tonamesbyaddLocal, so every temp lookup missed in all N catch scopes first — each a realwyhash, because a catch scope holds its parameter and is not empty. A destructuring pattern's iterator protocol performs many temp loads and stores, each resolved up to three times. A plaincatch (p)touches a temp once or twice, which is why its quadratic term was invisible at these depths.The fix
FnScopecounts how many open lexical scopes bind each name, andgetskips the probe when that count is zero — a name no open scope binds cannot be found there, so every result is unchanged. Counts rise before a binding is inserted and fall only on pop, so they can over-count but never under-count.I deliberately did not make temps bypass name resolution by slot. Temps go through the
withand direct-eval dynamic binding plans, and short-circuiting that would change semantics insidewithrather than just speed.Results — median of 5
--evalrunsGrowth for the fixed shape is now x1.51 and x1.65 per doubling (flattened by process start).
A correction to my own report above: I suggested the steepening of plain nested blocks inside a function might be the same loop. It is not — this fix leaves that shape unchanged, because Zig's hash map returns before hashing on an empty map, and plain block scopes are empty. It has a separate cause and is tracked in #933 item 2.
Verification
Because this changes name resolution for every compiled function, and ordinary test262 runs mostly use the tree-walker, the corpus was run with the bytecode path forced:
--vm-witness-subtree, ReleaseFast, 23 subtrees weighted towardwith, eval, identifier resolution and scoping — 13,814 tests, divergence sets identical to the pre-change binary in every subtree. The pre-existing divergences inexpressions/call(3),statements/try(3),statements/switch(3) andstatements/for(4) are byte-identical before and after.getre-probes each skip and asserts the name is really absent — exactly whatgetalways cost before, so Debug is no slower. With it live: compiler tests 313 passed, 0 failed in Debug, and a ReleaseSafe--vm-witness-subtreerun over 9 subtrees — 7,317 tests — exits cleanly with no panics.Found on the way — #934
Timing at depth 8000 crashed both the pre- and post-fix binaries with SIGSEGV. Characterizing it showed a general bug: 32 KB of nested parentheses segfaults the process, where node throws a catchable
RangeError: Maximum call stack size exceeded. Filed as #934 and placed right after this item in #933.bench(frontend): freeze function-body growth rows and re-pin the runner