[Mono-dev] Trivial Enumerable Optimizations
Jonathan Pryor
jonpryor at vt.edu
Fri May 9 12:32:38 EDT 2008
On Fri, 2008-05-09 at 17:38 +0200, Robert Jordan wrote:
> Jonathan Pryor wrote:
> > So is it possible to further optimize the LongCount() extension method?
> > Perhaps something like this?
> >
> > public static long LongCount<TSource> (this IEnumerable<TSource> source)
> > {
> > // as before...
> > ICollection<TSource> c = source as ICollection<TSource>;
> > if (c != null) {
> > try {
> > return c.Count;
> > }
> > catch {
> > // ignore; collection has > int.MaxValue elements,
> > // so count manually below.
> > }
> > }
> > // as before...
> > }
>
> I'd check whether "source" is an array and return what
> Array.GetLongLength returned.
This was part of the original optimization which started this thread
(though that uses the LongLength property, not the GetLongLength()
method), and has been committed to svn.
So it would be part of the first "as before" block of code.
- Jon
More information about the Mono-devel-list
mailing list