Repository navigation
Commit ce97d58
authored
perf(scope-analyzer): cut the built-in analyser's time and memory by a third to two thirds (#21808)
* perf(scope-analyzer): cut the built-in analyser's time and memory by a third to two thirds
Profiling one analysis of typescript.js put the garbage collector at 23% of the
run, and a heap-snapshot diff put 17.5 MB of the 32 MB it retained in array
backing stores — 109,462 of them the identical 152 bytes, because V8 grows an
empty array on its first `push` to a capacity of 17 whatever it ends up
holding. Five changes, each measured on its own:
- A binding collects its references only where a caller reads them back — the
module scope and its direct children, which is all `getAllReferences` asks
for. Anything deeper resolves as before but keeps no list.
- `_reference` no longer allocates a `Reference` per identifier occurrence, of
which `_resolve` dropped ~81%. It records the identifier and its scope in
parallel arrays and builds the object only where one survives.
- `_pattern` takes a mode integer rather than a callback, so binding a
parameter or a declarator allocates no closure. The walk's `switch` also
gains a case for each type that made up ~98% of its key-table fallback — a
`CallExpression` alone is 10% of all visits.
- A scope with eight bindings or fewer scans them instead of indexing them in
a `Map`; 96% of the scopes that bind anything stay below that.
- Each list is seeded with its first element instead of being pushed into
empty, and a block or switch that declares nothing opens no scope at all.
Retained heap after one analysis, and analysis time (min of 11):
| fixture | heap | time |
| --------------- | ------------: | ------------: |
| lodash.js | -65% (0.8 MB) | -57% (3.3 ms) |
| three.module.js | -61% (3.5 MB) | -32% (28 ms) |
| three.webgpu.js | -59% (5.6 MB) | -33% (49 ms) |
| typescript.js | -67% (19 MB) | -39% (220 ms) |
Over five analyses of typescript.js the young generation is collected 6 times
rather than 45, dropping the collector from 23% of the run to under 10%.
`Scope.set` was an index nobody outside the analyser read, so it gives way to
`Scope.getBinding(name)`; `Scope.type` carries its union again rather than the
widened `string` the generated declarations had picked up.
* test(scope-analyzer): cover the branches the new fast paths added
Three of them the unit suite never reached: promoting a scope past the index
threshold and adding to the index it built, a binding that collects a second
declaring identifier, and the alternate arm of an if statement or ternary.
Unit-only line coverage of `ScopeAnalyzer.js` goes from 93.7% to 95.9%; what
is left is code this change only moved, which the integration suite drives.
* docs(scope-analyzer): trim the comments this change added to two lines
`_pattern`, `analyzeScope` and `_recorded` each grew a prose paragraph past
what AGENTS.md allows in `lib/`. The non-obvious part of each is kept.
* test(scope-analyzer): reach the arms the walk had never exercised
Moving `_pattern`'s body out of a closure made codecov count its arms as new
lines, and the report showed three regions no suite reaches: the targets a
parser may report as expressions rather than patterns, the non-computed key an
unknown node carries, and a compound assignment or update against something
other than a plain identifier.
acorn emits a pattern for every destructuring target, so the first group is
reachable only by handing the walker the tree a parser that does not would
produce — the file already mutates node types for that.
`ScopeAnalyzer.js` now has full line coverage from the unit suite alone.
* docs(scope-analyzer): trim the parser-shape note to two lines1 parent 578b86b commit ce97d58
4 files changed
Lines changed: 747 additions & 256 deletions
File tree
- .changeset
- lib/javascript
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
0 commit comments