Skip to content

Commit 5b94342

Browse files
committed
rustc_span: Symbol perf experiments
1 parent a609fb4 commit 5b94342

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,6 +3910,7 @@ dependencies = [
39103910
name = "rustc_macros"
39113911
version = "0.1.0"
39123912
dependencies = [
3913+
"phf_codegen",
39133914
"proc-macro2",
39143915
"quote",
39153916
"syn",
@@ -4197,6 +4198,7 @@ version = "0.0.0"
41974198
dependencies = [
41984199
"cfg-if 0.1.10",
41994200
"md-5",
4201+
"phf",
42004202
"rustc_arena",
42014203
"rustc_data_structures",
42024204
"rustc_index",

compiler/rustc_macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ proc-macro = true
1010
[dependencies]
1111
synstructure = "0.12.1"
1212
syn = { version = "1", features = ["full"] }
13+
phf_codegen = "0.8"
1314
proc-macro2 = "1"
1415
quote = "1"

compiler/rustc_macros/src/symbols.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
127127
let mut keyword_stream = quote! {};
128128
let mut symbols_stream = quote! {};
129129
let mut prefill_stream = quote! {};
130+
let mut symbol_strings = Vec::new();
130131
let mut counter = 0u32;
131132
let mut keys =
132133
HashMap::<String, Span>::with_capacity(input.keywords.len() + input.symbols.len() + 10);
@@ -163,6 +164,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
163164
keyword_stream.extend(quote! {
164165
pub const #name: Symbol = Symbol::new(#counter);
165166
});
167+
symbol_strings.push(value_string);
166168
counter += 1;
167169
}
168170

@@ -182,6 +184,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
182184
symbols_stream.extend(quote! {
183185
pub const #name: Symbol = Symbol::new(#counter);
184186
});
187+
symbol_strings.push(value);
185188
counter += 1;
186189
}
187190

@@ -194,9 +197,19 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
194197
prefill_stream.extend(quote! {
195198
#n,
196199
});
200+
symbol_strings.push(n);
197201
}
198202
let _ = counter; // for future use
199203

204+
// Build the PHF map. This translates from strings to Symbol values.
205+
let mut phf_map = phf_codegen::Map::<&str>::new();
206+
for (symbol_index, symbol) in symbol_strings.iter().enumerate() {
207+
phf_map.entry(symbol, format!("Symbol::new({})", symbol_index as u32).as_str());
208+
}
209+
let phf_map_built = phf_map.build();
210+
let phf_map_text = phf_map_built.to_string();
211+
let phf_map_expr = syn::parse_str::<syn::Expr>(&phf_map_text).unwrap();
212+
200213
let output = quote! {
201214
const SYMBOL_DIGITS_BASE: u32 = #digits_base;
202215

@@ -222,6 +235,8 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
222235
])
223236
}
224237
}
238+
239+
static STATIC_SYMBOLS_PHF: ::phf::Map<&'static str, Symbol> = #phf_map_expr;
225240
};
226241

227242
(output, errors.list)

compiler/rustc_span/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ tracing = "0.1"
2020
sha-1 = "0.9"
2121
sha2 = "0.9"
2222
md-5 = "0.9"
23+
phf = "0.8"

compiler/rustc_span/src/symbol.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,11 @@ impl Symbol {
14531453

14541454
/// Maps a string to its interned representation.
14551455
pub fn intern(string: &str) -> Self {
1456-
with_interner(|interner| interner.intern(string))
1456+
if let Some(symbol) = STATIC_SYMBOLS_PHF.get(string) {
1457+
*symbol
1458+
} else {
1459+
with_interner(|interner| interner.intern(string))
1460+
}
14571461
}
14581462

14591463
/// Convert to a `SymbolStr`. This is a slowish operation because it

src/tools/tidy/src/deps.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
132132
"parking_lot",
133133
"parking_lot_core",
134134
"pathdiff",
135+
"phf",
136+
"phf_codegen",
137+
"phf_generator",
138+
"phf_shared",
135139
"pkg-config",
136140
"polonius-engine",
137141
"ppv-lite86",
@@ -164,6 +168,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
164168
"sha-1",
165169
"sha2",
166170
"smallvec",
171+
"siphasher",
167172
"snap",
168173
"stable_deref_trait",
169174
"stacker",

0 commit comments

Comments
 (0)