-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Improve [U]Int128 string conversion performance. #65508
base: main
Are you sure you want to change the base?
Conversation
String to (U)Int128 conversions are 9 times faster. (U)Int128 to String conversions are 25 times faster.
stdlib/public/core/Int128.swift.gyb
Outdated
private static func times<T:FixedWidthInteger>( | ||
x: T, timesRadix radix: UInt64, toPower n: Int) -> T { | ||
// calculate the powers of the radix and store in a table | ||
if powers.isEmpty || powers.first! != radix { |
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 has an obvious toctou race condition, as well as a more subtle race condition on targets without total store ordering. I don't think that it's actually possible to use a lazily computed table of powers like this without an explicit lock. Given what the expected distribution of radices looks like, simply covering power-of-two bases with a precomputed table for base 10 would be a much more attractive solution.
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.
Thanks for the feedback! Will do a precomputed table for base 10 and powers of 2.
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, latest update should fix this race condition issue.
Fixed toctou race condition, as well as a more subtle race condition on targets without total store ordering.
Set the radix during power calculations.
String to [U]Int128 conversions are 9 times faster.
[U]Int128 to String conversions are 25 times faster.
@stephentyrone suggested my [U]Int128 changes would be better placed in the standard library.
[U]Int128 probably fits better in the standard library itself (there's already a partial implementation there to support Duration.