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 ilog and checked_ilog for primitive integer types #115874

Open
FedericoStra opened this issue Sep 15, 2023 · 3 comments
Open
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@FedericoStra
Copy link
Contributor

FedericoStra commented Sep 15, 2023

Currently checked_ilog is implemented with iterated divisions:

pub const fn checked_ilog(self, base: Self) -> Option<u32> {
if self <= 0 || base <= 1 {
None
} else {
let mut n = 0;
let mut r = self;
// Optimization for 128 bit wide integers.
if Self::BITS == 128 {
let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
n += b;
r /= base.pow(b as u32);
}
while r >= base {
r /= base;
n += 1;
}
Some(n)
}
}

It's pretty straightforward to convert it to iterated multiplications:

fn checked_int_log(self, base:Self) -> Option<u32> {
    if self <= 0 || base <= 1 {
        None
    } else {
        let mut n = 0;
        let mut r = 1;

        // Optimization for 128 bit wide integers.
        if Self::BITS == 128 {
            let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
            n += b;
            r *= base.pow(b);
        }

        while r <= self / base {
            n += 1;
            r *= base;
        }
        Some(n)
    }
}

and this results in some decent speedups (1.5x - 7x).

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 15, 2023
@saethlin saethlin added I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 16, 2023
@ChaiTRex
Copy link
Contributor

@FedericoStra In case you didn't know, it seems that you closed the related pull request.

@FedericoStra
Copy link
Contributor Author

@FedericoStra In case you didn't know, it seems that you closed the related pull request.

Ooops... Yes I had some troubles forking and cloning the repository (possibly slow connection) and I did something wrong. Thanks for telling me!

@FedericoStra
Copy link
Contributor Author

New PR: #115913.

I'll make sure not to accidentally close the new one this time 😬

bors added a commit to rust-lang-ci/rust that referenced this issue Apr 22, 2024
checked_ilog: improve performance

Addresses rust-lang#115874.

(This PR replicates the original rust-lang#115875, which I accidentally closed by deleting my forked repository...)
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Apr 22, 2024
checked_ilog: improve performance

Addresses rust-lang#115874.

(This PR replicates the original rust-lang#115875, which I accidentally closed by deleting my forked repository...)
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 23, 2024
Rollup merge of rust-lang#115913 - FedericoStra:checked_ilog, r=the8472

checked_ilog: improve performance

Addresses rust-lang#115874.

(This PR replicates the original rust-lang#115875, which I accidentally closed by deleting my forked repository...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants