-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C# and JIT optimizers do not eliminate local variables #6542
Comments
VS2010? Roslyn is >= VS2015. Please confirm you can still repro and please post the IL that you think is incorrectly generated. |
I reported this same issue on Microsoft Connected back in the day. It was eventually deleted without ever being marked as resolved, so I took that as a good sign that the issue still exists. I can double check though. |
Retested with VS2015. Same behavior as on VS2010. The relevant IL follows. Single line:
Multiple lines:
|
Note that the verbose IL in the multiple line case is not necessarily incorrect, since the C# compiler could rely on the JIT compiler to optimize out |
@breyed Yup, that IL looks fine to me -- it should get picked up by the JIT. I can't reproduce your performance issue on .NET 4.6, though -- it looks like the second one is sometimes faster. |
Analyzing performance gets complicated because code alignment is an issue (and a separate optimization opportunity!). See the comment at the start of Fortunately, looking at the machine code is straightforward. At the end of the day, if it isn't identical with or without the local variable, there's a problem (makes for easy unit testing :-). |
@breyed So have you verified that the machine code is different on 4.6 between the two assemblies? |
Commenting out the first two lines of this for loop and uncommenting the third results in a 42% speedup:
Behind the timing is vastly different assembly code: 13 vs. 7 instructions in the loop. The platform is Windows 7 running .NET 4.0 x64. Code optimization is enabled, and the test app was run outside VS2010.
Repro project
StackOverflow question on this topic
The text was updated successfully, but these errors were encountered: