Skip to content

Commit 658169b

Browse files
authored
Rollup merge of #102811 - the8472:bufread-memset, r=m-ou-se
Use memset to initialize readbuf The write loop was found to be slow in #102727 The proper fix is in #102760 but this might still help debug builds and code running under miri by using the write_bytes intrinsic instead of writing one byte at a time.
2 parents d8091f8 + b9e4a1c commit 658169b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

library/std/src/io/readbuf.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#[cfg(test)]
44
mod tests;
55

6-
use crate::cmp;
76
use crate::fmt::{self, Debug, Formatter};
87
use crate::io::{Result, Write};
98
use crate::mem::{self, MaybeUninit};
9+
use crate::{cmp, ptr};
1010

1111
/// A borrowed byte buffer which is incrementally filled and initialized.
1212
///
@@ -250,8 +250,11 @@ impl<'a> BorrowedCursor<'a> {
250250
/// Initializes all bytes in the cursor.
251251
#[inline]
252252
pub fn ensure_init(&mut self) -> &mut Self {
253-
for byte in self.uninit_mut() {
254-
byte.write(0);
253+
let uninit = self.uninit_mut();
254+
// SAFETY: 0 is a valid value for MaybeUninit<u8> and the length matches the allocation
255+
// since it is comes from a slice reference.
256+
unsafe {
257+
ptr::write_bytes(uninit.as_mut_ptr(), 0, uninit.len());
255258
}
256259
self.buf.init = self.buf.capacity();
257260

0 commit comments

Comments
 (0)