From acd38f656ad20166f25f527530ddc32a523c9e97 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Wed, 25 Jul 2018 13:20:53 +0200 Subject: [PATCH] Improve a few vectors - calculate capacity or build from iterators --- src/librustc/hir/map/hir_id_validator.rs | 2 +- src/librustc/infer/outlives/obligations.rs | 6 +----- src/librustc/lint/context.rs | 2 +- src/librustc/traits/object_safety.rs | 11 ++++------- src/librustc_codegen_llvm/back/link.rs | 5 +++-- src/libsyntax_pos/lib.rs | 9 ++++----- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/librustc/hir/map/hir_id_validator.rs b/src/librustc/hir/map/hir_id_validator.rs index 656f325b4dd89..a17c160c4d03e 100644 --- a/src/librustc/hir/map/hir_id_validator.rs +++ b/src/librustc/hir/map/hir_id_validator.rs @@ -105,7 +105,7 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> { .collect(); // Try to map those to something more useful - let mut missing_items = vec![]; + let mut missing_items = Vec::with_capacity(missing.len()); for local_id in missing { let hir_id = HirId { diff --git a/src/librustc/infer/outlives/obligations.rs b/src/librustc/infer/outlives/obligations.rs index 07286f1250cd3..b8991a0366a61 100644 --- a/src/librustc/infer/outlives/obligations.rs +++ b/src/librustc/infer/outlives/obligations.rs @@ -505,11 +505,7 @@ where } fn recursive_type_bound(&self, ty: Ty<'tcx>) -> VerifyBound<'tcx> { - let mut bounds = vec![]; - - for subty in ty.walk_shallow() { - bounds.push(self.type_bound(subty)); - } + let mut bounds = ty.walk_shallow().map(|subty| self.type_bound(subty)).collect::>(); let mut regions = ty.regions(); regions.retain(|r| !r.is_late_bound()); // ignore late-bound regions diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 5c1009fb31f19..14cfaa8153377 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -219,7 +219,7 @@ impl LintStore { } } - let mut future_incompatible = vec![]; + let mut future_incompatible = Vec::with_capacity(lints.len()); for lint in lints { future_incompatible.push(lint.id); self.future_incompatible.insert(lint.id, lint); diff --git a/src/librustc/traits/object_safety.rs b/src/librustc/traits/object_safety.rs index 83128ba75d582..aa4f63675d734 100644 --- a/src/librustc/traits/object_safety.rs +++ b/src/librustc/traits/object_safety.rs @@ -98,13 +98,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { pub fn astconv_object_safety_violations(self, trait_def_id: DefId) -> Vec { - let mut violations = vec![]; - - for def_id in traits::supertrait_def_ids(self, trait_def_id) { - if self.predicates_reference_self(def_id, true) { - violations.push(ObjectSafetyViolation::SupertraitSelf); - } - } + let violations = traits::supertrait_def_ids(self, trait_def_id) + .filter(|&def_id| self.predicates_reference_self(def_id, true)) + .map(|_| ObjectSafetyViolation::SupertraitSelf) + .collect(); debug!("astconv_object_safety_violations(trait_def_id={:?}) = {:?}", trait_def_id, diff --git a/src/librustc_codegen_llvm/back/link.rs b/src/librustc_codegen_llvm/back/link.rs index f2b17584adcba..13f0c90e8855e 100644 --- a/src/librustc_codegen_llvm/back/link.rs +++ b/src/librustc_codegen_llvm/back/link.rs @@ -40,6 +40,7 @@ use std::env; use std::fmt; use std::fs; use std::io; +use std::iter; use std::path::{Path, PathBuf}; use std::process::{Output, Stdio}; use std::str; @@ -885,9 +886,9 @@ fn exec_linker(sess: &Session, cmd: &mut Command, out_filename: &Path, tmpdir: & } let file = tmpdir.join("linker-arguments"); let bytes = if sess.target.target.options.is_like_msvc { - let mut out = vec![]; + let mut out = Vec::with_capacity((1 + args.len()) * 2); // start the stream with a UTF-16 BOM - for c in vec![0xFEFF].into_iter().chain(args.encode_utf16()) { + for c in iter::once(0xFEFF).chain(args.encode_utf16()) { // encode in little endian out.push(c as u8); out.push((c >> 8) as u8); diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index cc09a944e4ccc..dfa38e874a7a5 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -634,15 +634,14 @@ impl MultiSpan { /// `SpanLabel` instances with empty labels. pub fn span_labels(&self) -> Vec { let is_primary = |span| self.primary_spans.contains(&span); - let mut span_labels = vec![]; - for &(span, ref label) in &self.span_labels { - span_labels.push(SpanLabel { + let mut span_labels = self.span_labels.iter().map(|&(span, ref label)| + SpanLabel { span, is_primary: is_primary(span), label: Some(label.clone()) - }); - } + } + ).collect::>(); for &span in &self.primary_spans { if !span_labels.iter().any(|sl| sl.span == span) {