-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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); | |||
} |
There was a problem hiding this comment.
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.
} | |
} | |
// 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) |
There was a problem hiding this comment.
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.
Fixes #
Context
Changes Made
Testing
Notes