Skip to content

Check for count through IReadOnlyCollection interface #11805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Build/Collections/CopyOnReadEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public IEnumerator<TResult> GetEnumerator()
#endif
list = new List<TResult>(count);
}
Copy link
Preview

Copilot AI May 7, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a comment here explaining that this branch is used to pre-allocate the List capacity using IReadOnlyCollection's Count for improved performance.

Suggested change
}
}
// If the backing enumerable is an IReadOnlyCollection, use its Count property
// to pre-allocate the List capacity for improved performance.

Copilot uses AI. Check for mistakes.

else if (_backingEnumerable is IReadOnlyCollection<TSource> readOnlyCollection)
Copy link
Member

Choose a reason for hiding this comment

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

How does this interact with the previous #if net directive please?
Might the if (_backingEnumerable.TryGetNonEnumeratedCount(out int count)) condition make this one basically dead?
Even if it doesn't, it means that in some cases we're mixing two paradigms together I think.

{
list = new List<TResult>(readOnlyCollection.Count);
}
else
{
list = new List<TResult>();
Expand Down