Skip to content

Commit 5d192a4

Browse files
committed
Slow down Symbol::as_str
1 parent 5b94342 commit 5d192a4

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

compiler/rustc_macros/src/symbols.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
201201
}
202202
let _ = counter; // for future use
203203

204+
// We have finished collecting symbol strings.
205+
let static_symbols_len = symbol_strings.len();
206+
207+
// Build the body of STATIC_SYMBOLS.
208+
let symbol_strings_tokens: TokenStream = symbol_strings.iter().map(|s| quote!(#s,)).collect();
209+
204210
// Build the PHF map. This translates from strings to Symbol values.
205211
let mut phf_map = phf_codegen::Map::<&str>::new();
206212
for (symbol_index, symbol) in symbol_strings.iter().enumerate() {
@@ -236,6 +242,10 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
236242
}
237243
}
238244

245+
static STATIC_SYMBOLS: [&str; #static_symbols_len as usize] = [
246+
#symbol_strings_tokens
247+
];
248+
239249
static STATIC_SYMBOLS_PHF: ::phf::Map<&'static str, Symbol> = #phf_map_expr;
240250
};
241251

compiler/rustc_span/src/symbol.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,19 +1453,24 @@ impl Symbol {
14531453

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

14631464
/// Convert to a `SymbolStr`. This is a slowish operation because it
14641465
/// requires locking the symbol interner.
14651466
pub fn as_str(self) -> SymbolStr {
1466-
with_interner(|interner| unsafe {
1467-
SymbolStr { string: std::mem::transmute::<&str, &str>(interner.get(self)) }
1468-
})
1467+
if let Some(string) = STATIC_SYMBOLS.get(self.0.as_usize()) {
1468+
SymbolStr { string }
1469+
} else {
1470+
with_interner(|interner| unsafe {
1471+
SymbolStr { string: std::mem::transmute::<&str, &str>(interner.get(self)) }
1472+
})
1473+
}
14691474
}
14701475

14711476
pub fn as_u32(self) -> u32 {

0 commit comments

Comments
 (0)