Skip to content

Commit ebb1f47

Browse files
committed
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.
1 parent fd0c901 commit ebb1f47

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

library/core/src/fmt/num.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ fn enc_16lsd<const OFFSET: usize>(buf: &mut [MaybeUninit<u8>], n: u64) {
823823
let mut remain = n;
824824

825825
// Format per four digits from the lookup table.
826-
for quad_index in (0..4).rev() {
826+
for quad_index in (1..4).rev() {
827827
// pull two pairs
828828
let quad = remain % 1_00_00;
829829
remain /= 1_00_00;
@@ -834,6 +834,14 @@ fn enc_16lsd<const OFFSET: usize>(buf: &mut [MaybeUninit<u8>], n: u64) {
834834
buf[quad_index * 4 + OFFSET + 2].write(DECIMAL_PAIRS[pair2 * 2 + 0]);
835835
buf[quad_index * 4 + OFFSET + 3].write(DECIMAL_PAIRS[pair2 * 2 + 1]);
836836
}
837+
838+
// final two pairs
839+
let pair1 = (remain / 100) as usize;
840+
let pair2 = (remain % 100) as usize;
841+
buf[OFFSET + 0].write(DECIMAL_PAIRS[pair1 * 2 + 0]);
842+
buf[OFFSET + 1].write(DECIMAL_PAIRS[pair1 * 2 + 1]);
843+
buf[OFFSET + 2].write(DECIMAL_PAIRS[pair2 * 2 + 0]);
844+
buf[OFFSET + 3].write(DECIMAL_PAIRS[pair2 * 2 + 1]);
837845
}
838846

839847
/// Euclidean division plus remainder with constant 1E16 basically consumes 16

0 commit comments

Comments
 (0)