Auto merge of #26904 - bluss:no-repeat, r=alexcrichton

In a followup to PR #26849, improve one more location for I/O where
we can use `Vec::resize` to ensure better performance when zeroing
buffers.

Use the `vec![elt; n]` macro everywhere we can in the tree. It replaces
`repeat(elt).take(n).collect()` which is more verbose, requires type
hints, and right now produces worse code. `vec![]` is preferable for vector
initialization.

The `vec![]` replacement touches upon one I/O path too, Stdin::read
for windows, and that should be a small improvement.

r? @alexcrichton
This commit is contained in:
bors 2015-07-09 10:36:41 +00:00
commit f11502cda8
32 changed files with 64 additions and 93 deletions

View file

@ -12,7 +12,6 @@
#![feature(rand, vec_push_all, duration, duration_span)]
use std::iter::repeat;
use std::mem::swap;
use std::env;
use std::__rand::{thread_rng, Rng};
@ -56,7 +55,7 @@ fn maybe_run_test<F>(argv: &[String], name: String, test: F) where F: FnOnce() {
}
fn shift_push() {
let mut v1 = repeat(1).take(30000).collect::<Vec<_>>();
let mut v1 = vec![1; 30000];
let mut v2 = Vec::new();
while !v1.is_empty() {
@ -70,7 +69,7 @@ fn vec_plus() {
let mut v = Vec::new();
let mut i = 0;
while i < 1500 {
let rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>();
let rv = vec![i; r.gen_range(0, i + 1)];
if r.gen() {
v.extend(rv);
} else {
@ -88,7 +87,7 @@ fn vec_append() {
let mut v = Vec::new();
let mut i = 0;
while i < 1500 {
let rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>();
let rv = vec![i; r.gen_range(0, i + 1)];
if r.gen() {
let mut t = v.clone();
t.push_all(&rv);
@ -108,7 +107,7 @@ fn vec_push_all() {
let mut v = Vec::new();
for i in 0..1500 {
let mut rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>();
let mut rv = vec![i; r.gen_range(0, i + 1)];
if r.gen() {
v.push_all(&rv);
}

View file

@ -42,7 +42,6 @@ use std::cmp::min;
use std::env;
use std::io;
use std::io::prelude::*;
use std::iter::repeat;
const LINE_LEN: usize = 60;
const LOOKUP_SIZE: usize = 4 * 1024;
@ -121,7 +120,7 @@ impl<'a, W: Write> RepeatFasta<'a, W> {
fn make(&mut self, n: usize) -> io::Result<()> {
let alu_len = self.alu.len();
let mut buf = repeat(0).take(alu_len + LINE_LEN).collect::<Vec<_>>();
let mut buf = vec![0; alu_len + LINE_LEN];
let alu: &[u8] = self.alu.as_bytes();
for (slot, val) in buf.iter_mut().zip(alu) {

View file

@ -42,7 +42,6 @@
#![feature(iter_cmp)]
use std::iter::repeat;
use std::sync::Arc;
use std::sync::mpsc::channel;
use std::thread;
@ -221,7 +220,7 @@ fn get_id(m: u64) -> u8 {
// Converts a list of mask to a Vec<u8>.
fn to_vec(raw_sol: &List<u64>) -> Vec<u8> {
let mut sol = repeat('.' as u8).take(50).collect::<Vec<_>>();
let mut sol = vec![b'.'; 50];
for &m in raw_sol.iter() {
let id = '0' as u8 + get_id(m);
for i in 0..50 {

View file

@ -43,7 +43,6 @@
#![allow(non_snake_case)]
#![feature(unboxed_closures, iter_arith, core_simd, scoped)]
use std::iter::repeat;
use std::thread;
use std::env;
use std::simd::f64x2;
@ -62,7 +61,7 @@ fn main() {
fn spectralnorm(n: usize) -> f64 {
assert!(n % 2 == 0, "only even lengths are accepted");
let mut u = repeat(1.0).take(n).collect::<Vec<_>>();
let mut u = vec![1.0; n];
let mut v = u.clone();
let mut tmp = v.clone();
for _ in 0..10 {

View file

@ -15,7 +15,6 @@
use std::io::prelude::*;
use std::io;
use std::iter::repeat;
use std::env;
// Computes a single solution to a given 9x9 sudoku
@ -59,8 +58,7 @@ impl Sudoku {
reader.read_line(&mut s).unwrap();
assert_eq!(s, "9,9\n");
let mut g = repeat(vec![0, 0, 0, 0, 0, 0, 0, 0, 0])
.take(10).collect::<Vec<_>>();
let mut g = vec![vec![0, 0, 0, 0, 0, 0, 0, 0, 0]; 10];
for line in reader.lines() {
let line = line.unwrap();
let comps: Vec<&str> = line

View file

@ -68,12 +68,9 @@ impl Drop for AsciiArt {
// If there is a canonical constructor it is typically named the same as the type.
// Other constructor sort of functions are typically named from_foo, from_bar, etc.
fn AsciiArt(width: usize, height: usize, fill: char) -> AsciiArt {
// Use an anonymous function to build a vector of vectors containing
// blank characters for each position in our canvas.
let mut lines = Vec::new();
for _ in 0..height {
lines.push(repeat('.').take(width).collect::<Vec<_>>());
}
// Build a vector of vectors containing blank characters for each position in
// our canvas.
let lines = vec![vec!['.'; width]; height];
// Rust code often returns values by omitting the trailing semi-colon
// instead of using an explicit return statement.

View file

@ -19,7 +19,6 @@ extern crate alloc;
use alloc::heap;
use std::ptr;
use std::iter::repeat;
fn main() {
unsafe {
@ -29,7 +28,7 @@ fn main() {
unsafe fn test_triangle() -> bool {
static COUNT : usize = 16;
let mut ascend = repeat(ptr::null_mut()).take(COUNT).collect::<Vec<_>>();
let mut ascend = vec![ptr::null_mut(); COUNT];
let ascend = &mut *ascend;
static ALIGN : usize = 1;