Skip to content

Commit 5b6a464

Browse files
committed
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.
1 parent a5cc17a commit 5b6a464

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/libstd/io/cursor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use io::prelude::*;
1313

1414
use cmp;
1515
use io::{self, SeekFrom, Error, ErrorKind};
16-
use iter::repeat;
1716
use slice;
1817

1918
/// 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>> {
143142
// currently are
144143
let pos = self.position();
145144
let amt = pos.saturating_sub(self.inner.len() as u64);
146-
self.inner.extend(repeat(0).take(amt as usize));
145+
// use `resize` so that the zero filling is as efficient as possible
146+
let len = self.inner.len();
147+
self.inner.resize(len + amt as usize, 0);
147148

148149
// Figure out what bytes will be used to overwrite what's currently
149150
// there (left), and what will be appended on the end (right)

0 commit comments

Comments
 (0)