Repository navigation
Share Roslyn metadata references across AssemblyGenerator compilations (#1086) - #1089
Merged
Merged
Conversation
#1086) MetadataReference.CreateFromFile() copies the whole PE image into native memory that is only released on finalization. AssemblyGenerator.Compile() created a fresh reference for every reachable assembly on every call, so processes that compile repeatedly grew native memory by ~60-100 MB per compilation while the managed heap stayed too small to trigger a GC. Cache one MetadataReference per assembly path for the whole process and reuse it in every generator. Per-generator de-duplication now uses a HashSet of paths instead of a linear scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.
Fixes #1086
Problem
AssemblyGenerator.Compile()builds a new generator per call and callsMetadataReference.CreateFromFile()for every reachable assembly (~200 in a typical ASP.NET Core app). Roslyn prefetches the entire image into native memory that is only released on finalization. The managed heap barely moves, so the GC rarely runs and native memory grows by ~60-100 MB per compilation — e.g. many in-process hosts in a test run, each compiling inTypeLoadMode.Dynamic.Fix
ConcurrentDictionary<string, MetadataReference>keyed by assembly path; every generator reuses the same (immutable, thread-safe)PortableExecutableReference.HashSet<string>of paths instead of a linear_references.Any(x => x.Display == path)scan (same ordinal semantics).internal Referencesaccessor for tests).Measurements
The issue's
compilerrepro, 15 ×AssemblyGenerator.Compile, no forced GC, working set on macOS:Tests
Bug_1086_metadata_references_are_shared: separate generators share the same reference instances, each path is referenced once, repeated compiles still work. All 495 CodegenTests pass on net10.0.🤖 Generated with Claude Code