Skip to content
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

Optimize isqrt #2108

Merged
merged 3 commits into from
Apr 12, 2024
Merged

Optimize isqrt #2108

merged 3 commits into from
Apr 12, 2024

Conversation

chipshort
Copy link
Collaborator

@chipshort chipshort commented Apr 11, 2024

taken out of #2029
somewhat related to #1897

Benchmarked the gas costs and they look much better for big numbers, while only having a small regression for small numbers:

Uint64::isqrt 1.0 MAX MAX / 2
old 4026600 4446000 4440900
new 4045800 4182450 4207950
rel.diff. +0.5% -5.9% -5.2%
Uint128::isqrt 1.0 MAX MAX / 2
old 4065600 24899400 24076200
new 4156200 4937850 6977250
rel.diff. +2.2% -80.2% -71%
Uint256::isqrt 1.0 MAX MAX / 2
old 4145700 114904200 114196500
new 4333500 7516500 14925450
rel.diff. +4.5% -93.5% -86.9%
Uint512::isqrt 1.0 MAX MAX / 2
old 4208550 567245850 565513200
new 4555800 15074250 37877400
rel.diff. +8.2% -97.3% -93.3%

If we want to avoid the increase in the lower numbers really badly, we could try to figure out the points until which the old version is better and manually check if the number is below that threshold, but that's likely to change with different compiler versions, bnum updates, etc.

Copy link
Member

@aumetra aumetra left a comment

Choose a reason for hiding this comment

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

LGTM in general, just this small nit

packages/core/src/math/isqrt.rs Outdated Show resolved Hide resolved
Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Super nice! Thanks for the benchmarks. Let's not complicate things for small savings for small numbers.

I can't follow the exact math right now but if this is well tested and the two of you agree, this is good to go.

packages/core/src/math/isqrt.rs Show resolved Hide resolved
packages/core/src/math/isqrt.rs Outdated Show resolved Hide resolved
@chipshort chipshort requested a review from aumetra April 12, 2024 10:37
Copy link
Member

@aumetra aumetra left a comment

Choose a reason for hiding this comment

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

Looks good! Especially the solution with the Unsigned trait, very nice

@chipshort chipshort merged commit 5148df2 into main Apr 12, 2024
30 checks passed
@chipshort chipshort deleted the chipshort/isqrt-optimization branch April 12, 2024 10:59
@chipshort chipshort mentioned this pull request Apr 12, 2024
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.

None yet

3 participants