Skip to content

Optimize 128-bit integer formatting#154077

Open
jhpratt wants to merge 1 commit intorust-lang:mainfrom
jhpratt:optimize-128-bit-formatting
Open

Optimize 128-bit integer formatting#154077
jhpratt wants to merge 1 commit intorust-lang:mainfrom
jhpratt:optimize-128-bit-formatting

Conversation

@jhpratt
Copy link
Member

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

r? @dtolnay

(copied from dtolnay/itoa#68)

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.
@jhpratt jhpratt added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Mar 19, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 19, 2026

Some changes occurred in integer formatting

cc @tgross35

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 19, 2026
Copy link
Member

@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!

I confirmed that the only 2 callers pass a value which is the remainder part of div_rem_1e16, which is always less than 1016.

let (quot_1e16, mod_1e16) = div_rem_1e16(self);
let (mut remain, mut offset) = if quot_1e16 == 0 {
(mod_1e16, U128_MAX_DEC_N)
} else {
// Write digits at buf[23..39].
enc_16lsd::<{ U128_MAX_DEC_N - 16 }>(buf, mod_1e16);
// Take another 16 decimals.
let (quot2, mod2) = div_rem_1e16(quot_1e16);
if quot2 == 0 {
(mod2, U128_MAX_DEC_N - 16)
} else {
// Write digits at buf[7..23].
enc_16lsd::<{ U128_MAX_DEC_N - 32 }>(buf, mod2);

Optional suggestion: debug_assert!(n < 1_0000_0000_0000_0000) inside enc_16lsd.

View changes since this review

@dtolnay
Copy link
Member

dtolnay commented Mar 19, 2026

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 19, 2026

📌 Commit ebb1f47 has been approved by dtolnay

It is now in the queue for this repository.

@rust-bors rust-bors bot 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 Mar 19, 2026
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)
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #152870 (Do not enable split debuginfo for windows-gnu)
 - #153333 (Fix bootstrap rust build failure for vxworks)
 - #153681 (ci: add runners for vanilla LLVM 22)
 - #153824 (Add `Wake` diagnostic item for `alloc::task::Wake`)
 - #154077 (Optimize 128-bit integer formatting)
 - #154078 (`define_callbacks` tweaks)
 - #151905 (Split the `dec2flt::RawFloat` trait for easier reuse)
 - #153170 (Add is_disconnected functions to mpsc and mpmc channels)
 - #153804 (Derive Macro Eq: link to more detailed documentation)
 - #153974 (Point at return type when it is the source of the type expectation)
 - #154018 (simplify and remove `tests/ui/kindck` and related tests)
 - #154041 (bootstrap: Allow `--bless`ing changes to editor settings files)
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)
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.

3 participants