Why is the .maxstack value greater in the released dll / exe mode?

I am curious to know the difference between the IL code generated in Debug and Release mode. I wrote a simple code.

        using System;

        namespace ConsoleApplication6
        {
            class Program
            {
                static void Main(string[] args)
                {
                    int result = int.Parse(Console.ReadLine());
                    if (true)
                    {
                        Console.WriteLine("Hi There");
                    }
                    Console.WriteLine("Done");
                    Console.ReadLine();
                }
            }
        }

I compared the exe generated using IL Deassembler. And find the value .maxstack is 8 in Release mode and 1 in build mode. Before asking a question here, I searched for some online articles and found that the number of entries made for the stack for any operation is counted here. In addition, according to my understanding, the release mode code is more organized and optimized. Someone please confirm your understanding and let me know if I am wrong? Also, I wanted to know if the version mode output is optimized, why does the stack size increase? What does stack size mean. Thank.


        .method private hidebysig static void  Main(string[] args) cil managed
        {
        .entrypoint
        // Code size       38 (0x26)
        .maxstack  8
        IL_0000:  call       string [mscorlib]System.Console::ReadLine()
        IL_0005:  call       int32 [mscorlib]System.Int32::Parse(string)
        IL_000a:  pop
        IL_000b:  ldstr      "Hi There"
        IL_0010:  call       void [mscorlib]System.Console::WriteLine(string)
        IL_0015:  ldstr      "Done"
        IL_001a:  call       void [mscorlib]System.Console::WriteLine(string)
        IL_001f:  call       string [mscorlib]System.Console::ReadLine()
        IL_0024:  pop
        IL_0025:  ret
        } // end of method Program::Main

            .method private hidebysig static void  Main(string[] args) cil managed
            {
              .entrypoint
              // Code size       45 (0x2d)
              .maxstack  1
              .locals init ([0] int32 result,
                       [1] bool CS$4$0000)
              IL_0000:  nop
              IL_0001:  call       string [mscorlib]System.Console::ReadLine()
              IL_0006:  call       int32 [mscorlib]System.Int32::Parse(string)
              IL_000b:  stloc.0
              IL_000c:  ldc.i4.0
              IL_000d:  stloc.1
              IL_000e:  nop
              IL_000f:  ldstr      "Hi There"
              IL_0014:  call       void [mscorlib]System.Console::WriteLine(string)
              IL_0019:  nop
              IL_001a:  nop
              IL_001b:  ldstr      "Done"
              IL_0020:  call       void [mscorlib]System.Console::WriteLine(string)
              IL_0025:  nop
              IL_0026:  call       string [mscorlib]System.Console::ReadLine()
              IL_002b:  pop
              IL_002c:  ret
            } // end of method Program::Main
+4
3

, , Fat Tiny ( EC-335 Partition II, 25.4.3 25.4.2). )

12 , . IL 63 , , .maxstack 8.

, , , .maxstack 8, .maxstack.

+7

, , #, Roslyn . Release, , :

static void Foo() {
    // Nothing
}

:

.method private hidebysig static void  Foo() cil managed
{
  // Code size       2 (0x2)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ret
} // end of method Program::Foo

.maxstack 8 Debug, Release.

, # , result , . WriteLine Console.WriteLine(result), , .maxstack 1, . , /optimize, , Release - .

, , .maxstack 0 .maxstack 8. , Q + D, , . , , , . . , , , . , , , .

, jitters 16 , ++ new, .maxstack , 16.

: . Q + D, - IL . . , . CLR 8, .

+3

, , . , , ldloc stloc s. , , JIT- (, IL - , x86). , , .

, maxstack - - . , . , - , , , , , , , t . , , ( , , ).

0
source

Source: https://habr.com/ru/post/1659709/


All Articles