Conversation
nkolev92
approved these changes
Feb 13, 2026
Member
Member
|
I wanted to double-check my assumption, and yes, passing a func without using an anonymous function wrapper avoids any allocations:
source codeI don't know if |
Contributor
Author
Member
|
I'm fine with it as is. |
zivkan
approved these changes
Feb 19, 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.
🤖 AI-Generated Pull Request 🤖
This pull request was generated by the VS Perf Rel AI Agent. Please review this AI-generated PR with extra care! For more information, visit our wiki. Please share feedback with TIP Insights
Issue:
CachingSourceProvider.CreateRepositorycallsConcurrentDictionary.GetOrAdd(key, value)where thevalueisnew SourceRepository(source, _resourceProviders, type).This eagerly constructs a
SourceRepositoryon every call — even when the key already exists in the cache and the newly-constructed instance is immediately discarded.The
SourceRepositoryconstructor callsInit()→GroupBy()→Sort(), allocating multipleList<T>,Dictionary<Type, ...>, andInt32[]instances per construction even if they are already cached.This matches the allocation stack flow showing
DependencyGraphSpecRequestProvider.GetRequestsFromItems→RestoreArgs.GetEffectiveSourcesCore→CachingSourceProvider.CreateRepository→SourceRepository..ctor→SourceRepository.Init→SourceRepository.Sort→List<T>..ctor→EnumerableSorter.Sort→JIT_NewArr1→SVR::GCHeap::Alloc→TypeAllocated!System.Int32[]Issue type: Eliminate redundant
SourceRepositoryconstruction and associated LINQ/sort/collection allocations on cache-hit hot pathProposed fix: Add a
TryGetValuefast-path check beforeGetOrAddinCachingSourceProvider.CreateRepository.On a cache hit (the common case under parallel restore),
TryGetValuereturns the existingSourceRepositorywith zero allocations — no constructor call, no closure, no LINQ overhead. TheGetOrAddfallback is only reached on a cache miss, where theSourceRepositoryconstruction cost is unavoidable and identical to the original behavior.The
_cachedSourcesdictionary is append-only (no removals anywhere in the codebase), so aTryGetValuehit guarantees the key remains present, making the non-atomicTryGetValue→GetOrAddsequence safe.Alternatively, if a single dictionary access is preferred,
GetOrAdd(key, _ => new SourceRepository(...))can be used — it trades a small closure allocation (~32 bytes) per call for simpler code, which is still a major improvement over the current eager construction.Best practices wiki
See related failure in PRISM
ADO work item