[Mono-dev] Trivial Enumerable Optimizations

Jonathan Pryor jonpryor at vt.edu
Fri May 9 12:30:16 EDT 2008


On Fri, 2008-05-09 at 11:27 -0400, Avery Pennarun wrote:
> On 5/9/08, Jonathan Pryor <jonpryor at vt.edu> wrote:
> > If the developer asks for a count, we have an obligation to provide a
> >  count.  Throwing an exception claiming "You're a moron to ask about size
> >  on a collection this big" should not be acceptable.
> 
> But if a programmer is working on a collection that ought to be able
> to provide an O(1) count, and it normally does, but suddenly it
> doesn't and decides to count through 2+ billion records, the data
> structure's contract has already been broken.  We're only talking
> about what kind of phone call the developer gets when his app doesn't
> work, not about actually making it work :)

If the programmer is working on a collection with a Count property, then
they can use the Count() extension method (which *will* use
ICollection<T>.Count).  We're talking about the LongCount() extension
method, which has no equivalent interface property.  The only direct
equivalent is System.Array.LongLength, which *is* checked for.

So currently option (1) is to always use Count() if that's what your
underlying collection supports;  (2) always use arrays with LongCount();
and (3) accept that LongCount() will need to actually iterate over all
collection members.

 - Jon




More information about the Mono-devel-list mailing list