From 198fdf4bea2eaaacc9ce7d4618d5ac9d48572d52 Mon Sep 17 00:00:00 2001 From: perf-bundle Date: Thu, 27 Aug 2026 14:52:03 +0200 Subject: [PATCH] Use ListInline.foldBack in free-variable typar folds 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 --- src/Compiler/TypedTree/TypedTreeOps.FreeVars.fs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compiler/TypedTree/TypedTreeOps.FreeVars.fs b/src/Compiler/TypedTree/TypedTreeOps.FreeVars.fs index 975ff6cfb3a..e1c2d8e418b 100644 --- a/src/Compiler/TypedTree/TypedTreeOps.FreeVars.fs +++ b/src/Compiler/TypedTree/TypedTreeOps.FreeVars.fs @@ -260,7 +260,7 @@ module internal FreeTypeVars = acc and accFreeInTyparConstraints opts cxs acc = - List.foldBack (accFreeInTyparConstraint opts) cxs acc + ListInline.foldBack (accFreeInTyparConstraint opts) cxs acc and accFreeInTyparConstraint opts tpc acc = match tpc with @@ -375,7 +375,7 @@ module internal FreeTypeVars = accFreeInTyparConstraints opts v emptyFreeTyvars let accFreeInTypars opts tps acc = - List.foldBack (accFreeTyparRef opts) tps acc + ListInline.foldBack (accFreeTyparRef opts) tps acc let rec addFreeInModuleTy (mtyp: ModuleOrNamespaceType) acc = QueueList.foldBack