Skip to content

Commit

Permalink
io: Use Vec::resize in Cursor<Vec<u8>> for more efficient zero fill
Browse files Browse the repository at this point in the history
Vec::resize compiles to better code than .extend(repeat(0).take(n)) does
right now.
  • Loading branch information
bluss committed Jul 8, 2015
1 parent a5cc17a commit 5b6a464
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/libstd/io/cursor.rs
Expand Up @@ -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`
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5b6a464

Please sign in to comment.