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

Improve performance of BinaryNumeralString #34

Merged
merged 8 commits into from
Apr 10, 2023
Merged

Improve performance of BinaryNumeralString #34

merged 8 commits into from
Apr 10, 2023

Conversation

str4d
Copy link
Owner

@str4d str4d commented Jan 18, 2023

No description provided.

This allows NumeralStrings to implement the additions and subtractions
more efficiently.

The Numeral trait is moved into the alloc module as an extension trait
on BigUint.
We don't ever want to split a numeral string arbitrarily, so this
enables trait implementors to optimise for the precise kind of split
that we do need.
Benchmark throughput increases on a Ryzen 9 5950X:
- 10 bytes: around 215%
- 100 and 1000 bytes: around 375%
We already consume `self` in `BinaryOps::{add_mod_exp, sub_mod_exp}`,
and it is guaranteed to have the same byte length as the output, so we
can reuse its `Vec<u8>` for the latter.
src/ff1.rs Outdated Show resolved Hide resolved
src/ff1.rs Outdated Show resolved Hide resolved
src/ff1.rs Outdated Show resolved Hide resolved
@daira
Copy link
Contributor

daira commented Apr 3, 2023

fixes #11

@@ -127,50 +127,46 @@ impl Radix {
}
}
Copy link
Contributor

@daira daira Apr 3, 2023

Choose a reason for hiding this comment

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

Suggestion for simpler Radix::from_u32 above this:

    fn from_u32(radix: u32) -> Result<Self, InvalidRadix> {
        // radix must be in range [2..=2^16]
        if !(2..=(1 << 16)).contains(&radix) {
            return Err(InvalidRadix(radix));
        }

        Ok(if radix.count_ones() == 1 {
            let log_radix: u32 = 31 - radix.leading_zeros();
            Radix::PowerTwo {
                radix,
                min_len: cmp::max((MIN_RADIX_2_NS_LEN + log_radix - 1) / log_radix, MIN_NS_LEN),
                log_radix: u8::try_from(log_radix).unwrap(),
            }
        } else {
            use libm::{ceil, log10};
            let min_len = ceil(LOG_10_MIN_NS_DOMAIN_SIZE / log10(f64::from(radix))) as u32;
            Radix::Any { radix, min_len }
        })
    }

with either use std::convert::TryFrom;, or edition = "2021" in Cargo.toml.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ah yes, we can indeed use u32::count_ones and u32::leading_zeros now (they were added in 1.32, seven months after I wrote fpe 0.1 for Sapling). I also bumped MSRV to 1.56 for this upcoming release, so we can use edition = "2021" as well. I'll make both of these changes in a separate PR (as it's unrelated to the changes in this one).

Copy link
Owner Author

Choose a reason for hiding this comment

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

#38

src/ff1.rs Show resolved Hide resolved
src/ff1.rs Show resolved Hide resolved
src/ff1.rs Show resolved Hide resolved
src/ff1.rs Show resolved Hide resolved
src/ff1.rs Show resolved Hide resolved
src/ff1.rs Show resolved Hide resolved
src/ff1/alloc.rs Outdated Show resolved Hide resolved
src/ff1/alloc.rs Outdated Show resolved Hide resolved
src/ff1/alloc.rs Show resolved Hide resolved
src/ff1/alloc.rs Show resolved Hide resolved
src/ff1.rs Show resolved Hide resolved
src/ff1/alloc.rs Outdated Show resolved Hide resolved
src/ff1/alloc.rs Show resolved Hide resolved
Copy link

@ebfull ebfull left a comment

Choose a reason for hiding this comment

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

ACK, no changes from the last time I reviewed this.

Copy link
Contributor

@daira daira left a comment

Choose a reason for hiding this comment

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

utACK with suggestions (I believe the unresolved ones are now just in comments).

criterion 0.4 now has an MSRV of 1.59 due to its dependencies, and pprof
has an MSRV of 1.64 due to its dependencies. This is a follow-on to
0c53a91.
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.

3 participants