Check for ICollection<T>in GetEnumerator() - #12562
#12562Merged
YuliiaKovalova merged 4 commits intoOct 27, 2025
dotnet:maindotnet/msbuild:mainfrom
Erarndt:dev/erarndt/copyOnReadICollectionErarndt/msbuild:dev/erarndt/copyOnReadICollectionCopy head branch name to clipboard
Merged
Check for ICollection<T>in GetEnumerator()#12562YuliiaKovalova merged 4 commits intodotnet:maindotnet/msbuild:mainfrom Erarndt:dev/erarndt/copyOnReadICollectionErarndt/msbuild:dev/erarndt/copyOnReadICollectionCopy head branch name to clipboard
YuliiaKovalova merged 4 commits into
dotnet:maindotnet/msbuild:mainfrom
Erarndt:dev/erarndt/copyOnReadICollectionErarndt/msbuild:dev/erarndt/copyOnReadICollectionCopy head branch name to clipboard
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the CopyOnReadEnumerable.GetEnumerator() method by checking for ICollection<T> to get the collection count before initializing a List<T>. This reduces memory allocations by avoiding list resizing during enumeration.
- Adds
ICollection<T>check to obtain count beforeIReadOnlyCollection<T>check - Uses collection count for initial
List<T>capacity to prevent dynamic resizing - Targets ~68MB allocation reduction in OrchardCore scenarios
Youssef1313
reviewed
Sep 30, 2025
YuliiaKovalova
approved these changes
Oct 13, 2025
YuliiaKovalova
enabled auto-merge (squash)
October 13, 2025 15:53
This was referenced Oct 27, 2025
This was referenced Nov 19, 2025
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 #
Context
There is a meaningful amount of allocations coming from resizing a
List<T>inCopyOnReadEnumerable.GetEnumerator()Total allocations in this path are ~68MB for OrchardCore. We won't eliminate them entirely, but this will reduce them.
Changes Made
Testing
Notes