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

Use divide and conquer in to_radix_digits #316

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ fn to_str_radix_10_2(b: &mut Bencher) {
to_str_radix_bench(b, 10, 10009);
}

#[bench]
fn to_str_radix_10_3(b: &mut Bencher) {
to_str_radix_bench(b, 10, 100009);
}

#[bench]
fn to_str_radix_10_4(b: &mut Bencher) {
to_str_radix_bench(b, 10, 1000009);
}

#[bench]
fn to_str_radix_16(b: &mut Bencher) {
to_str_radix_bench(b, 16, 1009);
Expand Down
92 changes: 66 additions & 26 deletions src/biguint/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This uses stdlib features higher than the MSRV
#![allow(clippy::manual_range_contains)] // 1.35

use super::{biguint_from_vec, BigUint, ToBigUint};
use super::{biguint_from_vec, BigUint, IntDigits, ToBigUint};

use super::addition::add2;
use super::division::{div_rem_digit, FAST_DIV_WIDE};
Expand Down Expand Up @@ -701,34 +701,48 @@ pub(super) fn to_radix_digits_le(u: &BigUint, radix: u32) -> Vec<u8> {
// The threshold for this was chosen by anecdotal performance measurements to
// approximate where this starts to make a noticeable difference.
if digits.data.len() >= 64 {
Copy link
Member

Choose a reason for hiding this comment

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

Did you re-evaluate this threshold at all? Notably, it's different than the one you used in to_radix_digits_le_divide_and_conquer. Maybe that does make sense since the inner part doesn't have to pay for creating big_bases, but I'm not sure.

Copy link
Author

@HKalbasi HKalbasi Dec 16, 2024

Choose a reason for hiding this comment

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

These are new results relevant for the threshold:

simple:
test 1009 bit      ... bench:       4,169.26 ns/iter (+/- 470.97)
test 2009 bit    ... bench:      14,735.97 ns/iter (+/- 1,819.63)
test 3009 bit    ... bench:      32,522.20 ns/iter (+/- 2,949.82)
test 4009 bit    ... bench:      56,441.64 ns/iter (+/- 6,354.65)
divide and conquer:
test 1009 bit       ... bench:       5,955.14 ns/iter (+/- 859.07)
test 2009 bit       ... bench:      12,731.82 ns/iter (+/- 1,780.59)
test 3009 bit       ... bench:      18,701.03 ns/iter (+/- 2,284.40)
test 4009 bit       ... bench:      27,605.41 ns/iter (+/- 5,229.87)

So probably 2000/64 ~ 32 make sense as new threshold?

since the inner part doesn't have to pay for creating big_bases, but I'm not sure

If I understand correctly, the main difference in small numbers is that the recursive algorithm loses const propagation for 10. If it wasn't the case, I'd expect some threshold near 8.

Copy link
Member

Choose a reason for hiding this comment

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

What exactly did you change for your new results?

Copy link
Author

Choose a reason for hiding this comment

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

In benchmarks? I changed them to this:


#[bench]
fn to_str_radix_10(b: &mut Bencher) {
    to_str_radix_bench(b, 10, 1009);
}

#[bench]
fn to_str_radix_10_2(b: &mut Bencher) {
    to_str_radix_bench(b, 10, 2009);
}

#[bench]
fn to_str_radix_10_3(b: &mut Bencher) {
    to_str_radix_bench(b, 10, 3009);
}

#[bench]
fn to_str_radix_10_4(b: &mut Bencher) {
    to_str_radix_bench(b, 10, 4009);
}

And I changed if digits.data.len() >= 64 { to if digits.data.len() >= 1 { and if digits.data.len() >= 1000 {.

let mut big_base = BigUint::from(base);
let mut big_power = 1usize;

// Choose a target base length near √n.
let target_len = digits.data.len().sqrt();
while big_base.data.len() < target_len {
big_base = &big_base * &big_base;
big_power *= 2;
}

// This outer loop will run approximately √n times.
while digits > big_base {
// This is still the dominating factor, with n digits divided by √n digits.
let (q, mut big_r) = digits.div_rem(&big_base);
digits = q;

// This inner loop now has O(√n²)=O(n) behavior altogether.
for _ in 0..big_power {
let (q, mut r) = div_rem_digit(big_r, base);
big_r = q;
for _ in 0..power {
res.push((r % radix) as u8);
r /= radix;
}
let mut big_bases = Vec::with_capacity(32);
big_bases.push((BigUint::from(base), power));

loop {
let (big_base, power) = big_bases.last().unwrap();
if big_base.data.len() > digits.data.len() / 2 + 1 {
break;
}
let next_big_base = big_base * big_base;
let next_power = *power * 2;
big_bases.push((next_big_base, next_power));
}

to_radix_digits_le_divide_and_conquer(
digits,
base,
power,
&big_bases,
big_bases.len() - 1,
&mut res,
radix,
);
while res.last() == Some(&0) {
res.pop();
}
return res;
}

to_radix_digits_le_small(digits, base, power, &mut res, radix);

res
}

// Extract little-endian radix digits for small numbers
#[inline(always)] // forced inline to get const-prop for radix=10
fn to_radix_digits_le_small(
mut digits: BigUint,
base: u64,
power: usize,
res: &mut Vec<u8>,
radix: u64,
) {
while digits.data.len() > 1 {
let (q, mut r) = div_rem_digit(digits, base);
for _ in 0..power {
Expand All @@ -743,8 +757,34 @@ pub(super) fn to_radix_digits_le(u: &BigUint, radix: u32) -> Vec<u8> {
res.push((r % radix) as u8);
r /= radix;
}
}

res
fn to_radix_digits_le_divide_and_conquer(
number: BigUint,
base: u64,
power: usize,
big_bases: &[(BigUint, usize)],
k: usize,
res: &mut Vec<u8>,
radix: u64,
) {
let &(ref big_base, result_len) = &big_bases[k];
if number.data.len() < 8 {
let prev_res_len = res.len();
if !number.is_zero() {
to_radix_digits_le_small(number, base, power, res, radix);
}
while res.len() < prev_res_len + result_len * 2 {
res.push(0);
}
return;
}
// number always has two digits in the big base
let (digit_1, digit_2) = number.div_rem(big_base);
assert!(&digit_1 < big_base);
assert!(&digit_2 < big_base);
to_radix_digits_le_divide_and_conquer(digit_2, base, power, big_bases, k - 1, res, radix);
to_radix_digits_le_divide_and_conquer(digit_1, base, power, big_bases, k - 1, res, radix);
}

pub(super) fn to_radix_le(u: &BigUint, radix: u32) -> Vec<u8> {
Expand Down