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

Conversation

Erarndt
Copy link
Contributor

@Erarndt Erarndt commented May 7, 2025

Fixes #

Context

Changes Made

Testing

Notes

@Copilot Copilot AI review requested due to automatic review settings May 7, 2025 22:24
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request optimizes list allocation in GetEnumerator by utilizing the Count property from an IReadOnlyCollection interface.

  • Introduces a code branch to pre-allocate list capacity when the backing enumerable implements IReadOnlyCollection.
  • Enhances performance by reducing unnecessary list growth.

@@ -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.

@@ -72,6 +72,10 @@ public IEnumerator<TResult> GetEnumerator()
#endif
list = new List<TResult>(count);
}
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.

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.

2 participants