Ahmed
Ahmed

Reputation: 7238

At what level C# compiler or JIT optimize the application code?

I want to know this info to reduce my code size so I will not waste my time optimize things that will be done by compiler or JIT.

for example:

if we assume the compiler inline the call to the get function of a property so I do not have to save the return value in a local variable to avoid function call.

I want to recommend a good reference that describes what is going on?

Upvotes: 9

Views: 3807

Answers (4)

Kev
Kev

Reputation: 119806

You may want to take a look at these articles:

JIT Optimizations - (Sasha Goldshtein - CodeProject)
Jit Optimizations: Inlining I (David Notario)
Jit Optimizations: Inlining II (David Notario)

To be honest you shouldn't be worrying too much about this level of micro-detail. Let the compiler/JIT'er worry about this for you, it's better at it than you are in almost all cases. Don't get hung up on Premature Optimisation. Focus on getting your code working, then worry about optimisations later on if (a) it doesn't run fast enough, (b) you have 'size' issues.

Upvotes: 16

Paul Biggar
Paul Biggar

Reputation: 28739

The most powerful optimization performed by a JIT is typically inlining. A JIT can even inline hundreds of functions deep (I heard this figure for JikesRVM). They will even inline things that aren't always possible to inline, and back it out later if they need to (called dynamic deoptimization).

A nice overview is http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_4.html.

For your specific question, I would say probably, if the function call in question is hot.

Upvotes: -1

Davy Landman
Davy Landman

Reputation: 15439

This looks like a kind of micro optimization which you shouldn't be looking at. If I'm not mistaken it depends on the architecture and version of the CLR which kind of optimization is applied.

If your method is called that much, and you really want it to inline, you can inline it yourself at the cost of spaghetti code.

I would recommend analyzing your algorithm, to inline a method will not save magnitudes of speed, while a better algorithm can make your running time decrease from hours to seconds.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062502

If you are worried about performance, run a profiler. Then change code. Chances are that you will never in a million years guess 100% correctly where the time is going. You could be changing the 0.02% timing, and leaving the method that contributes 62% of the burden. You could also be making it worse. Without a profiler and evidence, you are blind.


You can't assume that the JIT will inline a property getter. There are many reasons it may or may not do so; size of the method body, virtual, value vs reference type, architecture, debugger attached, etc.

"Hoisting" still has a place, and can still achieve savings if the code is called repeatedly in a tight loop; for example:

var count = list.Count;
for(int i = 0 ; i < count ; i++) {...}

(forget the for vs foreach debate fr the above - this is an orthogonal discussion). In the above, the "hoist" will help performance. But just to be really confusing - with arrays, it is the opposite, and it is more efficient to not hoist it:

for(int i = 0 ; i < arr.Length ; i++) {...}

The JIT recognises this and removes the bounds check (as arrays are fixed size).

Upvotes: 18

Related Questions