dimanche 5 avril 2015

Really weird behavior involving a static constructor in a generic class


Vote count:

0




I stumbled on this really weird behavior while doing some micro benchmarks of various common operations like boxing, casting, object allocation, etc, in order to better learn how to implement a data structure library I've been working on.


Here is the behavior itself: http://ift.tt/1NKW8ck


The code is rather long, so I don't want to post it here. However, if you're having trouble accessing it, here is the gist: http://ift.tt/1bYW391


Basically, having a static constructor (even if it is empty) in a generic class causes any method call to be drastically slower, but only if the type parameter is of a specific kind (probably a reference type). This also applies to any lambdas defined in the body. Lambdas do eventually get translated to methods in nested classes, so I guess nested classes are affected as well (?). Strangely, having a complicated initializer has no impact.


The performance impact is much higher than a normal interface method call, and is around as large as a virtual generic method call (this from other research).


What is the reason for this? This is the first time I've been genuinely stumped by how .NET behaves. I was under the impression that a static constructor runs once, and has little effect otherwise.



asked 1 min ago

Just Greg

3,004






Really weird behavior involving a static constructor in a generic class

Aucun commentaire:

Enregistrer un commentaire