Skip to content
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

Add Enumerable.TryGetNonEnumeratedCount (Implements #27183) #48239

Conversation

eiriktsarpalis
Copy link
Member

Fix #27183.

@eiriktsarpalis eiriktsarpalis added this to the 6.0.0 milestone Feb 12, 2021
@eiriktsarpalis eiriktsarpalis self-assigned this Feb 12, 2021
@ghost
Copy link

ghost commented Feb 12, 2021

Tagging subscribers to this area: @eiriktsarpalis
See info in area-owners.md if you want to be subscribed.

Issue Details

Fix #27183.

Author: eiriktsarpalis
Assignees: eiriktsarpalis
Labels:

area-System.Linq

Milestone: 6.0.0

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@eiriktsarpalis eiriktsarpalis changed the title Add Enumerable.TryGetEnumeratingCount (Implements #27183) Add Enumerable.TryGetNonEnumeratingCount (Implements #27183) Feb 12, 2021
src/libraries/System.Linq/src/System/Linq/Count.cs Outdated Show resolved Hide resolved
src/libraries/System.Linq/src/System/Linq/Count.cs Outdated Show resolved Hide resolved
src/libraries/System.Linq/src/System/Linq/Count.cs Outdated Show resolved Hide resolved
src/libraries/System.Linq/tests/CountTests.cs Outdated Show resolved Hide resolved
src/libraries/System.Linq/src/System/Linq/Count.cs Outdated Show resolved Hide resolved
@eiriktsarpalis eiriktsarpalis changed the title Add Enumerable.TryGetNonEnumeratingCount (Implements #27183) Add Enumerable.TryGetNonEnumeratedCount (Implements #27183) Feb 13, 2021
Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

Other than the build breaks, LGTM.

@@ -190,7 +190,7 @@ public void Reserve(int count)
public bool ReserveOrAdd(IEnumerable<T> items)
{
int itemCount;
if (EnumerableHelpers.TryGetCount(items, out itemCount))
if (System.Linq.Enumerable.TryGetNonEnumeratedCount(items, out itemCount))
Copy link
Member

Choose a reason for hiding this comment

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

Nit: using System.Linq;

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to avoid bringing all the enumerable methods into scope since this is a System.Collections namespace.

/// The method is typically a constant-time operation, but ultimately this depends on the complexity
/// characteristics of the underlying collection implementation.
/// </remarks>
public static bool TryGetNonEnumeratedCount<TSource>(this IEnumerable<TSource> source, out int count)
Copy link

@alrz alrz Feb 15, 2021

Choose a reason for hiding this comment

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

This doesn't need to be generic? if it does, I think a non-generic overload would also make sense.
(roslyn is considering using this method as part of list pattern lowering, we probably don't want to skip it for IEnumerable see https://github.com/dotnet/csharplang/blob/master/meetings/2021/LDM-2021-02-03.md).

Copy link
Member Author

Choose a reason for hiding this comment

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

We could do that, however it becomes more difficult to check for generic interfaces, which would require some form of reflection.

Copy link

Choose a reason for hiding this comment

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

which would require some form of reflection.

That's definitely a no-go because the point of using this is performance. Perhaps those generic interfaces need a non-generic base with Count prop. IMO TryGetNonEnumeratedCount shouldn't care about TSource.

@eiriktsarpalis
Copy link
Member Author

Linker test build failures seem unrelated to the change.

@eiriktsarpalis eiriktsarpalis merged commit d687544 into dotnet:master Feb 15, 2021
@eiriktsarpalis eiriktsarpalis deleted the enumerable-trygetnonenumeratingcount branch February 15, 2021 17:15
@ghost ghost locked as resolved and limited conversation to collaborators Mar 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Non-enumerating Count Linq Method
3 participants