You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.maxstack 8
IL_0000: ldarga.s x
IL_0002: call instance bool valuetype [System.Private.CoreLib]System.Nullable`1<int32>::get_HasValue()
IL_0007: ldarga.s y
IL_0009: call instance bool valuetype [System.Private.CoreLib]System.Nullable`1<int32>::get_HasValue()
IL_000e: bne.un.s IL_0021
IL_0010: ldarga.s x
IL_0012: call instance !0 valuetype [System.Private.CoreLib]System.Nullable`1<int32>::GetValueOrDefault()
IL_0017: ldarga.s y
IL_0019: call instance !0 valuetype [System.Private.CoreLib]System.Nullable`1<int32>::GetValueOrDefault()
IL_001e: ceq
IL_0020: ret
IL_0021: ldc.i4.0
IL_0022: ret
then the compiler generates code that has no locals and has 3 IL instructions less. The JITed code difference is bigger - the first version is 18 instructions and the second 13.
This time the code generated is almost the same and the performance is the same. One thing I don't understand is why in this case the compiler generates 3 locals? Both GetValueOrDefault and HasValue are readonly methods and will not mutate the objects?
The text was updated successfully, but these errors were encountered:
GetValueOrDefault and HasValue were only recently (6 days ago: dotnet/runtime#1727) made readonly, so this might be a point in time issue and it would be worth checking against the latest dotnet/runtime nightly build.
Version Used: 16.4.2
If we write the following code:
The compiler generates IL that introduces two locals.
If we write the code as:
then the compiler generates code that has no locals and has 3 IL instructions less. The JITed code difference is bigger - the first version is 18 instructions and the second 13.
Here is a repro in sharplab.
Even microbenchmarking the code shows that the second version is better:
EDIT:
This is what the code looks like when the values being compared are not arguments: here.
This time the code generated is almost the same and the performance is the same. One thing I don't understand is why in this case the compiler generates 3 locals? Both GetValueOrDefault and HasValue are readonly methods and will not mutate the objects?
The text was updated successfully, but these errors were encountered: