Skip to content

[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

Conversation

xwu
Copy link
Collaborator

@xwu xwu commented Sep 11, 2020

This PR replaces an unconditional invocation of the generic BinaryFloatingPoint._convert in the default implementation of BinaryFloatingPoint.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 returning nil. If the value can be represented exactly, then we invoke BinaryFloatingPoint.init<T: BinaryFloatingPoint>(T), which has the aforementioned fast paths.

A test is updated to correspond with this change.

@xwu xwu marked this pull request as ready for review September 11, 2020 14:33
@xwu xwu requested a review from stephentyrone September 11, 2020 14:33
@xwu
Copy link
Collaborator Author

xwu commented Sep 11, 2020

@swift-ci test

@xwu
Copy link
Collaborator Author

xwu commented Sep 11, 2020

/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 }

Copy link
Contributor

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?

Copy link
Collaborator Author

@xwu xwu Sep 16, 2020

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.

@xwu xwu changed the base branch from master to main September 24, 2020 03:00
@xwu
Copy link
Collaborator Author

xwu commented Sep 24, 2020

@swift-ci benchmark

@swift-ci
Copy link
Contributor

Performance: -O

Improvement OLD NEW DELTA RATIO
ConvertFloatingPoint.MockFloat64Exactly 680 5 -99.3% 135.97x
ConvertFloatingPoint.MockFloat64Exactly2 1013 16 -98.4% 63.31x
NopDeinit 9900 8800 -11.1% 1.12x

Code size: -O

Regression OLD NEW DELTA RATIO
FloatingPointConversion.o 41265 41821 +1.3% 0.99x

Performance: -Osize

Regression OLD NEW DELTA RATIO
FlattenListFlatMap 2987 4001 +33.9% 0.75x (?)
 
Improvement OLD NEW DELTA RATIO
ConvertFloatingPoint.MockFloat64Exactly 675 5 -99.3% 134.97x
ConvertFloatingPoint.MockFloat64Exactly2 1017 17 -98.3% 59.82x
NopDeinit 11000 9900 -10.0% 1.11x (?)

Code size: -Osize

Regression OLD NEW DELTA RATIO
FloatingPointConversion.o 39406 39858 +1.1% 0.99x

Performance: -Onone

Regression OLD NEW DELTA RATIO
DataAppendDataSmallToSmall 2840 3140 +10.6% 0.90x (?)
 
Improvement OLD NEW DELTA RATIO
ConvertFloatingPoint.MockFloat64Exactly2 1592 707 -55.6% 2.25x
ConvertFloatingPoint.MockFloat64Exactly 773 489 -36.7% 1.58x

Code size: -swiftlibs

How to read the data The 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
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac mini
  Model Identifier: Macmini8,1
  Processor Name: 6-Core Intel Core i7
  Processor Speed: 3.2 GHz
  Number of Processors: 1
  Total Number of Cores: 6
  L2 Cache (per Core): 256 KB
  L3 Cache: 12 MB
  Memory: 64 GB

@xwu
Copy link
Collaborator Author

xwu commented Sep 24, 2020

@stephentyrone Are you good with merging this?

@xwu
Copy link
Collaborator Author

xwu commented Oct 4, 2020

Hey @stephentyrone, just another ping. If no objections, I'll go ahead and get this one merged. Thanks!

@stephentyrone
Copy link
Contributor

Ship it.

@xwu xwu merged commit 23dc001 into swiftlang:main Oct 5, 2020
@xwu xwu deleted the exactness-in-little-things-a-wonderful-source-of-cheerfulness branch March 20, 2021 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants