From b5e463dec92f9637de39469ad9f410b549fe3183 Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 8 Mar 2024 21:06:46 +0100 Subject: [PATCH] Add cache-based fast path for cfgs and check-cfgs --- compiler/rustc_attr/src/builtin.rs | 64 ++++++++++++----------- compiler/rustc_interface/src/interface.rs | 33 +++++++++++- compiler/rustc_session/src/parse.rs | 13 +++++ 3 files changed, 78 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index 814104ec78c49..0e1eadddb824c 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -525,38 +525,40 @@ pub fn cfg_matches( ) -> bool { eval_condition(cfg, sess, features, &mut |cfg| { try_gate_cfg(cfg.name, cfg.span, sess, features); - match sess.psess.check_config.expecteds.get(&cfg.name) { - Some(ExpectedValues::Some(values)) if !values.contains(&cfg.value) => { - sess.psess.buffer_lint_with_diagnostic( - UNEXPECTED_CFGS, - cfg.span, - lint_node_id, - if let Some(value) = cfg.value { - format!("unexpected `cfg` condition value: `{value}`") - } else { - format!("unexpected `cfg` condition value: (none)") - }, - BuiltinLintDiag::UnexpectedCfgValue( - (cfg.name, cfg.name_span), - cfg.value.map(|v| (v, cfg.value_span.unwrap())), - ), - ); - } - None if sess.psess.check_config.exhaustive_names => { - sess.psess.buffer_lint_with_diagnostic( - UNEXPECTED_CFGS, - cfg.span, - lint_node_id, - format!("unexpected `cfg` condition name: `{}`", cfg.name), - BuiltinLintDiag::UnexpectedCfgName( - (cfg.name, cfg.name_span), - cfg.value.map(|v| (v, cfg.value_span.unwrap())), - ), - ); + sess.psess.config_cache.get(&(cfg.name, cfg.value)).copied().unwrap_or_else(|| { + match sess.psess.check_config.expecteds.get(&cfg.name) { + Some(ExpectedValues::Some(values)) if !values.contains(&cfg.value) => { + sess.psess.buffer_lint_with_diagnostic( + UNEXPECTED_CFGS, + cfg.span, + lint_node_id, + if let Some(value) = cfg.value { + format!("unexpected `cfg` condition value: `{value}`") + } else { + format!("unexpected `cfg` condition value: (none)") + }, + BuiltinLintDiag::UnexpectedCfgValue( + (cfg.name, cfg.name_span), + cfg.value.map(|v| (v, cfg.value_span.unwrap())), + ), + ); + } + None if sess.psess.check_config.exhaustive_names => { + sess.psess.buffer_lint_with_diagnostic( + UNEXPECTED_CFGS, + cfg.span, + lint_node_id, + format!("unexpected `cfg` condition name: `{}`", cfg.name), + BuiltinLintDiag::UnexpectedCfgName( + (cfg.name, cfg.name_span), + cfg.value.map(|v| (v, cfg.value_span.unwrap())), + ), + ); + } + _ => { /* not unexpected */ } } - _ => { /* not unexpected */ } - } - sess.psess.config.contains(&(cfg.name, cfg.value)) + sess.psess.config.contains(&(cfg.name, cfg.value)) + }) }) } diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 284d965979ece..02a20277e0e72 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -17,7 +17,7 @@ use rustc_query_impl::QueryCtxt; use rustc_query_system::query::print_query_stack; use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName}; use rustc_session::filesearch::sysroot_candidates; -use rustc_session::parse::ParseSess; +use rustc_session::parse::{CfgCache, ParseSess}; use rustc_session::{lint, CompilerIO, EarlyDiagCtxt, Session}; use rustc_span::source_map::FileLoader; use rustc_span::symbol::sym; @@ -260,6 +260,35 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec) -> CheckCfg { check_cfg } +/// Create the config cache of `--cfg` and `--check-cfg` for internal use. +/// +/// The cache is used to reduce the number of hashmap lookups by +/// pre-computing the maximum possible configs[^1] from `--check-cfg` and `--cfg`. +/// +/// It heavily favours expected cfgs (if any are provided), since if the +/// user provides a list of expected cfgs, they are likely to care more +/// about expected cfgs than unexpected cfgs. +/// +/// [^1]: See [`CfgCache`] for an explanation of "maximum possible". +fn config_cache(config: &Cfg, check_cfg: &CheckCfg) -> CfgCache { + let mut config_cache = CfgCache::default(); + + if check_cfg.expecteds.is_empty() { + config_cache.extend(config.iter().map(|cfg| (*cfg, true))); + } else { + #[allow(rustc::potential_query_instability)] + for (name, expected) in &check_cfg.expecteds { + if let ExpectedValues::Some(values) = expected { + config_cache.extend( + values.iter().map(|value| ((*name, *value), config.contains(&(*name, *value)))), + ); + } + } + } + + config_cache +} + /// The compiler configuration pub struct Config { /// Command line options @@ -402,6 +431,8 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se check_cfg.fill_well_known(&sess.target); sess.psess.check_config = check_cfg; + sess.psess.config_cache = config_cache(&sess.psess.config, &sess.psess.check_config); + if let Some(psess_created) = config.psess_created { psess_created(&mut sess.psess); } diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index 398138d7e1ff3..1d9484b9b62dc 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -196,12 +196,24 @@ pub fn add_feature_diagnostics_for_issue( } } +/// Config cache that tries to combine expected cfgs and enabled cfgs. +/// +/// If a config is found, this means it is definitely an expected config +/// and the value represents whenever that config is enabled. +/// +/// If a config is not found, it doesn't imply anything! The config still may +/// be enabled and it may also be expected. You should only rely on the +/// cache having a config and not "NOT having it". You should always fallback +/// to a manual lookup in `ParseSess::config` and `ParseSess::check_cfg`. +pub type CfgCache = FxHashMap<(Symbol, Option), bool>; + /// Info about a parsing session. pub struct ParseSess { pub dcx: DiagCtxt, pub unstable_features: UnstableFeatures, pub config: Cfg, pub check_config: CheckCfg, + pub config_cache: CfgCache, pub edition: Edition, /// Places where raw identifiers were used. This is used to avoid complaining about idents /// clashing with keywords in new editions. @@ -250,6 +262,7 @@ impl ParseSess { unstable_features: UnstableFeatures::from_environment(None), config: Cfg::default(), check_config: CheckCfg::default(), + config_cache: CfgCache::default(), edition: ExpnId::root().expn_data().edition, raw_identifier_spans: Default::default(), bad_unicode_identifiers: Lock::new(Default::default()),