Skip to content

Commit bc991de

Browse files
committed
reduce allocations when validating cfgs
1 parent 9cb4373 commit bc991de

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

src/tools/compiletest/src/common.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ macro_rules! string_enum {
2222

2323
impl $name {
2424
$vis const VARIANTS: &'static [Self] = &[$(Self::$variant,)*];
25+
$vis const STR_VARIANTS: &'static [&'static str] = &[$(Self::$variant.to_str(),)*];
2526

26-
$vis fn to_str(&self) -> &'static str {
27+
$vis const fn to_str(&self) -> &'static str {
2728
match self {
2829
$(Self::$variant => $repr,)*
2930
}

src/tools/compiletest/src/header/cfg.rs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::common::{Config, CompareMode, Debugger};
1+
use crate::common::{CompareMode, Config, Debugger};
22
use std::collections::HashSet;
33

44
/// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
@@ -48,7 +48,7 @@ pub(super) fn parse_cfg_name_directive<'a>(
4848
outcome = MatchOutcome::NoMatch;
4949
}
5050
}
51-
$(else if $allowed_names.contains(name) {
51+
$(else if $allowed_names.custom_contains(name) {
5252
message = Some(format_message());
5353
outcome = MatchOutcome::NoMatch;
5454
})?
@@ -69,13 +69,6 @@ pub(super) fn parse_cfg_name_directive<'a>(
6969
}
7070
};
7171
}
72-
macro_rules! hashset {
73-
($($value:expr),* $(,)?) => {{
74-
let mut set = HashSet::new();
75-
$(set.insert($value);)*
76-
set
77-
}}
78-
}
7972

8073
let target_cfgs = config.target_cfgs();
8174
let target_cfg = config.target_cfg();
@@ -140,7 +133,7 @@ pub(super) fn parse_cfg_name_directive<'a>(
140133

141134
condition! {
142135
name: &config.channel,
143-
allowed_names: hashset!["stable", "beta", "nightly"],
136+
allowed_names: &["stable", "beta", "nightly"],
144137
message: "when the release channel is {name}",
145138
}
146139
condition! {
@@ -155,7 +148,7 @@ pub(super) fn parse_cfg_name_directive<'a>(
155148
}
156149
condition! {
157150
name: config.stage_id.split('-').next().unwrap(),
158-
allowed_names: hashset!["stable", "beta", "nightly"],
151+
allowed_names: &["stable", "beta", "nightly"],
159152
message: "when the bootstrapping stage is {name}",
160153
}
161154
condition! {
@@ -170,20 +163,17 @@ pub(super) fn parse_cfg_name_directive<'a>(
170163
}
171164
maybe_condition! {
172165
name: config.debugger.as_ref().map(|d| d.to_str()),
173-
allowed_names: Debugger::VARIANTS
174-
.iter()
175-
.map(|v| v.to_str())
176-
.collect::<HashSet<_>>(),
166+
allowed_names: &Debugger::STR_VARIANTS,
177167
message: "when the debugger is {name}",
178168
}
179169
maybe_condition! {
180170
name: config.compare_mode
181171
.as_ref()
182172
.map(|d| format!("compare-mode-{}", d.to_str())),
183-
allowed_names: CompareMode::VARIANTS
184-
.iter()
185-
.map(|cm| format!("compare-mode-{}", cm.to_str()))
186-
.collect::<HashSet<_>>(),
173+
allowed_names: ContainsPrefixed {
174+
prefix: "compare-mode-",
175+
inner: CompareMode::STR_VARIANTS,
176+
},
187177
message: "when comparing with {name}",
188178
}
189179

@@ -231,3 +221,39 @@ pub(super) enum MatchOutcome {
231221
/// The directive is handled by other parts of our tooling.
232222
External,
233223
}
224+
225+
trait CustomContains {
226+
fn custom_contains(&self, item: &str) -> bool;
227+
}
228+
229+
impl CustomContains for HashSet<String> {
230+
fn custom_contains(&self, item: &str) -> bool {
231+
self.contains(item)
232+
}
233+
}
234+
235+
impl CustomContains for &[&str] {
236+
fn custom_contains(&self, item: &str) -> bool {
237+
self.contains(&item)
238+
}
239+
}
240+
241+
impl<const N: usize> CustomContains for [&str; N] {
242+
fn custom_contains(&self, item: &str) -> bool {
243+
self.contains(&item)
244+
}
245+
}
246+
247+
struct ContainsPrefixed<T: CustomContains> {
248+
prefix: &'static str,
249+
inner: T,
250+
}
251+
252+
impl<T: CustomContains> CustomContains for ContainsPrefixed<T> {
253+
fn custom_contains(&self, item: &str) -> bool {
254+
match item.strip_prefix(self.prefix) {
255+
Some(stripped) => self.inner.custom_contains(stripped),
256+
None => false,
257+
}
258+
}
259+
}

0 commit comments

Comments
 (0)