Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Check for ICollection<T>in GetEnumerator() - #12562

#12562
Merged
YuliiaKovalova merged 4 commits into
dotnet:maindotnet/msbuild:mainfrom
Erarndt:dev/erarndt/copyOnReadICollectionErarndt/msbuild:dev/erarndt/copyOnReadICollectionCopy head branch name to clipboard
Oct 27, 2025
Merged

Check for ICollection<T>in GetEnumerator()#12562
YuliiaKovalova merged 4 commits into
dotnet:maindotnet/msbuild:mainfrom
Erarndt:dev/erarndt/copyOnReadICollectionErarndt/msbuild:dev/erarndt/copyOnReadICollectionCopy head branch name to clipboard

Conversation

@Erarndt

@Erarndt Erarndt commented Sep 23, 2025

Copy link
Copy Markdown
Contributor

Fixes #

Context

There is a meaningful amount of allocations coming from resizing a List<T> in CopyOnReadEnumerable.GetEnumerator()

image

Total allocations in this path are ~68MB for OrchardCore. We won't eliminate them entirely, but this will reduce them.

Changes Made

Testing

Notes

Copilot AI review requested due to automatic review settings September 23, 2025 23:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 before IReadOnlyCollection<T> check
  • Uses collection count for initial List<T> capacity to prevent dynamic resizing
  • Targets ~68MB allocation reduction in OrchardCore scenarios

Comment thread src/Build/Collections/CopyOnReadEnumerable.cs
Comment thread src/Build/Collections/CopyOnReadEnumerable.cs
@YuliiaKovalova
YuliiaKovalova enabled auto-merge (squash) October 13, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.