Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve a few vectors - calculate capacity or build from iterators #52703

Merged
merged 1 commit into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/hir/map/hir_id_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/infer/outlives/obligations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions src/librustc/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that this one doesn't actually help, since filter gives a lower bound of zero:

rust/src/libcore/iter/mod.rs

Lines 1514 to 1517 in fefe816

fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}

And thus the collect will be essentially useless at preallocating the Vec:

rust/src/liballoc/vec.rs

Lines 1852 to 1853 in fefe816

let (lower, _) = iterator.size_hint();
let mut vector = Vec::with_capacity(lower.saturating_add(1));

Copy link
Contributor Author

@ljedrz ljedrz Jul 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point; I'd still consider it, though, since it improves readability and is almost a half shorter.

.map(|_| ObjectSafetyViolation::SupertraitSelf)
.collect();

debug!("astconv_object_safety_violations(trait_def_id={:?}) = {:?}",
trait_def_id,
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_codegen_llvm/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,15 +634,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) {
Expand Down