Skip to content

Improve performance of ilog and checked_ilog for primitive integer types #115874

Closed
@FedericoStra

Description

@FedericoStra
Contributor

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).

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Sep 15, 2023
added
I-slowIssue: Problems and improvements with respect to performance of generated code.
T-libsRelevant to the library team, which will review and decide on the PR/issue.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Sep 16, 2023
ChaiTRex

ChaiTRex commented on Sep 16, 2023

@ChaiTRex
Contributor

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

FedericoStra

FedericoStra commented on Sep 17, 2023

@FedericoStra
ContributorAuthor

@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

FedericoStra commented on Sep 17, 2023

@FedericoStra
ContributorAuthor

New PR: #115913.

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

added a commit that references this issue on Apr 22, 2024

Auto merge of rust-lang#115913 - FedericoStra:checked_ilog, r=the8472

added a commit that references this issue on Apr 22, 2024

Rollup merge of rust-lang#115913 - FedericoStra:checked_ilog, r=the8472

206e0df
added a commit that references this issue on Apr 23, 2024
added
C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
on Feb 14, 2025
FedericoStra

FedericoStra commented on May 6, 2025

@FedericoStra
ContributorAuthor

Closed by #115913.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @FedericoStra@saethlin@ChaiTRex@workingjubilee@rustbot

        Issue actions

          Improve performance of `ilog` and `checked_ilog` for primitive integer types · Issue #115874 · rust-lang/rust