Fix VS allocation issue: NuGet restore content-file glob matching#7405
Open
nareshjo wants to merge 4 commits into
Open
Fix VS allocation issue: NuGet restore content-file glob matching#7405nareshjo wants to merge 4 commits into
nareshjo wants to merge 4 commits into
Conversation
nkolev92
reviewed
May 23, 2026
jeffkl
previously approved these changes
May 26, 2026
nkolev92
previously approved these changes
May 26, 2026
| // Matcher.Execute once per nuspec entry instead of once per (entry × file), reducing | ||
| // MatcherContext + internal list/array allocations from O(N×M) to O(N). | ||
| var relativePathToEntries = new Dictionary<string, List<ContentFilesEntry>>(entryMappings.Count, StringComparer.OrdinalIgnoreCase); | ||
| foreach ((var file, var entries) in entryMappings) |
Member
There was a problem hiding this comment.
using type specific declarations here imprves redability.
Contributor
Author
There was a problem hiding this comment.
switched to explicit types for that - the tuple deconstruction seems to be the main case where types weren't obvious from the rest of the line. Kept var for the others since the types are visible from new/method names there, which is consistent with rest of the file and codebase
nkolev92
approved these changes
May 28, 2026
jeffkl
approved these changes
Jun 1, 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.
Bug
Fixes: 2576115
Description
This PR addresses several of the top most allocation issues in recent VS releases, with sampled allocation rates for the highest at 546 MB/s (p50) and 1,072 MB/s (p90). All originate from the same code path in NuGet restore content-file matching and are resolved by a single localized fix.
Problem
ContentFileUtils.GetContentFileGroupcallsMatcher.Match()N×M times (once per nuspec<contentFiles>pattern × content file), each internally creating a newInMemoryDirectoryInfoandMatcherContext. TheMatcherContextallocates lists, arrays, hash sets, and strings for construction and directory traversal — all immediately discarded.Allocation site tree derived from VS telemetry, filtered to types present in the current codebase (all addressed by this fix):
Fix
Replace per-file
Matcher.Match()with a single sharedInMemoryDirectoryInfocontaining all M files.Matcher.Executeis called N times (once per nuspec entry) instead of N×M. Matched paths are mapped back via dictionary lookup.Impact of fix
Measured by benchmarking
GetContentFileGroupin isolation with synthetic packages containing N<contentFiles>patterns (all**/*) and M content files across 10 subdirectories. BenchmarkDotNet[MemoryDiagnoser], .NET Framework 4.7.2, out-of-process:N = nuspec
<contentFiles>patterns, M = content files per package. Gen0 = GC Generation 0 collections per 1000 calls.Safety
MatcherAPI, sameOrdinalIgnoreCasesemantics, same nuspec entry ordering (bottom-has-priority invariant preserved)InMemoryDirectoryInfois immutable —MatcherContextowns its own traversal state, so reuse acrossExecutecalls is safeinternal staticmethod, one production caller (LockFileUtils.AddContentFiles)InMemoryDirectoryInfo, multi-matcher reuse correctness) + 26 existing integration tests pass (globs, excludes, rule ordering,.pptransforms,_._empty folders, TxM/language selection)See related failures in PRISM
PR Checklist