-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
area-System.Linqin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is mergedneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsiderationIssue has been initially triaged, but needs deeper consideration or reconsiderationtenet-performancePerformance related issuePerformance related issue
Milestone
Description
The implementation of the new CountBy adds an unnecessary outer loop through the dictionary elements.
runtime/src/libraries/System.Linq/src/System/Linq/CountBy.cs
Lines 39 to 42 in 019d758
| foreach (KeyValuePair<TKey, int> countBy in BuildCountDictionary(enumerator, keySelector, keyComparer)) | |
| { | |
| yield return countBy; | |
| } |
I think we can return created Dictionary enumerator:
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer = null) where TKey : notnull
{
// Same code
return new CountByEnumerable<TSource, TKey>(source, keySelector, keyComparer);
}
private sealed class CountByEnumerable<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer) : IEnumerable<KeyValuePair<TKey, int>> where TKey : notnull
{
public IEnumerator<KeyValuePair<TKey, int>> GetEnumerator()
{
using IEnumerator<TSource> enumerator = source.GetEnumerator();
if (!enumerator.MoveNext())
{
return Empty<KeyValuePair<TKey, int>>().GetEnumerator();
}
return BuildCountDictionary(enumerator, keySelector, keyComparer).GetEnumerator();
}
}Metadata
Metadata
Assignees
Labels
area-System.Linqin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is mergedneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsiderationIssue has been initially triaged, but needs deeper consideration or reconsiderationtenet-performancePerformance related issuePerformance related issue