From 9a1de086e4e5d46259c46128d17848cabbe7b8de Mon Sep 17 00:00:00 2001 From: ljedrz Date: Fri, 12 Oct 2018 14:14:03 +0200 Subject: [PATCH] rustc/driver: improve/remove allocations --- src/librustc_driver/driver.rs | 12 ++++++------ src/librustc_driver/lib.rs | 27 ++++++++++++--------------- src/librustc_driver/profile/trace.rs | 23 ++++++++++++----------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index ca1291670d0..248cf549d81 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -1586,9 +1586,8 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec Vec CrateDisambiguator { diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 981c4148f88..8d886d8c040 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -137,9 +137,7 @@ pub mod target_features { codegen_backend: &dyn CodegenBackend) { let tf = Symbol::intern("target_feature"); - for feat in codegen_backend.target_features(sess) { - cfg.insert((tf, Some(feat))); - } + cfg.extend(codegen_backend.target_features(sess).into_iter().map(|feat| (tf, Some(feat)))); if sess.crt_static_feature() { cfg.insert((tf, Some(Symbol::intern("crt-static")))); @@ -997,7 +995,7 @@ impl RustcDefaultCalls { input: &Input) -> Compilation { let r = matches.opt_strs("Z"); - if r.contains(&("ls".to_string())) { + if r.iter().any(|s| *s == "ls") { match input { &Input::File(ref ifile) => { let path = &(*ifile); @@ -1085,8 +1083,7 @@ impl RustcDefaultCalls { let allow_unstable_cfg = UnstableFeatures::from_environment() .is_nightly_build(); - let mut cfgs = Vec::new(); - for &(name, ref value) in sess.parse_sess.config.iter() { + let mut cfgs = sess.parse_sess.config.iter().filter_map(|&(name, ref value)| { let gated_cfg = GatedCfg::gate(&ast::MetaItem { ident: ast::Path::from_ident(ast::Ident::with_empty_ctxt(name)), node: ast::MetaItemKind::Word, @@ -1105,16 +1102,16 @@ impl RustcDefaultCalls { let value = value.as_ref().map(|s| s.as_ref()); if name != "target_feature" || value != Some("crt-static") { if !allow_unstable_cfg && gated_cfg.is_some() { - continue; + return None } } - cfgs.push(if let Some(value) = value { - format!("{}=\"{}\"", name, value) + if let Some(value) = value { + Some(format!("{}=\"{}\"", name, value)) } else { - name.to_string() - }); - } + Some(name.to_string()) + } + }).collect::>(); cfgs.sort(); for cfg in cfgs { @@ -1177,7 +1174,7 @@ fn usage(verbose: bool, include_unstable_options: bool) { for option in groups.iter().filter(|x| include_unstable_options || x.is_stable()) { (option.apply)(&mut options); } - let message = "Usage: rustc [OPTIONS] INPUT".to_string(); + let message = "Usage: rustc [OPTIONS] INPUT"; let nightly_help = if nightly_options::is_nightly_build() { "\n -Z help Print internal options for debugging rustc" } else { @@ -1192,7 +1189,7 @@ fn usage(verbose: bool, include_unstable_options: bool) { -C help Print codegen options -W help \ Print 'lint' options and default settings{}{}\n", - options.usage(&message), + options.usage(message), nightly_help, verbose_help); } @@ -1463,7 +1460,7 @@ pub fn handle_options(args: &[String]) -> Option { "the --no-stack-check flag is deprecated and does nothing"); } - if cg_flags.contains(&"passes=list".to_string()) { + if cg_flags.iter().any(|x| *x == "passes=list") { get_codegen_sysroot("llvm")().print_passes(); return None; } diff --git a/src/librustc_driver/profile/trace.rs b/src/librustc_driver/profile/trace.rs index 6cb728c4d58..8c065038d4b 100644 --- a/src/librustc_driver/profile/trace.rs +++ b/src/librustc_driver/profile/trace.rs @@ -89,18 +89,18 @@ fn html_of_duration(_start: &Instant, dur: &Duration) -> (String, String) { ) } -fn html_of_fraction(frac: f64) -> (String, String) { +fn html_of_fraction(frac: f64) -> (String, &'static str) { let css = { - if frac > 0.50 { "frac-50".to_string() } - else if frac > 0.40 { "frac-40".to_string() } - else if frac > 0.30 { "frac-30".to_string() } - else if frac > 0.20 { "frac-20".to_string() } - else if frac > 0.10 { "frac-10".to_string() } - else if frac > 0.05 { "frac-05".to_string() } - else if frac > 0.02 { "frac-02".to_string() } - else if frac > 0.01 { "frac-01".to_string() } - else if frac > 0.001 { "frac-001".to_string() } - else { "frac-0".to_string() } + if frac > 0.50 { "frac-50" } + else if frac > 0.40 { "frac-40" } + else if frac > 0.30 { "frac-30" } + else if frac > 0.20 { "frac-20" } + else if frac > 0.10 { "frac-10" } + else if frac > 0.05 { "frac-05" } + else if frac > 0.02 { "frac-02" } + else if frac > 0.01 { "frac-01" } + else if frac > 0.001 { "frac-001" } + else { "frac-0" } }; let percent = frac * 100.0; if percent > 0.1 { (format!("{:.1}%", percent), css) } @@ -148,6 +148,7 @@ fn write_traces_rec(file: &mut File, traces: &[Rec], total: Duration, depth: usi } fn compute_counts_rec(counts: &mut FxHashMap, traces: &[Rec]) { + counts.reserve(traces.len()); for t in traces.iter() { match t.effect { Effect::TimeBegin(ref msg) => {