io: Use Vec::resize in Cursor<Vec<u8>> for more efficient zero fill

Vec::resize compiles to better code than .extend(repeat(0).take(n)) does
right now.
This commit is contained in:
Ulrik Sverdrup 2015-07-08 22:52:06 +02:00
parent a5cc17adaa
commit 5b6a464358

View file

@ -13,7 +13,6 @@ use io::prelude::*;
use cmp;
use io::{self, SeekFrom, Error, ErrorKind};
use iter::repeat;
use slice;
/// A `Cursor` is a type which wraps a non-I/O object to provide a `Seek`
@ -143,7 +142,9 @@ impl Write for Cursor<Vec<u8>> {
// currently are
let pos = self.position();
let amt = pos.saturating_sub(self.inner.len() as u64);
self.inner.extend(repeat(0).take(amt as usize));
// use `resize` so that the zero filling is as efficient as possible
let len = self.inner.len();
self.inner.resize(len + amt as usize, 0);
// Figure out what bytes will be used to overwrite what's currently
// there (left), and what will be appended on the end (right)