I attended a seminar a few months ago and the speaker made the statement that the general cost of a boxing or unboxing operation has been reduced since .NET 1.1. I've looked through my (poor) notes and can't determine if this statement makes reference to the box and unbox instructions, or to the introduction of classes (i.e. generic types) that make boxing/unboxing less likely to occur.
Has there been a performance improvement in the CLR boxing-related instructions between .NET 1.1 and .NET 4.0, and if so, where can I find information on the measurements that show the gains?
I can't comment on the performance (for that you'd need profiling, etc) - but one interesting change here is the constrained op-code, that is used in particular with generics. The advantage here is that for a method like:
static void DoSomething<T>(T x, T y) where T : IComparable<T>
{
if(x.CompareTo(y) < 0) { /* whatever */ }
}
it will use a constrained-call for CompareTo
, which allows it to either use a static-call to the method implementation on a value-type (without an unbox), or use a virtual-call if it is a reference-type. Normally, calling an interface-based method on a value-type requires a box, so this is pretty helpful.