bpatton (3) [Avatar] Offline
#1
List<int> vs ArrayList
Page 88 makes a compelling argument about always using List<int> vs an ArrayList of ints.

In the MSDN article (http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) for List(T) in the performance consideration section they make the following claim:

If a value type is used for type T, the compiler generates an implementation of the List<(Of <(T>smilie>smilie class specifically for that value type. That means a list element of a List<(Of <(T>smilie>smilie object does not have to be boxed before the element can be used, and after about 500 list elements are created the memory saved not boxing list elements is greater than the memory used to generate the class implementation.

The book claims that once List<int> is JIT that implementation is cached for all future uses. I haven't been able to find anywhere to verify this but if true it would seem that for any reasonable size program the MSDN issue is moot and List<int> should always be used. Can anyone confirm?
jon.skeet (451) [Avatar] Offline
#2
Re: List&lt;int&gt; vs ArrayList
I don't see any disparity between what's on P88 and what's in MSDN. In particular, if you look in the second paragraph on P88, it states that "The JIT creates different code for each value type argument [...] that we use." It goes on for the next couple of paragraphs, mentioning that JITting lots of types does have a slight performance impact.

But yes, the overall gain from using generics is almost always going to be much more important than the slight issue of more native code. If you created an empty List<T> for each value type T in the framework, that would be a Bad Thing - but that kind of situation doesn't arise in real life smilie

I don't think MSDN is really recommending otherwise - just raising the fact that there is a slight penalty incurred by JITting separately for each type.

Jon
bpatton (3) [Avatar] Offline
#3
Re: List&lt;int&gt; vs ArrayList
It wasn't a disparity that I was trying to point out, more of surprise that the MSDN article calls out the native code overhead. We have a fairly large ASP.net application that we ported a couple of years back from 1.1 to 2.0. As we have been adding new code and updating old code we have been using Generic collections with reference types but much less so with value types (partly because of what's in MSDN). We looked at each ArrayList independently and based on whether it was a large or small collection would decide whether to use a Generic or not. But if, for example, the JIT uses the same class for each List<int> it finds (which makes sense now that I think about it) then there really is no reason to ever have an ArrayList of ints.

I was just looking for confirmation that the JIT re-uses the dynamic classes it generates for each List<T> based on a specific value type. If that is the case then I think the MSDN article should be clear that the overhead may only be an issue for less used value types rather than some like an int.

-brad
jon.skeet (451) [Avatar] Offline
#4
Re: List&lt;int&gt; vs ArrayList
Ah, fair enough smilie

No, it's definitely worth using generics over non-generic collections wherever possible. The JIT will definitely, definitely reuse the same code for List<int> wherever it finds it (within the same AppDomain, at least).

Jon
bpatton (3) [Avatar] Offline
#5
Re: List&lt;int&gt; vs ArrayList
Excellent. Thanks for the quick reply.