Skip to content

[MicroPerf] Inline the free-variable typar foldBacks - #20385

Open
T-Gro wants to merge 1 commit into
mainfrom
t-gro-freevars-inline-foldback
Open

[MicroPerf] Inline the free-variable typar foldBacks#20385
T-Gro wants to merge 1 commit into
mainfrom
t-gro-freevars-inline-foldback

Conversation

@T-Gro

@T-Gro T-Gro commented Aug 27, 2026

Copy link
Copy Markdown
Member

Stacked on #20384 — depends on its ListInline module; the base auto-retargets to main when #20384 merges.

Free-variable computation is one of the hottest compiler traversals. accFreeInTyparConstraints and accFreeInTypars fold the top-level accFreeInTyparConstraint / accFreeTyparRef over a typar's constraint and typar lists — usually tiny or empty. Passed to List.foldBack, each call allocated a partial-application closure; since foldBack builds no result list, that closure is the entire allocation. ListInline.foldBack inlines the folder (InlineIfLambda) and the top-level partial application fuses, so nothing is allocated.

Closures eliminated (dotnet-trace gc-verbose, 6 self-compiles of a 120-file corpus):

closure before after
accFreeInTyparConstraint opts (foldBack) 147.8 MB 0
accFreeTyparRef opts (foldBack) 238.8 MB 0

Fold order is unchanged, so free-variable results are identical; compiler output is byte-identical under --deterministic+ (SHA-256 match vs base), and tests pass.

@T-Gro T-Gro added the NO_RELEASE_NOTES Label for pull requests which signals, that user opted-out of providing release notes label Aug 27, 2026
@T-Gro
T-Gro requested a review from abonie August 27, 2026 13:07
@T-Gro
T-Gro marked this pull request as ready for review August 27, 2026 13:07
@T-Gro
T-Gro requested a review from a team as a code owner August 27, 2026 13:07
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Release notes required, but author opted out

Warning

Author opted out of release notes, check is disabled for this pull request.
cc @dotnet/fsharp-team-msft

@T-Gro
T-Gro force-pushed the t-gro-freevars-inline-foldback branch from 276248a to 198fdf4 Compare August 27, 2026 13:33
@github-actions github-actions Bot added the AI-Tooling-Check-Bypassed Tooling check: non-fork PR, not diff-analyzed label Aug 27, 2026
// Bound type vars form a recursively-referential set due to constraints, e.g. A: I<B>, B: I<A>
// So collect up free vars in all constraints first, then bind all variables
let acc =
List.foldBack (fun (tp: Typar) acc -> accFreeInTyparConstraints opts tp.Constraints acc) tps acc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, LLM flagged this as another good opportunity to apply the new version of foldBack

@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Sep 1, 2026
Base automatically changed from t-gro-foldhierarchy-loop-lift-spike to main September 4, 2026 13:13
accFreeInTyparConstraints and accFreeInTypars fold accFreeInTyparConstraint /
accFreeTyparRef over a typar's constraint and typar lists during
free-variable computation, one of the hottest traversals in the compiler.
Passing those top-level functions to List.foldBack allocated a partial-
application closure per call, over typically tiny (often empty) lists where
the closure is the entire cost.

Switch both folds to ListInline.foldBack (added in the parent change), whose
InlineIfLambda folder inlines the top-level partial application, so no closure
is allocated. Fold order is unchanged; compiler output is byte-identical under
--deterministic+.

Measured (dotnet-trace gc-verbose, 6 self-compiles of a 120-file corpus):
accFreeInTyparConstraints closure 147.8 MB -> 0, accFreeTyparRef closure
238.8 MB -> 0.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c6610fae-a96e-4421-a920-e73de4fc94dc
@abonie
abonie force-pushed the t-gro-freevars-inline-foldback branch from 198fdf4 to e4f972f Compare September 4, 2026 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-Tooling-Check-Bypassed Tooling check: non-fork PR, not diff-analyzed NO_RELEASE_NOTES Label for pull requests which signals, that user opted-out of providing release notes Theme-Performance

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants