From 5b6a4643583c2b580b9a57f48dd94ba5d7824765 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Wed, 8 Jul 2015 22:52:06 +0200 Subject: [PATCH] io: Use Vec::resize in Cursor> for more efficient zero fill Vec::resize compiles to better code than .extend(repeat(0).take(n)) does right now. --- src/libstd/io/cursor.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/cursor.rs b/src/libstd/io/cursor.rs index 72743106abf6..f059b24baf6a 100644 --- a/src/libstd/io/cursor.rs +++ b/src/libstd/io/cursor.rs @@ -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> { // 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)