Reputation:
I had a tool that generated several Boolean values, something that looks like this:
(!(dcn.XdbDate != null)) && (dcn.XdaDate != null)
The longest is 1361 characters long. I need to copy these over to C# source files to be used in linq queries.
My question is first, does C# automatically optimize these operators? In the above example, would it be automatically converted to the below when compiled?
dcn.XdbDate == null && dcn.XdaDate != null
If not, is there some C# compiler features where I can write a program, feed it the non-optimized ones and get optimized ones?
Edit:
Most of the generated Boolean operations are in the form of
xxx1 = condition ? value11 : value12,
xxx2 = condition ? value21 : value22,
etc...
The problem is with the condition
part being not optimized.
Upvotes: 6
Views: 465
Reputation: 6159
This is not an easy answer.. but let's try anyway.
In a more "generic" approach you are asking how to know what is being/going to be optimized and what is not? - of course as a responsible developer you have to assume that nothing will be optimized for you and writing an optimized code is fully under your responsibility.
Having that said, we of course know that some things do get optimized automatically.
Optimization may occur in 3 distinguished occasions:
During the build of the Debug version.
During the build of the Release version.
By executing the NGEN utility on an already built file.
The optimizations can be described as an onion - the outer shell will always include the inner shells and will add something of its own.
So,
What type of optimizations can occur in debug?
Only optimizations that will not change the code-separations for the user who debug.
In example: if(a==!true && a==false)
a==!true
is identical to a==false
- so having just 1 of those is a good idea - but if such an optimization will take place the developer will not be able to step by step the expression as it could before the optimization - so such optimization will not occur in debug version.
However.. a==!true
- only this autonomous part can be replaced to a==false
underneath without breaking the expression separation - meaning it will be ok from the compiler point of view to replace:
if(a==!true && a==false)
to a perhaps faster instructions set which actually represent
if(a==false && a==false)
as expression separation will not be hit and debugging or stepping into will not change.
What type of optimizations can occur in release?
If we get back to the same example - in release - the expression if(a==!true && a==false)
can be optimized to if(a==false)
as it is predefined that a release is not meant for debugging therefore the compiler has nothing to worry about when doing such an optimization.
What type of optimization can occur in NGEN?
This is an entirely different vertical...
NGEN can be made for debug or for release - meaning same rules apply just as before and this is actually being done by the JIT when you built the code. The NGEN Job is to optimize the code it finds to the native (current) machine CPU architecture. I will not cover it here - you can read all about it from here.
Keep in mind: if an expression cannot be stepped into per the parts of it - it is already optimized.
Upvotes: 1