Skip to content

Commit

Permalink
optimized <SharedBytesBuilder as io::Write>::write_vectored
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLuq committed Jul 23, 2024
1 parent 3628609 commit ed97c12
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,43 @@ impl Write for SharedBytesBuilder {
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}

fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
fn inner(targ: &mut [core::mem::MaybeUninit<u8>], bufs: &[std::io::IoSlice<'_>]) -> usize {
let max = targ.len();
let mut targ = targ.as_mut_ptr() as *mut u8;
let mut len = 0;
for buf in bufs {
let l = buf.len().min(max - len);
if l != 0 {
unsafe {
core::ptr::copy_nonoverlapping(buf.as_ptr(), targ, l);
targ = targ.add(l);
}
len += l;
}
}
len
}
let mut len = 0;
for buf in bufs {
len += buf.len();
}
if len == 0 {
return Ok(0);
}
self.reserve(len);
let len = self.apply_unfilled(|targ| {
let len = inner(targ, bufs);
(len, len)
});
if len != 0 {
Ok(len)
} else {
return Err(std::io::Error::new(
std::io::ErrorKind::WriteZero,
"write_vectored failed to write any data as the buffer is full",
));
}
}
}

0 comments on commit ed97c12

Please sign in to comment.