Disabling Optimizations

OK, so like 20 years ago (yes, I'm exaggerating a little) it was quite common to run across compiler bugs, where you needed to disable some optimization locally or globally to get your program to work correctly.  I think that's mostly gone now, but there are still the occasional cases.  Now days the most common reason for turning off optimizations is because you actually want to debug your code.

Am I right?  The CLR has one neat little custom attribute that allows you to turn off just one optimization in the JIT at a very local level: MethodImplAttributes.NoInlining

My question is this: Why would you ever want to use this?  My best guess is that people are wanting to the the StackTrace class to somehow compensate for C#'s lack of an equivalent for the __FILE__ macro (or it's friends __LINE__, __FUNC__, etc.)

What do you use this?  Or do you not use it?


Comments (2)

  1. Ryan Lamansky (Kardax) says:

    I’ve never used it, and probably never will. If anything, I wish there was a : MethodImplAttributes.PleaseInline or a more-general [PerformanceCritical] attribute to give the JIT a kick in the rear 🙂

  2. S N says:


    It is simple.

    consider the following code.

    // Frequency of execution < 50%




    // IL size is 16


    // Frequency of execution > 50%



    // IL size is 16


    fn3(bool condition)


    if (condition)





    In the above code path,

    Not only fn3() would have been inlined. It would have also inlined fn2() within the inlining of fn3().


Skip to main content