Home Dashboard Directory Help
Search

Enumerable.Count() is unnecessarily slow using a Covariant IEnumerable<T> by Oren Novotny


Status: 

Closed
 as Fixed Help for as Fixed


3
0
Sign in
to vote
Type: Bug
ID: 454130
Opened: 5/19/2009 3:01:48 PM
Access Restriction: Public
1
Workaround(s)
view
1
User(s) can reproduce this bug

Description

Now that IEnumerable is covariant, some of the methods in Linq to Objects run slower than expected. For example. Count(). Count checks to see if the input implements ICollection<TSource> and if so, shortcuts and returns .Count. With a covariant IEnumerable though, it won't. The easy workaround is for Enumerable.Count() to also check for the non-generic ICollection and if so return Count there. As Count (and other similar methods, aren't type-specific, then it works.
Details
Sign in to post a comment.
Posted by Microsoft on 6/4/2009 at 4:50 PM
Hi Oren,

It does indeed check both :)

David Sterling
Visual C# Compiler Team
Posted by Oren Novotny on 5/28/2009 at 8:50 PM
Ed,

Long as it checks both ICollection<T> and ICollection as ICollection<T> does not derive from ICollection and some collections may not implement ICollection....wouldn't want to lose out on those ones either.

Thanks!
Oren
Posted by Microsoft on 5/28/2009 at 6:00 PM
Nice catch, Oren. I've changed the Count optimization to use ICollection instead of ICollection<T>.

Ed Maurer
Development Manager, C# & VB compilers.
Posted by Microsoft on 5/22/2009 at 3:50 AM
Thanks for your feedback.

We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.
These specialized experts will follow-up with your issue.
Posted by Microsoft on 5/21/2009 at 4:16 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by Oren Novotny on 5/19/2009 at 3:02 PM
Write your own Count extension method and ensure it's resolved first.