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

Fix checked_pow() perf: BigInts can never overflow, so don't need checked math. #54484

Merged
merged 4 commits into from
May 24, 2024

Conversation

NHDaly
Copy link
Member

@NHDaly NHDaly commented May 15, 2024

This commit makes checked_pow(x::BigInt, y::Integer) skip directly to ^(x,y). Same for (Integer, BigInt).

This means we're again using the native C implementation for power, instead of interpreting it in julia via repeated multiplications. Thanks to @mcmcgrath13 for discovering the issue.

Before:

julia> @btime ^($(BigInt(2)), 100)
  56.134 ns (3 allocations: 80 bytes)
1267650600228229401496703205376

julia> @btime Base.Checked.checked_pow($(BigInt(2)), 100)
  326.895 ns (24 allocations: 392 bytes)
1267650600228229401496703205376

After:

julia> @btime ^($(BigInt(2)), 100)
  56.430 ns (3 allocations: 80 bytes)
1267650600228229401496703205376

julia> @btime Base.Checked.checked_pow($(BigInt(2)), 100)
  56.557 ns (3 allocations: 80 bytes)
1267650600228229401496703205376

Co-authored-by: @mcmcgrath13

Make checked_pow(::BigInt, ::Integer) skip directly to ^().

Co-authored-by: @mcmcgrath13
Copy link
Contributor

@mcmcgrath13 mcmcgrath13 left a comment

Choose a reason for hiding this comment

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

thanks! nice test ❤️

base/checked.jl Outdated Show resolved Hide resolved
@nsajko nsajko added performance Must go faster domain:maths Mathematical functions domain:bignums BigInt and BigFloat labels May 16, 2024
@NHDaly NHDaly added the status:merge me PR is reviewed. Merge when all tests are passing label May 22, 2024
@fingolfin fingolfin merged commit 981e454 into master May 24, 2024
8 checks passed
@fingolfin fingolfin deleted the nhd-checked_pow-bigint branch May 24, 2024 23:46
@DilumAluthge DilumAluthge removed the status:merge me PR is reviewed. Merge when all tests are passing label May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:bignums BigInt and BigFloat domain:maths Mathematical functions performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants