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

Add a special case for single-digit divisors #42

Merged
merged 2 commits into from
May 14, 2018

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented May 14, 2018

It was pointed out in the blog post Big Integers in Zig that we don't
have a special case in num-bigint for single-digit divisors. While
you can already get this optimization by dividing directly by u32,
it's easy to make small BigUint divisors work like this too.

$ cargo benchcmp baseline single-div
 name                   baseline ns/iter  single-div ns/iter  diff ns/iter   diff %  speedup
 factorial_div_biguint  5,638,353         1,005,488             -4,632,865  -82.17%   x 5.61

BigInt will also gain from this, since it uses BigUint division
internally.

Running zig-bn's facdiv-rs shows a nice improvement too. My i7-7700K
with Rust 1.26 goes from 4.15 seconds to just 0.65 -- a 6.38x speedup!

@cuviper
Copy link
Member Author

cuviper commented May 14, 2018

Note, this is on num-bigint-0.1.x rather than master, so I can publish it sooner for 0.1 users. I'll do a followup to merge this into master.

cuviper added 2 commits May 14, 2018 11:24
It was pointed out in the blog post [Big Integers in Zig] that we don't
have a special case in `num-bigint` for single-digit divisors.  While
you can already get this optimization by dividing directly by `u32`,
it's easy to make small `BigUint` divisors work like this too.

    $ cargo benchcmp baseline single-div
     name                   baseline ns/iter  single-div ns/iter  diff ns/iter   diff %  speedup
     factorial_div_biguint  5,638,353         1,005,488             -4,632,865  -82.17%   x 5.61

`BigInt` will also gain from this, since it uses `BigUint` division
internally.

Running [zig-bn's facdiv-rs] shows a nice improvement too.  My i7-7700K
with Rust 1.26 goes from 4.15 seconds to just 0.65 -- a 6.38x speedup!

[Big Integers in Zig]: https://tiehuis.github.io/big-integers-in-zig#division-test-single-limb
[zig-bn's facdiv-rs]: https://github.com/tiehuis/zig-bn/tree/master/bench/facdiv/crate-facdiv-rs
@cuviper
Copy link
Member Author

cuviper commented May 14, 2018

bors r+

bors bot added a commit that referenced this pull request May 14, 2018
42: Add a special case for single-digit divisors r=cuviper a=cuviper

It was pointed out in the blog post [Big Integers in Zig] that we don't
have a special case in `num-bigint` for single-digit divisors.  While
you can already get this optimization by dividing directly by `u32`,
it's easy to make small `BigUint` divisors work like this too.

    $ cargo benchcmp baseline single-div
     name                   baseline ns/iter  single-div ns/iter  diff ns/iter   diff %  speedup
     factorial_div_biguint  5,638,353         1,005,488             -4,632,865  -82.17%   x 5.61

`BigInt` will also gain from this, since it uses `BigUint` division
internally.

Running [zig-bn's facdiv-rs] shows a nice improvement too.  My i7-7700K
with Rust 1.26 goes from 4.15 seconds to just 0.65 -- a 6.38x speedup!

[Big Integers in Zig]: https://tiehuis.github.io/big-integers-in-zig#division-test-single-limb
[zig-bn's facdiv-rs]: https://github.com/tiehuis/zig-bn/tree/master/bench/facdiv/crate-facdiv-rs


Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors
Copy link
Contributor

bors bot commented May 14, 2018

Build succeeded

@bors bors bot merged commit 10e00ff into rust-num:num-bigint-0.1.x May 14, 2018
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.

1 participant