-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Simplify 'BinaryFloatingPoint.init?<T: BinaryFloatingPoint>(exactly: T)' #33910
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
[stdlib] Simplify 'BinaryFloatingPoint.init?<T: BinaryFloatingPoint>(exactly: T)' #33910
Conversation
@swift-ci test |
/cc @troughton |
// We define exactness by equality after roundtripping; since NaN is never | ||
// equal to itself, it can never be converted exactly. | ||
if value.isNaN { return nil } | ||
|
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.
I think we can simplify the logic downstream by pulling out zero and infinity here (since both are always exact). WDYT?
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.
@stephentyrone I left it out because, when specialized, the case of Float-to-Double would be a single 'isNaN' check followed by conversion. Checking for finite nonzero values would have to be written separately anyway, since it wouldn't return nil; this is what happens here already. The only difference is whether it can be short-circuited or not when the concrete types are known; the logic doesn't otherwise get any simpler, and I'd like to be able to short circuit that check when it's not necessary.
@swift-ci benchmark |
Performance: -O
Code size: -O
Performance: -Osize
Code size: -Osize
Performance: -Onone
Code size: -swiftlibsHow to read the dataThe tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.If you see any unexpected regressions, you should consider fixing the Noise: Sometimes the performance results (not code size!) contain false Hardware Overview
|
@stephentyrone Are you good with merging this? |
Hey @stephentyrone, just another ping. If no objections, I'll go ahead and get this one merged. Thanks! |
Ship it. |
This PR replaces an unconditional invocation of the generic
BinaryFloatingPoint._convert
in the default implementation ofBinaryFloatingPoint.init?<T: BinaryFloatingPoint>(exactly: T)
with a more thoughtful alternative to take advantage of fast paths introduced in #33803 and #33826.Specifically, the logic for determining if a value can be exactly represented is extracted from
BinaryFloatingPoint._convert
to enable early exits returningnil
. If the value can be represented exactly, then we invokeBinaryFloatingPoint.init<T: BinaryFloatingPoint>(T)
, which has the aforementioned fast paths.A test is updated to correspond with this change.