Repository navigation
Share one scope visitor across parses to stop leaking memory - #109
Conversation
parseFile created a Visitor from rolldown/utils for every parse, along
with about 25 closures (its callbacks and the scope helpers) over the
state of that parse. Visitor keeps the { enter, exit } objects of every
instance in a module-level cache that is never cleared
(oxc-project/oxc#25510), so the callbacks of each parse stayed alive for
the rest of the process, with the function bodies, scopes, and templates
they reached.
The visitor, its callbacks, and the scope helpers now live at module
level, and visitProgram resets the module-level state of the visit
before each one. A visit is synchronous, so visits cannot interleave.
functionBodies and firstSwitchCases test membership with delete(), so
they empty out as their nodes are visited. extractCssTagCandidates is
inlined into parseFile.
Also:
- hashText uses the one-shot crypto.hash(), stable since Node 24.13.1:
same output, about 2.3x faster on these short strings.
- unicorn/consistent-function-scoping is enabled through the suspicious
category, resolving its TODO. It has no findings.
- AGENTS.md says not to create a Visitor per file.
Replaying the transforms of react-data-grid's 15 ecij modules 2,000
times, with buildEnd between rounds, the heap grew from 109 to 1,115 MB
before and stays at about 8 MB now, and each file takes about 264 us
instead of 277 us.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
## Summary
`parseFile` created a `Visitor` from `rolldown/utils` for every parse,
along with about 25 closures (its callbacks and the scope helpers) over
the state of that parse. Besides allocating and compiling them every
time, this leaked: `Visitor` keeps the `{ enter, exit }` objects of
every instance in a module-level cache that is never cleared
([oxc-project/oxc#25510](oxc-project/oxc#25510),
whose fix has been waiting for review since August). So the callbacks of
each parse stayed alive for the rest of the process, along with the
function bodies, scopes, and templates they reached.
- The visitor, its callbacks, and the scope helpers (`pushScope`,
`popScope`, `findFunctionScope`, `recordBindingPattern`, `recordParams`,
and `addCssTagCandidate`) now live at module level. The state of a visit
is held in module-level variables, which `visitProgram(program,
rootScope)` resets before each visit. A visit is synchronous, so visits
cannot interleave.
- `functionBodies` and `firstSwitchCases` test membership with
`delete()` instead of `has()`, so they empty out as their nodes are
visited instead of keeping the nodes of every file.
- `extractCssTagCandidates`, called once per parse, is inlined into
`parseFile`.
- `hashText` uses the one-shot `crypto.hash()`, stable since Node
24.13.1, instead of `createHash()`: same output, about 2.3× faster on
these short strings.
- `unicorn/consistent-function-scoping` is enabled: the override and its
`TODO: enable` are gone, and the `suspicious` category turns the rule
on. It has no findings, and it flags nested functions that capture
nothing from their parent scope.
- AGENTS.md says not to create a `Visitor` per file, as no test would
catch the leak.
Most of the diff moves code. `git diff --color-moved=dimmed-zebra
--color-moved-ws=allow-indentation-change` shows only the edits.
Left as is:
- `resolveValue` and `resolveBinding`. They close over the state of one
transform (`context`, `filePath`, `constValues`, and
`stylesheetDependencies`), so moving them out would mean passing a state
object through every call, to save about two small allocations per
transform.
- `isWaitingFor`, `parseFile`, and `extractCssFromCode`, created once
per plugin instance.
- Option literals like `{ encoding: 'utf8' }` and `{ hires: 'boundary'
}`, warning objects, and the `?? []` fallbacks in `watchChange` and
`hotUpdate`. Each is one short-lived object next to a file read or
sourcemap generation, or on a path that rarely runs.
## Test plan
- `node --run test` (locally and with `CI=true` against `dist/`),
`lint`, `typecheck`, `format:check`, `build`, and `lint:package` pass,
with no snapshot changes. `src/index.ts` coverage is unchanged: the
`TSParameterProperty` branch is still the only uncovered statement.
- Replaying the transforms of react-data-grid's 15 ecij modules 2,000
times with a stub plugin context, calling `buildEnd` between rounds: the
heap grew from 109 to 1,115 MB before, and stays at about 8 MB now. Each
file takes about 264 µs instead of 277 µs.
- oxlint flags a nested function that captures nothing now that
`unicorn/consistent-function-scoping` is enabled.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 5.5 <[email protected]>
## Summary When CodeRabbit reviews a PR, it appends a "Summary by CodeRabbit" block to the PR description by default. Squash merges use the description as the commit message, so #109's summary landed in `main` until its message was rewritten. The new `.coderabbit.yaml` sets `reviews.high_level_summary_in_walkthrough: true`, which [places the summary at the top of the walkthrough comment instead of the description](https://docs.coderabbit.ai/pr-reviews/summaries#moving-the-summary-to-the-walkthrough). The rest keeps CodeRabbit's defaults. A repository file takes precedence over settings made in CodeRabbit's UI, but #109's review reported "Configuration used: defaults", so there were none to lose. The first line points editors to CodeRabbit's schema for validation and completion. ## Test plan - CodeRabbit uses the `.coderabbit.yaml` of the branch under review: its comment here reports "Configuration used: Repository: nstepien/ecij/.coderabbit.yaml". Once a review is triggered, it should put the summary in its walkthrough comment and leave this description untouched - `node --run format:check` (which now includes this file), `lint`, `typecheck`, `build`, `lint:package`, and `test` pass 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
main reshaped the code this branch builds on since the last merge, so the merge ports the branch onto main's design again: - The scope visitor is shared across parses (#109), so the callbacks for raw TypeScript bindings and `export default` join it, and the default export is recorded through its visit state. - Imported modules are resolved, loaded, and parsed once per transform through main's loadImportedModule (#124), which now also skips external modules, keys modules by moduleKey, and records dependencies by file. - Modules are parsed as the language the bundler assigned them through main's parseLangOf (#125), which this branch had too. - extractCssFromCode keeps main's single pass (#114), and hotUpdate its early return, with this branch's handling of query variants. - readModuleCode moved to module level, as consistent-function-scoping is on since #109. Tests: the inline snapshots of this branch's tests follow Rolldown's new module comments (`// path` instead of `//#region path` and `//#endregion`), with no other change. The dev server test of virtual modules uses main's shared getCode and getStylesheets helpers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
parseFilecreated aVisitorfromrolldown/utilsfor every parse, along with about 25 closures (its callbacks and the scope helpers) over the state of that parse. Besides allocating and compiling them every time, this leaked:Visitorkeeps the{ enter, exit }objects of every instance in a module-level cache that is never cleared (oxc-project/oxc#25510, whose fix has been waiting for review since August). So the callbacks of each parse stayed alive for the rest of the process, along with the function bodies, scopes, and templates they reached.pushScope,popScope,findFunctionScope,recordBindingPattern,recordParams, andaddCssTagCandidate) now live at module level. The state of a visit is held in module-level variables, whichvisitProgram(program, rootScope)resets before each visit. A visit is synchronous, so visits cannot interleave.functionBodiesandfirstSwitchCasestest membership withdelete()instead ofhas(), so they empty out as their nodes are visited instead of keeping the nodes of every file.extractCssTagCandidates, called once per parse, is inlined intoparseFile.hashTextuses the one-shotcrypto.hash(), stable since Node 24.13.1, instead ofcreateHash(): same output, about 2.3× faster on these short strings.unicorn/consistent-function-scopingis enabled: the override and itsTODO: enableare gone, and thesuspiciouscategory turns the rule on. It has no findings, and it flags nested functions that capture nothing from their parent scope.Visitorper file, as no test would catch the leak.Most of the diff moves code.
git diff --color-moved=dimmed-zebra --color-moved-ws=allow-indentation-changeshows only the edits.Left as is:
resolveValueandresolveBinding. They close over the state of one transform (context,filePath,constValues, andstylesheetDependencies), so moving them out would mean passing a state object through every call, to save about two small allocations per transform.isWaitingFor,parseFile, andextractCssFromCode, created once per plugin instance.{ encoding: 'utf8' }and{ hires: 'boundary' }, warning objects, and the?? []fallbacks inwatchChangeandhotUpdate. Each is one short-lived object next to a file read or sourcemap generation, or on a path that rarely runs.Test plan
node --run test(locally and withCI=trueagainstdist/),lint,typecheck,format:check,build, andlint:packagepass, with no snapshot changes.src/index.tscoverage is unchanged: theTSParameterPropertybranch is still the only uncovered statement.buildEndbetween rounds: the heap grew from 109 to 1,115 MB before, and stays at about 8 MB now. Each file takes about 264 µs instead of 277 µs.unicorn/consistent-function-scopingis enabled.🤖 Generated with Claude Code
Summary by CodeRabbit