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.
This commit is contained in:
Dylan DPC 2022-10-12 11:11:25 +05:30 committed by GitHub
commit 658169b7db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.
///
@ -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();