Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the .NET CLR do automatic inlining of properties?

Tags:

.net

clr

Does the .NET CLR runtime know how to optimize/inline simple property getters at runtime? For example:

property int Length { get; set; }

Will this be executing the "Length__get" function (building a stack for it, jumping to execute the code, etc) once it is JIT'd at runtime? Or is the jitter smart, and knows that this can just be rewritten as class field access?

like image 321
Armentage Avatar asked Jul 27 '09 13:07

Armentage


2 Answers

Yes, the CLR will inline that in "normal" cases. There are some situations in which inlining doesn't take place, however - including anything derived from MarshalByRefObject (because it could be a runtime proxy).

The rules for what gets inlined depend on the exact CLR you're using - x64 vs x86, version etc. Trivial properties are about as likely to be inlined as you'll get though :)

(For some reason I've seen a trivial property be slower than a field access for doubles in the past... there may be some restrictions around values larger than the native word size.)

like image 180
Jon Skeet Avatar answered Nov 12 '22 19:11

Jon Skeet


In .Net 2.0, methods (including property getters/setters) would be inlined if they had fewer than 32 bytes.

The .Net 3.5 JIT'r is a bit more intelligent, so it depends. It certianly could inline it.

For some discussion of this, see To Inline or not to Inline: That is the question.

like image 45
Stewart Avatar answered Nov 12 '22 20:11

Stewart