Wow – several hours of performance training for the CLR team today… One thing that came up was how to enabling writing fast foreach code by doing a better job of implementing IEnumerable…  Apparently a while back there was some code that need to be super fast (think default winproc here) that did nested foreach’s on a couple of ArrayLists…

Some thing like:

ArrayList l1 = … //put n items in  

ArrayList l2 =…

foreach (object o in l1){

   foreach (object o in l2) {

   }

}

 

As you know, the way foreach works is that it allocates an Enumerator… so that means the inner loop is causes n enumerators to be allocated.  Not a big problem for a normal app, but for this often accessed part of the system it meant over a 1/3 of the memory usage for a sample app was Enumerators.  Wow.

 

We certainly learned our lesson in the ArrayList class.  If we had it to do over we’d follow the guidelines I outlined earlier and in addition make sure the Current property is inlineable.  Oh wait, we DO have it to do over again.. sort of… In Whidbey will be adding Generic versions of the collections, and you can bet we addressed this problem there!