Use vec![elt; n] where possible

The common pattern `iter::repeat(elt).take(n).collect::<Vec<_>>()` is
exactly equivalent to `vec![elt; n]`, do this replacement in the whole
tree.

(Actually, vec![] is smart enough to only call clone n - 1 times, while
the former solution would call clone n times, and this fact is
virtually irrelevant in practice.)
This commit is contained in:
Ulrik Sverdrup 2015-07-08 22:52:55 +02:00
parent 5b6a464358
commit 836f32e769
31 changed files with 61 additions and 91 deletions

View file

@ -704,7 +704,7 @@ fn is_useful(cx: &MatchCheckCtxt,
match is_useful(cx, &matrix, v.tail(), witness) {
UsefulWithWitness(pats) => {
let arity = constructor_arity(cx, &constructor, left_ty);
let wild_pats: Vec<_> = repeat(DUMMY_WILD_PAT).take(arity).collect();
let wild_pats = vec![DUMMY_WILD_PAT; arity];
let enum_pat = construct_witness(cx, &constructor, wild_pats, left_ty);
let mut new_pats = vec![enum_pat];
new_pats.extend(pats);
@ -862,7 +862,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
} = raw_pat(r[col]);
let head: Option<Vec<&Pat>> = match *node {
ast::PatWild(_) =>
Some(repeat(DUMMY_WILD_PAT).take(arity).collect()),
Some(vec![DUMMY_WILD_PAT; arity]),
ast::PatIdent(_, _, _) => {
let opt_def = cx.tcx.def_map.borrow().get(&pat_id).map(|d| d.full_def());
@ -875,7 +875,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
} else {
None
},
_ => Some(repeat(DUMMY_WILD_PAT).take(arity).collect())
_ => Some(vec![DUMMY_WILD_PAT; arity])
}
}
@ -889,7 +889,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
DefVariant(..) | DefStruct(..) => {
Some(match args {
&Some(ref args) => args.iter().map(|p| &**p).collect(),
&None => repeat(DUMMY_WILD_PAT).take(arity).collect(),
&None => vec![DUMMY_WILD_PAT; arity],
})
}
_ => None

View file

@ -21,7 +21,6 @@ use middle::cfg::CFGIndex;
use middle::ty;
use std::io;
use std::usize;
use std::iter::repeat;
use syntax::ast;
use syntax::ast_util::IdRange;
use syntax::visit;
@ -239,11 +238,11 @@ impl<'a, 'tcx, O:DataFlowOperator> DataFlowContext<'a, 'tcx, O> {
let entry = if oper.initial_value() { usize::MAX } else {0};
let zeroes: Vec<_> = repeat(0).take(num_nodes * words_per_id).collect();
let gens: Vec<_> = zeroes.clone();
let kills1: Vec<_> = zeroes.clone();
let kills2: Vec<_> = zeroes;
let on_entry: Vec<_> = repeat(entry).take(num_nodes * words_per_id).collect();
let zeroes = vec![0; num_nodes * words_per_id];
let gens = zeroes.clone();
let kills1 = zeroes.clone();
let kills2 = zeroes;
let on_entry = vec![entry; num_nodes * words_per_id];
let nodeid_to_index = build_nodeid_to_index(decl, cfg);
@ -511,7 +510,7 @@ impl<'a, 'tcx, O:DataFlowOperator+Clone+'static> DataFlowContext<'a, 'tcx, O> {
changed: true
};
let mut temp: Vec<_> = repeat(0).take(words_per_id).collect();
let mut temp = vec![0; words_per_id];
while propcx.changed {
propcx.changed = false;
propcx.reset(&mut temp);

View file

@ -34,7 +34,6 @@ use util::nodemap::{FnvHashMap, FnvHashSet};
use std::cell::{Cell, RefCell};
use std::cmp::Ordering::{self, Less, Greater, Equal};
use std::fmt;
use std::iter::repeat;
use std::u32;
use syntax::ast;
@ -1304,7 +1303,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
// idea is to report errors that derive from independent
// regions of the graph, but not those that derive from
// overlapping locations.
let mut dup_vec: Vec<_> = repeat(u32::MAX).take(self.num_vars() as usize).collect();
let mut dup_vec = vec![u32::MAX; self.num_vars() as usize];
for idx in 0..self.num_vars() as usize {
match var_data[idx].value {

View file

@ -119,7 +119,6 @@ use util::nodemap::NodeMap;
use std::{fmt, usize};
use std::io::prelude::*;
use std::io;
use std::iter::repeat;
use std::rc::Rc;
use syntax::ast::{self, NodeId, Expr};
use syntax::codemap::{BytePos, original_sp, Span};
@ -566,8 +565,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
Liveness {
ir: ir,
s: specials,
successors: repeat(invalid_node()).take(num_live_nodes).collect(),
users: repeat(invalid_users()).take(num_live_nodes * num_vars).collect(),
successors: vec![invalid_node(); num_live_nodes],
users: vec![invalid_users(); num_live_nodes * num_vars],
loop_scope: Vec::new(),
break_ln: NodeMap(),
cont_ln: NodeMap(),