33

Possible Duplicate:
Inline functions in C#?
What is method inlining?

i've been debugging code, and a 'possible' source of the issue is in a function which is marked with this code:

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
protected virtual void PropertyHasChanged()

Reading MSDN says this: Specifies that the method cannot be inlined.

But what is 'inlining' a method?

EDIT:

To clarify: the PropertyHasChanged() method is called in the SET method of every property and updates (adds 1 to it) an internal object counter. When that counter > 0, the object is marked as 'dirty' and will be saved to the database when Save is called. When the counter = 0, the object will not be saved to the database. Now i've got the idea that this code sometimes is not executed (the counter is not increased) so the object won't be saved to the database,

Community
  • 1
  • 1
Michel
  • 23,085
  • 46
  • 152
  • 242
  • 2
    Note that while this is a valid question, the inlining or not of this method almost certainly has nothing to do with whatever issue you are debugging. – AakashM Mar 07 '12 at 11:54
  • @Veger: i've read that post, but it's mostly about inline methods in C#, and not about WHAT it means... – Michel Mar 07 '12 at 11:57
  • (my dupe candidate isn't C# specific, but does explain what inlining actually is) – AakashM Mar 07 '12 at 11:58
  • The highest voted answer states: "Inline methods are simply a compiler optimization where the code of a function is rolled into the caller." Which (very briefly) describes, that the the method code is placed directly into the calling context (instead of creating an actual method). That is inlining. – Veger Mar 07 '12 at 11:58
  • This quacks like a classic threading race. Yes, inlining can affect bugs like that. – Hans Passant Mar 07 '12 at 12:11
  • Hans, that is what we thought we did experience. Because.... if we added a thread.sleep after this method, all worked fine. But: how can a compiler optimization cause a threading issue? When i read the answer by @Oded, i don't see a threading issue coming up? – Michel Mar 07 '12 at 13:39
  • Slightly off-topic, but of note: methods marked `virtual` in .NET will *NEVER* be inlined. – BrainSlugs83 Mar 06 '14 at 20:15
  • Also of interest: 1.) because the method is `virtual`, it could be getting overwritten somewhere (in a sub-class for example) -- and that overwriting method may be both not updating the counter, and not calling the base class implementation, that might be the cause. 2.) as others have eluded to, there could also be some threading issue at play -- if so, it might make sense to lock on a sync variable while updating your counter, and/or declare your counter as `volatile`. [also: why use a counter and not a `bool`?] – BrainSlugs83 Mar 06 '14 at 20:19

1 Answers1

47

Inlining a method/property means that the compiler takes it and replaces the calls to it with its contents (making sure correct variables etc). This is not something that is C# specific.

This is done for performance normally.

For example, with this method and call:

private long Add(int x, int y)
{
   return x + y;
}

var z = Add(x, y);

The compiler may inline it as (eliminating the method in the process):

var z = x + y;

The Wikipeida article Inline expansion starts with:

In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing the final size of the program (i.e. the binary file size).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 7
    For clarity - the C# compiler will *never* inline a method. It's the JITter's task. – IS4 Apr 13 '17 at 08:30