Skip to content

Reserve HashSet capacity before inserting cfgs/check-cfgs #137069

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

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 23 additions & 3 deletions compiler/rustc_session/src/config/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {

/// Generate the default configs for a given session
pub(crate) fn default_configuration(sess: &Session) -> Cfg {
// As of 2025-02-14 a default `x86_64-unknown-linux-gnu` has ~20 cfgs.
//
// So let's round that up to 32 to avoid allocating unnecessarely and to
// give a bit a wigle room for the various options and cfgs that might
// affect the list of cfgs.
let mut ret = Cfg::default();
ret.reserve(32);

macro_rules! ins_none {
($key:expr) => {
Expand Down Expand Up @@ -310,6 +316,11 @@ impl CheckCfg {
return;
}

// As of 2025-02-14 there are 30 well known cfg, so pre-allocate
// at least that much.
self.well_known_names.reserve(30);
self.expecteds.reserve(30);

// for `#[cfg(foo)]` (ie. cfg value is none)
let no_values = || {
let mut values = FxHashSet::default();
Expand Down Expand Up @@ -400,18 +411,27 @@ impl CheckCfg {
&sym::target_vendor,
];

// As of 2025-02-14 the maximum number of values is 41 in `target_os`
// so allocate at least that much.
// FIXME: Be more granular as not all `taregt_*` cfg have that much values.
let target_values = || {
let mut values = FxHashSet::default();
values.reserve(41);
ExpectedValues::Some(values)
};

// Initialize (if not already initialized)
for &e in VALUES {
if !self.exhaustive_values {
ins!(e, || ExpectedValues::Any);
} else {
ins!(e, empty_values);
ins!(e, target_values);
}
}

if self.exhaustive_values {
// Get all values map at once otherwise it would be costly.
// (8 values * 220 targets ~= 1760 times, at the time of writing this comment).
// Get all values map at once otherwise it would be _very_ costly.
// (8 values * 287 targets ~= 2300 times, at the time of writing this comment).
let [
Some(values_target_abi),
Some(values_target_arch),
Expand Down
Loading