0

I want to know if my code can and is Inlined or not. I have found a way to do this, which is:

Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

But i am not sure how it´s supposed to tell me. If i use it on this:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ImageCodecInfo GetEncoderInfo(ImageFormat format)
{
    Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);
    return ImageCodecInfo.GetImageEncoders().ToList().Find(delegate(ImageCodecInfo codec)
    {
        return codec.FormatID == format.Guid;
    });
}

What is the Console suppose to write if it´s inlined? From my understanding, it´s supposed to write the name of the caller, for example:

Private void Caller()
 {

ImageCodecInfo GetEncoderInfo(bmpFormat)
 }

Console will then write Caller.

Is this correct? If so, then nothing i have tried it on is inlined.

Zerowalker
  • 761
  • 2
  • 8
  • 27
  • 1
    You would have to examine the actual IL in the assembly to determine if a method is inlined. – Robert Harvey Aug 22 '13 at 16:52
  • That doesn´t tell me much, can you be more detailed? – Zerowalker Aug 22 '13 at 16:54
  • See here: http://msdn.microsoft.com/en-us/library/ceats605.aspx – Robert Harvey Aug 22 '13 at 16:55
  • 1
    @RobertHarvey: Isn't inlining always performed by the JIT compiler, not the C# compiler? – dtb Aug 22 '13 at 16:55
  • @dtb: Ah, I think you're right. You would have to disassemble the actual binary code to identify inlining. Totally not worth it under any reasonable scenario, IMO. – Robert Harvey Aug 22 '13 at 16:56
  • Really, well that sucks. I would love to know if certain methods are inline or not. Cause if they aren´t, i need to move them and inline manually (at least for certain things). – Zerowalker Aug 22 '13 at 16:58
  • 2
    Have you done some measurements with a profiler and *specifically identified method calls as a performance problem?* – Robert Harvey Aug 22 '13 at 17:00
  • Yes i have, and certain stuff are Alot slower. Though those are at very small scales (fast calculations). – Zerowalker Aug 22 '13 at 17:02
  • But, it it possible to call AggressiveInlining on everything? Instead of writing it on every method? – Zerowalker Aug 22 '13 at 17:02
  • Use `unsafe` code blocks for those calculations. The overhead for a method call is very minimal, and if your loop is that tight already, just inline the code from the method yourself. – Robert Harvey Aug 22 '13 at 17:03
  • You *do not* want to inline everything. Measure your performance with a profiler, and inline only those calls that are going to make a significant difference in performance. – Robert Harvey Aug 22 '13 at 17:04
  • Should i call unsafe, even when it´s safe code? Really? Isn´t Inline faster, always? And well, for the method i am thinking about, it isn´t small, i prefer to have it in a seperate class. And about profiling, the problem is, i can´t use it. It says "no launchable projects", and i don´t find any way to fix that:( – Zerowalker Aug 22 '13 at 17:07
  • Are you familiar with the term "premature optimization?" Seriously, you should not be worrying about inlining. In 99 percent of cases, *inlining is not going to make any difference in the performance of your application.* The JIT is smart enough to make that decision for you when it does matter. – Robert Harvey Aug 22 '13 at 17:10
  • Yes, but i am not trying to optimize just for fun. But well anyway, my question is answered, it´s not possible without looking at binary code and disassemble etc, which isn´t something for me. Many thanks! – Zerowalker Aug 22 '13 at 17:17

1 Answers1

0

GetCurrentMethod prevents inlining (Source http://prdlxvm0001.codify.net/pipermail/ozdotnet/2011-March/009085.html)

Keep in mind that inlining is done during JIT, not compilation.

Robert Horvick
  • 3,966
  • 21
  • 18