-
Notifications
You must be signed in to change notification settings - Fork 5k
Simplify conditional operator #112074
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
Simplify conditional operator #112074
Conversation
They were originally written like that to mitigate some CQ issue. I believe they have been fixed in RyuJIT now but I'm not sure about mono. |
It seems base and diff are compiled to identical IL (sharplab) |
@@ -314,12 +314,12 @@ internal static object DefaultToType(IConvertible value, Type targetType, IForma | |||
// Conversions to Boolean | |||
public static bool ToBoolean([NotNullWhen(true)] object? value) | |||
{ | |||
return value == null ? false : ((IConvertible)value).ToBoolean(null); | |||
return value != null && ((IConvertible)value).ToBoolean(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could be value is IConvertible convertible && convertible.ToBoolean(null)
, yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a change in behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly a change in code generation, arguably for the worse: https://csharp.godbolt.org/z/nrM7df3YP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What makes you say it's "worse"?
The code is nearly identical, just without the early exit for null
.
src/libraries/System.Private.CoreLib/src/System/Collections/Hashtable.cs
Show resolved
Hide resolved
Tagging subscribers to this area: @dotnet/area-meta |
Simplify conditional operator in
src/coreclr/System.Private.CoreLib/System.Private.CoreLib.sln