]> git.lizzy.rs Git - rust.git/commitdiff
io: Use Vec::resize in Cursor<Vec<u8>> for more efficient zero fill
authorUlrik Sverdrup <bluss@users.noreply.github.com>
Wed, 8 Jul 2015 20:52:06 +0000 (22:52 +0200)
committerUlrik Sverdrup <bluss@users.noreply.github.com>
Wed, 8 Jul 2015 23:18:29 +0000 (01:18 +0200)
Vec::resize compiles to better code than .extend(repeat(0).take(n)) does
right now.

src/libstd/io/cursor.rs

index 72743106abf6a24ac589e0c852b960d7159c952e..f059b24baf6a4f74fd787559a9da9af905300fa0 100644 (file)
@@ -13,7 +13,6 @@
 
 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 @@ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
         // 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)