[Mono-dev] [PATCH] Optimization to System.Collections.Generic.List

Ben Maurer bmaurer at ximian.com
Sat Nov 12 14:00:25 EST 2005

On Sat, 2005-11-12 at 19:13 +0100, Laurent Debacker wrote:
> Hi!
> The file is a modified version of
> mcs/class/corlib/System.Collections.Generic/List.cs revision 52947.
> I've modified the ForEach method so that it no longer uses the
> enumerator. This avoids a lot of method calls to Enumerator (and the
> creation of yet another Enumerator object).

The Enumerator is a struct, it is allocated on the stack. So, your patch
just inlines the methods. With our current jit, this is probably faster,
first we aren't doing inlining, second even if we did inline, the struct
would always be on the stack, with int variables, we can keep it in a
register. We'd need something to decompose the struct into variables so
that we could do register allocation on it. I'd rather focus on making
foreach () on a List<T> be nearly as fast as manual iteration than
replacing things with manual loops blindly.

However, it looks like that is besides the point. MSFT does not do
failfast behavior (ie, it doesn't error out on modifications to the
list), as you commented in the method. I believe this is a bug (we
should file it with them), because modifying the collection will make
the enumeration meaningless. However, we should emulate the behavior for

Please rewrite using a for loop rather than a while loop and submit a
patch (svn diff).

-- Ben

More information about the Mono-devel-list mailing list