[Mono-dev] Trivial Enumerable Optimizations

Robert Jordan robertj at gmx.net
Fri May 9 14:10:56 EDT 2008


>> 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.

OK. Now I understand your dilemma.

MS.NET is not optimizing LongCount() on ICollection<T> sources.
It enumerates them straight w/out trying to get the length
from "Count".

This is not the case for the Count() extension: it tries to
access the "Count" property:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class NotImplementedCollection<T> : ICollection<T>
{
     public void Add(T item)
     {
         throw new NotImplementedException();
     }

     public void Clear()
     {
         throw new NotImplementedException();
     }

     public bool Contains(T item)
     {
         throw new NotImplementedException();
     }

     public void CopyTo(T[] array, int arrayIndex)
     {
         throw new NotImplementedException();
     }

     public int Count
     {
         get { throw new NotImplementedException(); }
     }

     public bool IsReadOnly
     {
         get { throw new NotImplementedException(); }
     }

     public bool Remove(T item)
     {
         throw new NotImplementedException();
     }

     public IEnumerator<T> GetEnumerator()
     {
         throw new NotImplementedException();
     }

     System.Collections.IEnumerator 
System.Collections.IEnumerable.GetEnumerator()
     {
         throw new NotImplementedException();
     }

}


class Test
{
	static void Main ()
	{
         	NotImplementedCollection<int> c = new 
NotImplementedCollection<int> ();

		// breaks in GetEnumerator
		Console.WriteLine(c.LongCount<int>());

		// breaks in Count
		Console.WriteLine(c.Count<int>());
	}
}

Robert



More information about the Mono-devel-list mailing list