Skip to content

Commit 75ba103

Browse files
Replace indexing with get_unchecked calls
1 parent f46ce66 commit 75ba103

File tree

1 file changed

+66
-20
lines changed

1 file changed

+66
-20
lines changed

library/core/src/fmt/num.rs

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ unsafe trait GenericRadix: Sized {
6969
let n = x % base; // Get the current place value.
7070
x = x / base; // Deaccumulate the number.
7171
curr -= 1;
72-
buf[curr].write(Self::digit(n.to_u8())); // Store the digit in the buffer.
72+
// SAFETY: `curr` is always between 0 and `buf`'s length.
73+
unsafe {
74+
buf.get_unchecked_mut(curr).write(Self::digit(n.to_u8()));
75+
} // Store the digit in the buffer.
7376
if x == zero {
7477
// No more digits left to accumulate.
7578
break;
@@ -81,7 +84,10 @@ unsafe trait GenericRadix: Sized {
8184
let n = zero - (x % base); // Get the current place value.
8285
x = x / base; // Deaccumulate the number.
8386
curr -= 1;
84-
buf[curr].write(Self::digit(n.to_u8())); // Store the digit in the buffer.
87+
// SAFETY: `curr` is always between 0 and `buf`'s length.
88+
unsafe {
89+
buf.get_unchecked_mut(curr).write(Self::digit(n.to_u8()));
90+
} // Store the digit in the buffer.
8591
if x == zero {
8692
// No more digits left to accumulate.
8793
break;
@@ -270,10 +276,14 @@ macro_rules! impl_Display {
270276
remain /= scale;
271277
let pair1 = (quad / 100) as usize;
272278
let pair2 = (quad % 100) as usize;
273-
buf[offset + 0].write(DEC_DIGITS_LUT[pair1 * 2 + 0]);
274-
buf[offset + 1].write(DEC_DIGITS_LUT[pair1 * 2 + 1]);
275-
buf[offset + 2].write(DEC_DIGITS_LUT[pair2 * 2 + 0]);
276-
buf[offset + 3].write(DEC_DIGITS_LUT[pair2 * 2 + 1]);
279+
// SAFETY: `offset` is always between 0 and `buf`'s length.
280+
unsafe { buf.get_unchecked_mut(offset + 0).write(DEC_DIGITS_LUT[pair1 * 2 + 0]); }
281+
// SAFETY: `offset` is always between 0 and `buf`'s length.
282+
unsafe { buf.get_unchecked_mut(offset + 1).write(DEC_DIGITS_LUT[pair1 * 2 + 1]); }
283+
// SAFETY: `offset` is always between 0 and `buf`'s length.
284+
unsafe { buf.get_unchecked_mut(offset + 2).write(DEC_DIGITS_LUT[pair2 * 2 + 0]); }
285+
// SAFETY: `offset` is always between 0 and `buf`'s length.
286+
unsafe { buf.get_unchecked_mut(offset + 3).write(DEC_DIGITS_LUT[pair2 * 2 + 1]); }
277287
}
278288

279289
// Format per two digits from the lookup table.
@@ -288,8 +298,10 @@ macro_rules! impl_Display {
288298

289299
let pair = (remain % 100) as usize;
290300
remain /= 100;
291-
buf[offset + 0].write(DEC_DIGITS_LUT[pair * 2 + 0]);
292-
buf[offset + 1].write(DEC_DIGITS_LUT[pair * 2 + 1]);
301+
// SAFETY: `offset` is always between 0 and `buf`'s length.
302+
unsafe { buf.get_unchecked_mut(offset + 0).write(DEC_DIGITS_LUT[pair * 2 + 0]); }
303+
// SAFETY: `offset` is always between 0 and `buf`'s length.
304+
unsafe { buf.get_unchecked_mut(offset + 1).write(DEC_DIGITS_LUT[pair * 2 + 1]); }
293305
}
294306

295307
// Format the last remaining digit, if any.
@@ -305,7 +317,8 @@ macro_rules! impl_Display {
305317
// Either the compiler sees that remain < 10, or it prevents
306318
// a boundary check up next.
307319
let last = (remain & 15) as usize;
308-
buf[offset].write(DEC_DIGITS_LUT[last * 2 + 1]);
320+
// SAFETY: `offset` is always between 0 and `buf`'s length.
321+
unsafe { buf.get_unchecked_mut(offset).write(DEC_DIGITS_LUT[last * 2 + 1]); }
309322
// not used: remain = 0;
310323
}
311324

@@ -639,10 +652,22 @@ impl u128 {
639652
remain /= 1_00_00;
640653
let pair1 = (quad / 100) as usize;
641654
let pair2 = (quad % 100) as usize;
642-
buf[offset + 0].write(DEC_DIGITS_LUT[pair1 * 2 + 0]);
643-
buf[offset + 1].write(DEC_DIGITS_LUT[pair1 * 2 + 1]);
644-
buf[offset + 2].write(DEC_DIGITS_LUT[pair2 * 2 + 0]);
645-
buf[offset + 3].write(DEC_DIGITS_LUT[pair2 * 2 + 1]);
655+
// SAFETY: `offset` is always between 0 and `buf`'s length.
656+
unsafe {
657+
buf.get_unchecked_mut(offset + 0).write(DEC_DIGITS_LUT[pair1 * 2 + 0]);
658+
}
659+
// SAFETY: `offset` is always between 0 and `buf`'s length.
660+
unsafe {
661+
buf.get_unchecked_mut(offset + 1).write(DEC_DIGITS_LUT[pair1 * 2 + 1]);
662+
}
663+
// SAFETY: `offset` is always between 0 and `buf`'s length.
664+
unsafe {
665+
buf.get_unchecked_mut(offset + 2).write(DEC_DIGITS_LUT[pair2 * 2 + 0]);
666+
}
667+
// SAFETY: `offset` is always between 0 and `buf`'s length.
668+
unsafe {
669+
buf.get_unchecked_mut(offset + 3).write(DEC_DIGITS_LUT[pair2 * 2 + 1]);
670+
}
646671
}
647672

648673
// Format per two digits from the lookup table.
@@ -657,8 +682,14 @@ impl u128 {
657682

658683
let pair = (remain % 100) as usize;
659684
remain /= 100;
660-
buf[offset + 0].write(DEC_DIGITS_LUT[pair * 2 + 0]);
661-
buf[offset + 1].write(DEC_DIGITS_LUT[pair * 2 + 1]);
685+
// SAFETY: `offset` is always between 0 and `buf`'s length.
686+
unsafe {
687+
buf.get_unchecked_mut(offset + 0).write(DEC_DIGITS_LUT[pair * 2 + 0]);
688+
}
689+
// SAFETY: `offset` is always between 0 and `buf`'s length.
690+
unsafe {
691+
buf.get_unchecked_mut(offset + 1).write(DEC_DIGITS_LUT[pair * 2 + 1]);
692+
}
662693
}
663694

664695
// Format the last remaining digit, if any.
@@ -674,7 +705,10 @@ impl u128 {
674705
// Either the compiler sees that remain < 10, or it prevents
675706
// a boundary check up next.
676707
let last = (remain & 15) as usize;
677-
buf[offset].write(DEC_DIGITS_LUT[last * 2 + 1]);
708+
// SAFETY: `offset` is always between 0 and `buf`'s length.
709+
unsafe {
710+
buf.get_unchecked_mut(offset).write(DEC_DIGITS_LUT[last * 2 + 1]);
711+
}
678712
// not used: remain = 0;
679713
}
680714

@@ -703,10 +737,22 @@ fn enc_16lsd<const OFFSET: usize>(buf: &mut [MaybeUninit<u8>], n: u64) {
703737
remain /= 1_00_00;
704738
let pair1 = (quad / 100) as usize;
705739
let pair2 = (quad % 100) as usize;
706-
buf[quad_index * 4 + OFFSET + 0].write(DEC_DIGITS_LUT[pair1 * 2 + 0]);
707-
buf[quad_index * 4 + OFFSET + 1].write(DEC_DIGITS_LUT[pair1 * 2 + 1]);
708-
buf[quad_index * 4 + OFFSET + 2].write(DEC_DIGITS_LUT[pair2 * 2 + 0]);
709-
buf[quad_index * 4 + OFFSET + 3].write(DEC_DIGITS_LUT[pair2 * 2 + 1]);
740+
// SAFETY: `quad_index * 4 + OFFSET` is always between 0 and `buf`'s length.
741+
unsafe {
742+
buf.get_unchecked_mut(quad_index * 4 + OFFSET + 0).write(DEC_DIGITS_LUT[pair1 * 2 + 0]);
743+
}
744+
// SAFETY: `quad_index * 4 + OFFSET` is always between 0 and `buf`'s length.
745+
unsafe {
746+
buf.get_unchecked_mut(quad_index * 4 + OFFSET + 1).write(DEC_DIGITS_LUT[pair1 * 2 + 1]);
747+
}
748+
// SAFETY: `quad_index * 4 + OFFSET` is always between 0 and `buf`'s length.
749+
unsafe {
750+
buf.get_unchecked_mut(quad_index * 4 + OFFSET + 2).write(DEC_DIGITS_LUT[pair2 * 2 + 0]);
751+
}
752+
// SAFETY: `quad_index * 4 + OFFSET` is always between 0 and `buf`'s length.
753+
unsafe {
754+
buf.get_unchecked_mut(quad_index * 4 + OFFSET + 3).write(DEC_DIGITS_LUT[pair2 * 2 + 1]);
755+
}
710756
}
711757
}
712758

0 commit comments

Comments
 (0)