Skip to content

Commit 5a01c94

Browse files
committed
Reserve HashSet capacity before inserting cfgs/check-cfgs
1 parent 3f33b30 commit 5a01c94

File tree

1 file changed

+23
-3
lines changed
  • compiler/rustc_session/src/config

1 file changed

+23
-3
lines changed

compiler/rustc_session/src/config/cfg.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
151151

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

156162
macro_rules! ins_none {
157163
($key:expr) => {
@@ -310,6 +316,11 @@ impl CheckCfg {
310316
return;
311317
}
312318

319+
// As of 2025-02-14 there are 30 well known cfg, so pre-allocate
320+
// at least that much.
321+
self.well_known_names.reserve(30);
322+
self.expecteds.reserve(30);
323+
313324
// for `#[cfg(foo)]` (ie. cfg value is none)
314325
let no_values = || {
315326
let mut values = FxHashSet::default();
@@ -400,18 +411,27 @@ impl CheckCfg {
400411
&sym::target_vendor,
401412
];
402413

414+
// As of 2025-02-14 the maximum number of values is 41 in `target_os`
415+
// so allocate at least that much.
416+
// FIXME: Be more granular as not all `taregt_*` cfg have that much values.
417+
let target_values = || {
418+
let mut values = FxHashSet::default();
419+
values.reserve(41);
420+
ExpectedValues::Some(values)
421+
};
422+
403423
// Initialize (if not already initialized)
404424
for &e in VALUES {
405425
if !self.exhaustive_values {
406426
ins!(e, || ExpectedValues::Any);
407427
} else {
408-
ins!(e, empty_values);
428+
ins!(e, target_values);
409429
}
410430
}
411431

412432
if self.exhaustive_values {
413-
// Get all values map at once otherwise it would be costly.
414-
// (8 values * 220 targets ~= 1760 times, at the time of writing this comment).
433+
// Get all values map at once otherwise it would be _very_ costly.
434+
// (8 values * 287 targets ~= 2300 times, at the time of writing this comment).
415435
let [
416436
Some(values_target_abi),
417437
Some(values_target_arch),

0 commit comments

Comments
 (0)