csc.exe /optimize flag - Any adverse impact of using optimize flag? [duplicate]

StackOverflow https://stackoverflow.com/questions/23494616

  •  16-07-2023
  •  | 
  •  

Question

I am compiling a C# program through csc.exe.

I've noticed that default setting for csc.exe is to not include the optimize option. Instead you have to explicitly state it with the /optimize flag.

I would like to know if there are any potential adverse impacts to using the /optimize flag. In other words, why wouldn't I always want to use it?

I am interested in understanding exactly what the flag does "under the hood", so any link to some good documentation would be appreciated (haven't found anything on MSDN or Google yet).

Thanks

Was it helpful?

Solution

The best documentation you will find is the code of the new compiler platform 'Roslyn' you can follow the compiler flag from CompilationOptions.cs

public bool Optimize { get; protected set; }

If you click on the property you will see all references in the code.

One of the main consequences of this flag seem to be that Optimizer.Optimize is called and try to optimize the code, especially to reduce pushing things on the stack as much as possible.

var a = Foo();
var b = Bar();
Baz(a, b);

Can be written as (pseudo-IL):

Declare Locals a, b
Call Foo
a := pop stack
Call Bar
b := pop stack
push a
push b
Call Baz

But that's totally possible to optimize it to remove locals:

Call Foo
Call Bar
Call Baz

(There are obviously more complex cases)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top