From a7f2bb634308a5f05f2af716482b67ba43701681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 16 Oct 2020 11:43:39 +0200 Subject: [PATCH] Reserve space in advance --- compiler/rustc_ast/src/tokenstream.rs | 2 +- compiler/rustc_ast_lowering/src/lib.rs | 10 +++++----- compiler/rustc_builtin_macros/src/deriving/debug.rs | 2 +- compiler/rustc_lint/src/levels.rs | 2 ++ 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index 1e7001c2b23..fe67b905bf3 100644 --- a/compiler/rustc_ast/src/tokenstream.rs +++ b/compiler/rustc_ast/src/tokenstream.rs @@ -221,7 +221,7 @@ impl TokenStream { } } if let Some((pos, comma, sp)) = suggestion { - let mut new_stream = vec![]; + let mut new_stream = Vec::with_capacity(self.0.len() + 1); let parts = self.0.split_at(pos + 1); new_stream.extend_from_slice(parts.0); new_stream.push(comma); diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index ad3d3efe46a..88ad2706eac 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -2011,17 +2011,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // // For the "output" lifetime parameters, we just want to // generate `'_`. - let mut generic_args: Vec<_> = lifetime_params[..input_lifetimes_count] - .iter() - .map(|&(span, hir_name)| { + let mut generic_args = Vec::with_capacity(lifetime_params.len()); + generic_args.extend(lifetime_params[..input_lifetimes_count].iter().map( + |&(span, hir_name)| { // Input lifetime like `'a` or `'1`: GenericArg::Lifetime(hir::Lifetime { hir_id: self.next_id(), span, name: hir::LifetimeName::Param(hir_name), }) - }) - .collect(); + }, + )); generic_args.extend(lifetime_params[input_lifetimes_count..].iter().map(|&(span, _)| // Output lifetime like `'_`. GenericArg::Lifetime(hir::Lifetime { diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs index d84b3956475..9381264f498 100644 --- a/compiler/rustc_builtin_macros/src/deriving/debug.rs +++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs @@ -66,7 +66,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_> let fmt = substr.nonself_args[0].clone(); - let mut stmts = vec![]; + let mut stmts = Vec::with_capacity(fields.len() + 2); match vdata { ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => { // tuple struct/"normal" variant diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index aca28988364..02da85d25d5 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -30,6 +30,8 @@ fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> LintLevelMap { let mut builder = LintLevelMapBuilder { levels, tcx, store }; let krate = tcx.hir().krate(); + builder.levels.id_to_set.reserve(krate.exported_macros.len() + 1); + let push = builder.levels.push(&krate.item.attrs, &store, true); builder.levels.register_id(hir::CRATE_HIR_ID); for macro_def in krate.exported_macros {