Skip to content

Integer to_string is slow #135543

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
jh05013 opened this issue Jan 15, 2025 · 4 comments
Open

Integer to_string is slow #135543

jh05013 opened this issue Jan 15, 2025 · 4 comments
Labels
A-fmt Area: `core::fmt` C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such

Comments

@jh05013
Copy link

jh05013 commented Jan 15, 2025

On my machine, the following code runs in 3 seconds in stable version, release build:

// (EDIT: made it compile)
fn main() {
    for i in 0..100000000u32 {
        let s = i.to_string();
        assert!(s.len() > 0);
    }
}

whereas the C++ counterpart runs in 1.2 seconds with -O2:

#include <string>
#include <cassert>
int main() {
    for(unsigned int i=0; i<100000000; i++){
        std::string s = std::to_string(i);
        assert(s.size() > 0);
    }
}

I've found that most of the time loss comes from passing &str through a formatter instead of directly memcpying; replacing to_string with _fmt with .to_owned() at the end speeds it up to around 1.6s.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 15, 2025
@clubby789 clubby789 added C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 15, 2025
@clubby789
Copy link
Contributor

FWIW, my understanding is that formatting goes through dynamic dispatch, as a deliberate choice to reduce the binary size bloat from inlining all formatting

@workingjubilee

This comment has been minimized.

@jfrimmel
Copy link
Contributor

You can try the itoa-crate, which removes some overhead mentioned in the first comment.

@GuillaumeGomez
Copy link
Member

To be noted that there is work in progress to improve this situation. Already two PRs were merged:

Hopefully more will follow soon. :)

@lolbinarycat lolbinarycat added the A-fmt Area: `core::fmt` label Jan 18, 2025
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 30, 2025
…string, r=<try>

Optimize `ToString` implementation for integers

Part of rust-lang#135543.

Follow-up of rust-lang#133247 and rust-lang#128204.

Rather than writing pretty bad benchers like I did last time, `@workingjubilee:` do you have a suggestion on how to check the impact on performance for this PR? Thanks in advance!

r? `@workingjubilee`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-fmt Area: `core::fmt` C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such
Projects
None yet
Development

No branches or pull requests

7 participants