8000 perf: improve conversion to primitive integers by DaniPopes · Pull Request #477 · recmo/uint · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

perf: improve conversion to primitive integers #477

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

Merged
merged 2 commits into from
May 29, 2025

Conversation

DaniPopes
Copy link
Contributor
@DaniPopes DaniPopes commented May 29, 2025

Make the overflow check faster, and branchless for smaller BITS.

These two functions have now identical codegen:

type T = <integer>;
pub fn u256_to(x: U256) -> Option<T> {
    T::try_from(x).ok()
}

pub fn u256_to_manual(x: U256) -> Option<T> {
    let x = x.as_limbs();
    if (x[1] | x[2] | x[3]) == 0 {
        x[0].try_into().ok()
    } else {
        None
    }
}

Whereas before that was only the case for u64/usize (64 bit unsigned).

@DaniPopes DaniPopes requested a review from prestwich as a code owner May 29, 2025 02:15
Copy link
codspeed-hq bot commented May 29, 2025

CodSpeed Performance Report

Merging #477 will degrade performances by 85.47%

Comparing DaniPopes:special-from (0e26736) with main (8a0613d)

Summary

⚡ 2 improvements
❌ 2 regressions
✅ 225 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
overflowing_shl 999.1 µs 565.7 µs +76.61%
overflowing_shr 588.6 µs 820.1 µs -28.22%
wrapping_shl 6,730 µs 928.2 µs ×7.3
wrapping_shr 140.1 µs 964.1 µs -85.47%

@DaniPopes DaniPopes force-pushed the special-from branch 2 times, most recently from 020cd2a to 64a3d13 Compare May 29, 2025 02:43
if Self::LIMBS == 2 && limbs[1] > Self::MASK {
limbs[1] %= Self::MASK;
if LIMBS == 2 && limbs[1] > Self::MASK {
limbs[1] &= Self::MASK;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

minor drive-by fix: & instead of %

Make the overflow check faster, and branchless for smaller BITS.
@prestwich prestwich enabled auto-merge May 29, 2025 13:42
@prestwich prestwich merged commit fe3ba54 into recmo:main May 29, 2025
18 checks passed
@DaniPopes DaniPopes deleted the special-from branch May 29, 2025 14:03
@DaniPopes DaniPopes mentioned this pull request May 29, 2025
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.

2 participants
0