Rollup merge of #52703 - ljedrz:vec_improvements, r=nikomatsakis

Improve a few vectors - calculate capacity or build from iterators

Collecting from iterators improves readability and tailoring vector capacities should be beneficial in terms of performance.
This commit is contained in:
kennytm 2018-07-28 16:24:57 +08:00 committed by GitHub
commit 7da22148ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 21 deletions

View File

@ -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 {

View File

@ -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::<Vec<_>>();
let mut regions = ty.regions();
regions.retain(|r| !r.is_late_bound()); // ignore late-bound regions

View File

@ -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);

View File

@ -98,13 +98,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn astconv_object_safety_violations(self, trait_def_id: DefId)
-> Vec<ObjectSafetyViolation>
{
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,

View File

@ -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);

View File

@ -632,15 +632,14 @@ impl MultiSpan {
/// `SpanLabel` instances with empty labels.
pub fn span_labels(&self) -> Vec<SpanLabel> {
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::<Vec<_>>();
for &span in &self.primary_spans {
if !span_labels.iter().any(|sl| sl.span == span) {