[SOLVED] When is a property "inlined" by code optimizations


This is kind of a noob question. I have a simple property

bool IsRoot { get { return parent==null; } }

that I call over and over many times from other properties and methods within the class (and derived classes)

I like to keep it like this because it makes the code readable (to me), but I am afraid that all the IsRoot calls are going to slow me down as they might not be “inlined” in the final release code. What I mean by “inlined” is that they are replaced by a copy of the parent==null evaluation in place of get_IsRoot().

Can someone explain to me when (or if) properties are inlined in C#, and for performance oriented applications are properties to be avoided, or not?

Short answer is: Properties translate to pure function calls and they might, or might not be inlined depending on what JIT decides. Trust the system to make the right choices and don’t worry about things that might affect things in the 5%-10% level, unless a profiler is used and the end result is fine tuned for performance.

Thanks for the links SO community, and I hope there was a way to award multiple correct answers. Sorry I had to pick one.


Here is an OLD post from Eric Gunnerson that talks about inlining.
The gist is that the jitter decides whether or a function is to be inlined. My understanding is that you can "encourage" inlining to some degree by writing small functions, but I don’t know how reliable that really is.

Here is another link from a question here on SO that asks a similar question. Many good links are included.

Here is an excerpt from one of the links from the second link I posted:

A short and simple method that is frequently used can be in-lined into the calling code. Currently, the JIT is documented (perhaps "blogged about" would be more precise here) to inline methods that are less than 32 bytes in length, do not contain any complex branching logic, and do not contain any exception handling-related mechanisms. See David Notario’s blog for some additional information on this topic (note that it is not entirely relevant for CLR 2.0).

I suppose that counts as a rough guideline of when a method might be inlined, but I don’t think that you can say that if a method does meet the criteria above that it will be inlined.

On a side note, you can force a function to NOT be inlined l ike this:

public void DoNotInlineMe()
  Console.WriteLine("Hello from non-inlined method");

Answered By – wageoghe

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *