Skip to content

Commit

Permalink
Rollup merge of rust-lang#102811 - the8472:bufread-memset, r=m-ou-se
Browse files Browse the repository at this point in the history
Use memset to initialize readbuf

The write loop was found to be slow in rust-lang#102727

The proper fix is in rust-lang#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.
  • Loading branch information
Dylan-DPC authored Oct 12, 2022
2 parents d8091f8 + b9e4a1c commit 658169b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions library/std/src/io/readbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#[cfg(test)]
mod tests;

use crate::cmp;
use crate::fmt::{self, Debug, Formatter};
use crate::io::{Result, Write};
use crate::mem::{self, MaybeUninit};
use crate::{cmp, ptr};

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

Expand Down

0 comments on commit 658169b

Please sign in to comment.