diff --git a/src/buffer.rs b/src/buffer.rs index 1168e2475e..2dacbc1228 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -1,5 +1,4 @@ use std::cmp; -use std::iter; use std::io::{self, Read, BufRead}; pub struct BufReader { @@ -21,7 +20,9 @@ impl BufReader { #[inline] pub fn with_capacity(rdr: R, cap: usize) -> BufReader { let mut buf = Vec::with_capacity(cap); - buf.extend(iter::repeat(0).take(cap)); + unsafe { + grow_zerofill(&mut buf, cap); + } BufReader { inner: rdr, buf: buf, @@ -71,11 +72,19 @@ impl BufReader { self.buf.reserve(cmp::min(cap * 4, MAX_BUFFER_SIZE) - cap); let new = self.buf.capacity() - self.buf.len(); trace!("reserved {}", new); - self.buf.extend(iter::repeat(0).take(new)); + unsafe { grow_zerofill(&mut self.buf, new) } } } } +#[inline] +unsafe fn grow_zerofill(buf: &mut Vec, additional: usize) { + use std::ptr; + let len = buf.len(); + buf.set_len(len + additional); + ptr::write_bytes(buf.as_mut_ptr(), 0, buf.len()); +} + impl Read for BufReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { if self.cap == self.pos && buf.len() >= self.buf.len() {