Repository navigation
Consolidate optimisation paths for Count #135270
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,24 +10,9 @@ public static partial class Enumerable | |
| { | ||
| public static int Count<TSource>(this IEnumerable<TSource> source) | ||
| { | ||
| if (source is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); | ||
| } | ||
|
|
||
| if (source is ICollection<TSource> collectionoft) | ||
| { | ||
| return collectionoft.Count; | ||
| } | ||
|
|
||
| if (source is Iterator<TSource> iterator) | ||
| { | ||
| return iterator.GetCount(onlyIfCheap: false); | ||
| } | ||
|
|
||
| if (source is ICollection collection) | ||
| if (TryGetNonEnumeratedCount(source, out int nonEnumeratedCount)) | ||
| { | ||
| return collection.Count; | ||
| return nonEnumeratedCount; | ||
| } | ||
|
|
||
| int count = 0; | ||
|
|
@@ -102,11 +87,6 @@ public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, | |
| /// </remarks> | ||
| public static bool TryGetNonEnumeratedCount<TSource>(this IEnumerable<TSource> source, out int count) | ||
| { | ||
| if (source is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); | ||
| } | ||
|
|
||
| if (source is ICollection<TSource> collectionoft) | ||
| { | ||
| count = collectionoft.Count; | ||
|
|
@@ -129,6 +109,11 @@ public static bool TryGetNonEnumeratedCount<TSource>(this IEnumerable<TSource> s | |
| return true; | ||
| } | ||
|
|
||
| if (source is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); | ||
| } | ||
|
Comment on lines
+112
to
+115
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving this null check below the type checks is a very minor optimisation for the case where any of the optimised types are provided.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does moving it improve such cases? The null check has to be performed regardless, no? |
||
|
|
||
| count = 0; | ||
| return false; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Sometimes it is possible to avoid iterating over all elements even when TryGetNonEnumeratedCount fails:
The PR introduces a regression for such case.