Skip to content

Import: Don't walk non-F# assemblies when labelling trait constraint sources - #20090

Open
auduchinok wants to merge 1 commit into
dotnet:mainfrom
auduchinok:skip-constraint-sources-for-il-assemblies
Open

Import: Don't walk non-F# assemblies when labelling trait constraint sources#20090
auduchinok wants to merge 1 commit into
dotnet:mainfrom
auduchinok:skip-constraint-sources-for-il-assemblies

Conversation

@auduchinok

@auduchinok auduchinok commented Jul 28, 2026

Copy link
Copy Markdown
Member

Prevents eager reading of all types in referenced non-F# assemblies, saving memory on projects with many references.

AI-generated summary:

addConstraintSources (added in #16304, so that a failed member constraint names the member it came from) is applied to every imported assembly, and recurses through e.ModuleOrNamespaceType for every module and namespace entity it finds.

For an assembly imported from IL there is nothing to find: the walk only reads AllValsAndMembers, and ImportILTypeDefs gives every namespace and type entity an empty val list; only an F# trait constraint produces a TyparConstraint.MayResolveMember to label in the first place. Meanwhile the recursion forces each namespace entity's ModuleOrNamespaceType, which imports that namespace - so referencing an assembly ends up importing every namespace in it, and reading every type definition, whether or not the code touches it.

Skip the CCUs that aren't F#. FSharp.Core and F# references are still walked, so the error messages are unchanged.

Measured with FSharpChecker.ParseAndCheckProject, keeping the results alive so the imported assembly structures stay on the heap (averages of 3 runs, one per process): a 486-reference F# project retains 1319.2 -> 952.1 MB (-27.8%), and a 168-reference console project 77.7 -> 69.8 MB (-10.2%). Checking FSharp.Compiler.Service itself (124 references, 397 sources) goes 2301.7 -> 2298.0 MB, i.e. within the noise at that size - what the imports cost there is dwarfed by the trees of the project's own code.

`addConstraintSources` (added in dotnet#16304, so that a failed member constraint
names the member it came from) is applied to every imported assembly, and
recurses through `e.ModuleOrNamespaceType` for every module and namespace
entity it finds.

For an assembly imported from IL there is nothing to find: the walk only
reads `AllValsAndMembers`, and `ImportILTypeDefs` gives every namespace and
type entity an empty val list; only an F# trait constraint produces a
`TyparConstraint.MayResolveMember` to label in the first place. Meanwhile the
recursion forces each namespace entity's `ModuleOrNamespaceType`, which
imports that namespace - so referencing an assembly ends up importing every
namespace in it, and reading every type definition, whether or not the code
touches it.

Skip the CCUs that aren't F#. FSharp.Core and F# references are still walked,
so the error messages are unchanged.

Measured with FSharpChecker.ParseAndCheckProject, keeping the results alive so
the imported assembly structures stay on the heap (averages of 3 runs, one per
process): a 486-reference F# project retains 1319.2 -> 952.1 MB (-27.8%), and
a 168-reference console project 77.7 -> 69.8 MB (-10.2%). Checking
FSharp.Compiler.Service itself (124 references, 397 sources) goes 2301.7 ->
2298.0 MB, i.e. within the noise at that size - what the imports cost there is
dwarfed by the trees of the project's own code.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

@T-Gro T-Gro left a comment

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.

🤖 AI review (@expert-reviewer): no significant issues found. Please verify independently.

Reviewed the change to addConstraintSources in CompilerImports.fs. The fix is correct and well-scoped:

  • The guard ia.FSharpViewOfMetadata.IsFSharp is cheap — IsFSharp is a plain bool field on CcuData (TypedTree.fs:5854), so reading it does not force Deref to materialize the namespace tree, which is the whole point of the optimization.
  • The inner addConstraintSources is a non-recursive let that shadows and wraps the outer function; addConstraintSources ia in its body correctly refers to the outer definition. This is subtle but works as intended.
  • The guard is semantically sound: only F# assemblies produce TyparConstraint.MayResolveMember constraints to label, so skipping IL-only assemblies cannot change diagnostic output. FSharp.Core and F# references (IsFSharp = true) are still walked, so error messages are unchanged.

No correctness, security, or error-handling concerns.

@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Aug 3, 2026
@T-Gro T-Gro added the AI-reviewed PR reviewed by AI review council label Aug 3, 2026
@T-Gro
T-Gro self-requested a review August 3, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-reviewed PR reviewed by AI review council AI-Tooling-Check-Scanned-Clean Tooling check: diff analyzed, no interesting infrastructure files

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants