Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# optimizations and side effects

Can optimizations done by the C# compiler or the JITter have visible side effects?

One example I've though off.

var x = new Something();
A(x);
B(x);

When calling A(x) x is guaranteed to be kept alive to the end of A - because B uses the same parameter. But if B is defined as

public void B(Something x) { }

Then the B(x) can be eliminated by the optimizer and then a GC.KeepAlive(x) call might be necessary instead.

Can this optimization actually be done by the JITter?

Are there other optimizations that might have visible side effects, except stack trace changes?

like image 869
configurator Avatar asked Jan 19 '11 18:01

configurator


1 Answers

If your function B does not use the parameter x, then eliminating it and collecting x early does not have any visible side effects.

To be "visible side effects", they have to be visible to the program, not to an external tool like a debugger or object viewer.

like image 84
Zan Lynx Avatar answered Oct 12 '22 22:10

Zan Lynx