Skip to content

Optimize 128-bit integer formatting#68

Merged
dtolnay merged 1 commit intodtolnay:masterfrom
jhpratt:master
Mar 20, 2026
Merged

Optimize 128-bit integer formatting#68
dtolnay merged 1 commit intodtolnay:masterfrom
jhpratt:master

Conversation

@jhpratt
Copy link
Copy Markdown
Contributor

@jhpratt jhpratt commented Mar 19, 2026

The compiler is unaware of the restricted range of the input, so it is unable to optimize out the final division and modulus. By doing this manually, we get a nontrivial performance gain.

By running the included benchmark (u128[max]/itoa) repeatedly, I see an improvement from 32.274 ns to 30.470 ns — a 5.6% gain.

The compiler is unaware of the restricted range of the input, so it is
unable to optimize out the final division and modulus. By doing this
manually, we get a nontrivial performance gain.
Copy link
Copy Markdown
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

All of this code is just copied from libcore. Please send this optimization there first, and then it can get synced here. I would prefer to diverge as little as possible.

https://github.com/rust-lang/rust/blob/fd0c901b00ee1e08a250039cdb90258603497e20/library/core/src/fmt/num.rs

@jhpratt
Copy link
Copy Markdown
Contributor Author

jhpratt commented Mar 19, 2026

Ah, I didn't realize it was a verbatim copy, just that it was largely copied. I'll do that momentarily.

Zalathar added a commit to Zalathar/rust that referenced this pull request Mar 19, 2026
…g, r=dtolnay

Optimize 128-bit integer formatting

The compiler is unaware of the restricted range of the input, so it is unable to optimize out the final division and modulus. By doing this manually, we get a nontrivial performance gain.

r? @dtolnay

(copied from dtolnay/itoa#68)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
…g, r=dtolnay

Optimize 128-bit integer formatting

The compiler is unaware of the restricted range of the input, so it is unable to optimize out the final division and modulus. By doing this manually, we get a nontrivial performance gain.

r? @dtolnay

(copied from dtolnay/itoa#68)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
…g, r=dtolnay

Optimize 128-bit integer formatting

The compiler is unaware of the restricted range of the input, so it is unable to optimize out the final division and modulus. By doing this manually, we get a nontrivial performance gain.

r? @dtolnay

(copied from dtolnay/itoa#68)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
…g, r=dtolnay

Optimize 128-bit integer formatting

The compiler is unaware of the restricted range of the input, so it is unable to optimize out the final division and modulus. By doing this manually, we get a nontrivial performance gain.

r? @dtolnay

(copied from dtolnay/itoa#68)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
…g, r=dtolnay

Optimize 128-bit integer formatting

The compiler is unaware of the restricted range of the input, so it is unable to optimize out the final division and modulus. By doing this manually, we get a nontrivial performance gain.

r? @dtolnay

(copied from dtolnay/itoa#68)
@dtolnay dtolnay merged commit 73a7c03 into dtolnay:master Mar 20, 2026
11 checks passed
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