Skip to content

Commit 8c9c6b3

Browse files
committed
Auto merge of rust-lang#137069 - Urgau:cfg-check-cfg-reserve, r=<try>
Reserve `HashSet` capacity before inserting cfgs/check-cfgs This PR tries to reserve capacity before inserting cfgs/check-cfgs into their hashset. Related to rust-lang#137005, mostly an experiment, but even if perf is neutral it still a good practice.
2 parents f77247a + 5a01c94 commit 8c9c6b3

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
152152

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

157163
macro_rules! ins_none {
158164
($key:expr) => {
@@ -316,6 +322,11 @@ impl CheckCfg {
316322
return;
317323
}
318324

325+
// As of 2025-02-14 there are 30 well known cfg, so pre-allocate
326+
// at least that much.
327+
self.well_known_names.reserve(30);
328+
self.expecteds.reserve(30);
329+
319330
// for `#[cfg(foo)]` (ie. cfg value is none)
320331
let no_values = || {
321332
let mut values = FxHashSet::default();
@@ -406,18 +417,27 @@ impl CheckCfg {
406417
&sym::target_vendor,
407418
];
408419

420+
// As of 2025-02-14 the maximum number of values is 41 in `target_os`
421+
// so allocate at least that much.
422+
// FIXME: Be more granular as not all `taregt_*` cfg have that much values.
423+
let target_values = || {
424+
let mut values = FxHashSet::default();
425+
values.reserve(41);
426+
ExpectedValues::Some(values)
427+
};
428+
409429
// Initialize (if not already initialized)
410430
for &e in VALUES {
411431
if !self.exhaustive_values {
412432
ins!(e, || ExpectedValues::Any);
413433
} else {
414-
ins!(e, empty_values);
434+
ins!(e, target_values);
415435
}
416436
}
417437

418438
if self.exhaustive_values {
419-
// Get all values map at once otherwise it would be costly.
420-
// (8 values * 220 targets ~= 1760 times, at the time of writing this comment).
439+
// Get all values map at once otherwise it would be _very_ costly.
440+
// (8 values * 287 targets ~= 2300 times, at the time of writing this comment).
421441
let [
422442
Some(values_target_abi),
423443
Some(values_target_arch),

0 commit comments

Comments
 (0)