Dynamic binding is one of the many new features included in .NET 4.0. I’ve been doing some testing today with mixing dynamic and generics, and came across a slightly obscure scenario that may or may not prove useful in the future.
When a dynamic object performs method resolution at runtime, the result is cached by the DLR in a “call site”. The next time this code is run, the call site is only recomputed if there is a cache miss (i.e., different types of arguments are passed to the method).
We can use generics to turn one call site into many call sites, all cached independently, thereby improving performance when invoked later:
Even though the SingleCallSite method only has one call site (for the + operator) and the MultipleCallSites method has two call sites (one for the + operator, and one for converting the dynamic result to T), MultipleCallSites reliably runs faster, as this remarkably unscientific test code demonstrates:
This idea came from Luca Bolognese’s blog post Simulating INumeric with dynamic in C# 4.0, where he states that with a generic signature “you get a different call site with each combination of type arguments and, since they are separate, the binding caches should stay small.”
Final note: this is only an implementation detail of the DLR. These performance characteristics may change in a .NET service pack or future version.