Skip to content

Use a distinct ToString implementation for u128 and i128 #142294

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Jun 10, 2025

Part of #135543.

Follow-up of #136264.

When working on #142098, I realized that i128 and u128 could also benefit from a distinct ToString implementation so here it.

The last commit is just me realizing that I forgot to add the format tests for usize and isize.

Here is the bench comparison:

bench name last nightly with this PR diff
bench_i128 29.25 ns/iter (+/- 0.66) 17.52 ns/iter (+/- 0.7) -40.1%
bench_u128 34.06 ns/iter (+/- 0.21) 16.1 ns/iter (+/- 0.6) -52.7%

I used this code to test:

#![feature(test)]

extern crate test;

use test::{Bencher, black_box};

#[inline(always)]
fn convert_to_string<T: ToString>(n: T) -> String {
    n.to_string()
}

macro_rules! decl_benches {
    ($($name:ident: $ty:ident,)+) => {
        $(
	    #[bench]
            fn $name(c: &mut Bencher) {
                c.iter(|| convert_to_string(black_box({ let nb: $ty = 20; nb })));
            }
	)+
    }
}

decl_benches! {
    bench_u128: u128,
    bench_i128: i128,
}

@rustbot
Copy link
Collaborator

rustbot commented Jun 10, 2025

r? @workingjubilee

rustbot has assigned @workingjubilee.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 10, 2025
@bors
Copy link
Collaborator

bors commented Jun 12, 2025

☔ The latest upstream changes (presumably #136594) made this pull request unmergeable. Please resolve the merge conflicts.

@GuillaumeGomez GuillaumeGomez force-pushed the specialize-tostring-on-128-integers branch from b5b6654 to 53dc659 Compare June 13, 2025 12:11
@GuillaumeGomez
Copy link
Member Author

Fixed merge conflicts.

@workingjubilee workingjubilee changed the title Specialize ToString implementation on u128 and i128 Use a distinct ToString implementation for u128 and i128 Jun 13, 2025
@workingjubilee
Copy link
Member

Having done code review of things that use Rust's feature(min_specialization), I now consider "specialization" a dirty word.

@GuillaumeGomez
Copy link
Member Author

Noted, thanks for the title update then. :)

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

If you've tested locally, is there any measurable perf/size impact here?

Comment on lines 581 to 583
// This is not a typo, we use the maximum number of digits of `u128`, hence why we use
// `u128::MAX`.
const MAX_DEC_N: usize = u128::MAX.ilog(10) as usize + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe pop this constant out so it can be reused between u128 and i128?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@GuillaumeGomez GuillaumeGomez force-pushed the specialize-tostring-on-128-integers branch from 53dc659 to 9b09948 Compare June 16, 2025 09:54
@GuillaumeGomez
Copy link
Member Author

@tgross35 I added the comparison in the first comment with the code I use to get the performance comparison.

@tgross35
Copy link
Contributor

Thanks!

I don't think this needs a pre-merge perf run since we didn't see any perf-visible impact in #136594, but it wouldn't hurt to get one after so

@bors r+ rollup=never

Fyi @pascaldekloe since you did the last round of perf boosts for these methods

@bors
Copy link
Collaborator

bors commented Jun 16, 2025

📌 Commit 9b09948 has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants