perf(strict-void-return): reuse function-body walk visitor closure#1033
Merged
Conversation
|
FWIW I also have microsoft/typescript-go#4395 which will make |
Member
Author
Thanks for calling that out! Makes sense to me, if we think that's likely to land upstream then I'm fine closing this PR and the other I just opened, since they'll be unnecessary once that change is in typescript-go. |
|
I think your PRs are fine; we have a lot of code in TS that does this same thing. |
The recursive `visit` that walks a function body declared its ForEachChild callback inline, heap-allocating a fresh closure on every recursive call (once per node in the body). Hoist it to a single reused `visitChild` closure to remove that per-node allocation, reducing GC pressure on large files. Same pattern as the no-unnecessary-type-parameters fix. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
5f93a77 to
47d635e
Compare
camc314
requested changes
Jul 1, 2026
Signed-off-by: Cameron <cameron.clark@hey.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
strict-void-returnwalks each function body (visit) looking for non-voidreturnstatements. TheForEachChildcallback was declared inline insidethe recursive
visit, so Go heap-allocated a fresh closure on everyrecursive call — i.e. once per node in the body.
Hoisting that callback to a single
visitChildclosure (allocated once andreused for the whole walk) removes the per-node allocation. Behavior is
unchanged — the callback body is identical, it's just no longer re-created at
every node.
Benchmark
Synthetic workload: 3,000 void-context callbacks (
arr.forEach((x) => { … })),each with ~20 statements of nested expressions plus a non-void
return(so therule does not early-out and actually walks the body). Allocations measured with
Go's heap profiler at
-memprofilerate=1(every allocation sampled, so thecounts are exact). The generated source is byte-identical before and after, so
the entire delta is attributable to this change.
The per-node closure was being allocated for every node in every body walk
(~3.9M closures across the workload), all of which are eliminated. Each closure
is tiny, so the byte reduction is smaller (~11%), but GC cost is driven largely
by object count — cutting 88% of allocations is a direct reduction in GC
pressure on large/closure-heavy files.
Notes
number of diagnostics.
ForEachChild/ForEachChildAndJSDoccallback inside arecursive walk) exists in a couple of other rules; this PR covers
strict-void-returnonly, which walks whole function bodies and so benefitsthe most.
🤖 Generated with Claude Code