Improve SourceGeneratorTests test speed.#12911
Merged
chsienki merged 1 commit intodotnet:mainfrom Mar 18, 2026
Merged
Conversation
The CreateBaseProject method was called per-test (~85 times), each time independently loading DependencyContext, scanning 200+ DLLs, performing O(n²) dedup checks, and creating MetadataReference objects for the same identical reference set. Cache the fully-constructed base Project in a static Lazy<Project>. Since Project is immutable, all test mutations (AddDocument, AddAdditionalDocument) return new forks without affecting the shared instance. The two tests that need custom CSharpParseOptions fork via WithParseOptions(). Also replaces the O(n²) dedup loop (.Any() per DLL) with Dictionary.TryAdd for O(1) lookups during the one-time initialization. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
davidwengier
approved these changes
Mar 18, 2026
This was referenced Mar 18, 2026
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
Cache the fully-constructed base
Projectin source generator tests so it is built once and shared across all ~85 test invocations, reducing test execution time by ~60%.Problem
CreateBaseProject()was called per-test, each time independently:DependencyContextvia reflectionDirectory.EnumerateFilesMetadataReference.CreateFromFile()for each DLL.Any()on all existing referencesSolutionsnapshots per addThe resulting reference set was identical every time.
Fix
Project(workspace, compilation options, parse options, and all metadata references) in astatic Lazy<Project>. SinceProjectis immutable, everyAddDocument/AddAdditionalDocumentcall in individual tests returns a new fork without affecting the shared instance.CSharpParseOptionsfork viaWithParseOptions()..Any()per DLL) withDictionary.TryAddfor O(1) lookups during the one-time initialization.Results