It's a great question that was unfortunately learned the hard way (through WinDbg rather than reflector or ILDasm). A colleague of mine (Herman Chan) was kind enough to pass on his findings – in summary, be very careful of the number of instantiations you have per request on generic types marked with new().
We were fully aware of the costs associated with late-bound instantiation [3] when building our ORM solution using reflection, and measures were taken to compensate for it; but we didn't have a good sense that Generic types required the same level of attention!
The 2nd link provides a nice summary [2] – note the distinction between CreateInstance() and CreateInstance<T>():
Running 10000000 iterations of creation test. Direct Call 00:00:00.5320932 Delegate Wrapper 00:00:00.8127212 Generic New 00:00:20.2164442 Activator.CreateInstance 00:00:43.3707797
References:
[1] –
http://stackoverflow.com/questions/367577/why-does-the-c-compiler-emit-activator-createinstance-when-calling-new-in-with-a
[2] –
http://blogs.msdn.com/zelmalki/archive/2008/12/13/creating-objects-using-generic-new-constraint.aspx
[3] -
http://hyperthink.net/blog/PermaLink,guid,0d573ba5-1228-419f-bd69-065f53fc64a8.aspx http://aspnetresources.com/blog/dynamic_insstantiation_perf.aspx
0 comments:
Post a Comment