What Gives? DEBUG, RELEASE & Inline (Answer To Quiz)

2 בדצמבר 2008

תגיות: ,
אין תגובות

In my recent post I have asked what will be the outcome of a method. You can view the post here. So, it is time for an answer.

The Answer

It depends! Let me explain…

Here is the output when executing it in DEBUG:


As you can see, we get the full trace that starts with Main(), A(), B(), C() and finally BadMethod().

Now, lets try to execute it in RELEASE:


Now you can see that the outcome changed. The trace is shorter and we only see Main() and BadMethod().

What Is The Reason?

DEBUG and RELEASE build configurations are different in a way that RELEASE are optimized for performance. Most of the performance optimizations are done by the JIT compiler. So, back to the outcome, we can see the the JIT had optimized our code for better performance. In fact not every method got their own local stack frame as they were optimized into fewer methods.

Method Inline

It is possible to force the JIT not to inline the code, or in other words, not to optimize the code for us. You can do that in several ways:

  1. [MethodImpl(MethodImplOptions.NoInlining)]
  2. Use Try & Catch on every method.
  3. Turn off JIT optimizations.

If you try each of the above you will get the same outcome for both DEBUG and RELEASE. Having said that, my personal opinion on the matter is that we should try to let the JIT do its work and what it does best…Optimization!

kick it on DotNetKicks.com

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים