Skip to content

Opt: Generalize the RuntimeLong.toString optimization to any base. #5192

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

Merged
merged 1 commit into from
Jun 8, 2025

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented Jun 6, 2025

With some more care to the choice of divisors (radixPowLength) we use in jl.Long.to{,Unsigned}String, we can generalize the optimization of RuntimeLong.toString to all the possible bases.

@sjrd sjrd requested a review from gzm0 June 6, 2025 09:09
@sjrd sjrd force-pushed the opt-long-tostring-base branch from 27f4e7a to 91547ff Compare June 6, 2025 13:27
Copy link
Contributor

@gzm0 gzm0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just some details on the comments.

* It holds that 2^30 >= radixPowLength >= 2^30 / 36 > 2^24.
*
* Therefore, (2^64 - 1) / radixPowLength < 2^(64-24) = 2^40, which
* confortably fits in a `Double`. `toUnsignedStringLarge` relies on that
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* confortably fits in a `Double`. `toUnsignedStringLarge` relies on that
* comfortably fits in a `Double`. `toUnsignedStringLarge` relies on that

* - chunkLength is then log_radix(radixPowLength).
* - paddingZeros is a string with exactly chunkLength '0's.
* - overflowBarrier is divideUnsigned(-1L, radixPowLength) so that we
* can test whether someValue * radixPowLength will overflow.
*
* It holds that 2^30 >= radixPowLength >= 2^30 / 36 > 2^24.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* It holds that 2^30 >= radixPowLength >= 2^30 / 36 > 2^24.
* It holds that 2^30 >= radixPowLength > 2^30 / maxRadix = 2^30 / 36 > 2^24.

With some more care to the choice of divisors (`radixPowLength`)
we use in `jl.Long.to{,Unsigned}String`, we can generalize the
optimization of `RuntimeLong.toString` to all the possible bases.
@sjrd sjrd force-pushed the opt-long-tostring-base branch from 91547ff to 2539e59 Compare June 8, 2025 07:13
@sjrd sjrd enabled auto-merge June 8, 2025 07:14
@sjrd sjrd merged commit d9184b9 into scala-js:main Jun 8, 2025
3 checks passed
@sjrd sjrd deleted the opt-long-tostring-base branch June 8, 2025 20:53
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.

2 participants