Skip to content

Commit 78260c1

Browse files
Auto merge of #149958 - yotamofek:pr/get-symbol-as-str-with-globals, r=<try>
Batch symbol interner lookups
2 parents 8188f6c + 9e1286d commit 78260c1

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ use rustc_data_structures::tagged_ptr::Tag;
3131
use rustc_macros::{Decodable, Encodable, HashStable_Generic, Walkable};
3232
pub use rustc_span::AttrId;
3333
use rustc_span::source_map::{Spanned, respan};
34-
use rustc_span::{ByteSymbol, DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
34+
use rustc_span::{
35+
ByteSymbol, DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym, with_session_globals,
36+
};
3537
use thin_vec::{ThinVec, thin_vec};
3638

3739
pub use crate::format::*;
@@ -170,16 +172,18 @@ pub fn join_path_syms(path: impl IntoIterator<Item = impl Borrow<Symbol>>) -> St
170172
let mut s = String::with_capacity(len_hint * 8);
171173

172174
let first_sym = *iter.next().unwrap().borrow();
173-
if first_sym != kw::PathRoot {
174-
s.push_str(first_sym.as_str());
175-
}
176-
for sym in iter {
177-
let sym = *sym.borrow();
178-
debug_assert_ne!(sym, kw::PathRoot);
179-
s.push_str("::");
180-
s.push_str(sym.as_str());
181-
}
182-
s
175+
with_session_globals(|globals| {
176+
if first_sym != kw::PathRoot {
177+
s.push_str(first_sym.get_str_from_session_globals(globals));
178+
}
179+
for sym in iter {
180+
let sym = *sym.borrow();
181+
debug_assert_ne!(sym, kw::PathRoot);
182+
s.push_str("::");
183+
s.push_str(sym.get_str_from_session_globals(globals));
184+
}
185+
s
186+
})
183187
}
184188

185189
/// Like `join_path_syms`, but for `Ident`s. This function is necessary because

compiler/rustc_ast/src/token.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use TokenKind::*;
88
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
99
use rustc_span::edition::Edition;
1010
use rustc_span::symbol::IdentPrintMode;
11-
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym};
11+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym, with_session_globals};
1212
#[allow(clippy::useless_attribute)] // FIXME: following use of `hidden_glob_reexports` incorrectly triggers `useless_attribute` lint.
1313
#[allow(hidden_glob_reexports)]
1414
use rustc_span::{Ident, Symbol};
@@ -927,8 +927,12 @@ impl Token {
927927
self.is_keyword(kw)
928928
|| (case == Case::Insensitive
929929
&& self.is_non_raw_ident_where(|id| {
930-
// Do an ASCII case-insensitive match, because all keywords are ASCII.
931-
id.name.as_str().eq_ignore_ascii_case(kw.as_str())
930+
with_session_globals(|globals| {
931+
// Do an ASCII case-insensitive match, because all keywords are ASCII.
932+
id.name
933+
.get_str_from_session_globals(globals)
934+
.eq_ignore_ascii_case(kw.get_str_from_session_globals(globals))
935+
})
932936
}))
933937
}
934938

compiler/rustc_parse/src/parser/item.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use rustc_errors::{Applicability, PResult, StashKey, struct_span_code_err};
1515
use rustc_session::lint::builtin::VARARGS_WITHOUT_PATTERN;
1616
use rustc_span::edit_distance::edit_distance;
1717
use rustc_span::edition::Edition;
18-
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, source_map, sym};
18+
use rustc_span::{
19+
DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, source_map, sym, with_session_globals,
20+
};
1921
use thin_vec::{ThinVec, thin_vec};
2022
use tracing::debug;
2123

@@ -2653,9 +2655,9 @@ impl<'a> Parser<'a> {
26532655
&& i.is_reserved()
26542656
)
26552657
|| case == Case::Insensitive
2656-
&& t.is_non_raw_ident_where(|i| quals.iter().any(|exp| {
2657-
exp.kw.as_str() == i.name.as_str().to_lowercase()
2658-
}))
2658+
&& with_session_globals(|globals| t.is_non_raw_ident_where(|i| quals.iter().any(|exp| {
2659+
exp.kw.get_str_from_session_globals(globals) == i.name.get_str_from_session_globals(globals).to_lowercase()
2660+
})))
26592661
)
26602662
// Rule out `unsafe extern {`.
26612663
&& !self.is_unsafe_foreign_mod()

compiler/rustc_span/src/symbol.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_data_structures::sync::Lock;
1515
use rustc_macros::{Decodable, Encodable, HashStable_Generic, symbols};
1616

1717
use crate::edit_distance::find_best_match_for_name;
18-
use crate::{DUMMY_SP, Edition, Span, with_session_globals};
18+
use crate::{DUMMY_SP, Edition, SessionGlobals, Span, with_session_globals};
1919

2020
#[cfg(test)]
2121
mod tests;
@@ -2830,6 +2830,13 @@ impl Symbol {
28302830
})
28312831
}
28322832

2833+
pub fn get_str_from_session_globals<'sess>(
2834+
&self,
2835+
session_globals: &'sess SessionGlobals,
2836+
) -> &'sess str {
2837+
session_globals.symbol_interner.get_str(*self)
2838+
}
2839+
28332840
pub fn as_u32(self) -> u32 {
28342841
self.0.as_u32()
28352842
}
@@ -2900,7 +2907,10 @@ impl StableCompare for Symbol {
29002907
const CAN_USE_UNSTABLE_SORT: bool = true;
29012908

29022909
fn stable_cmp(&self, other: &Self) -> std::cmp::Ordering {
2903-
self.as_str().cmp(other.as_str())
2910+
with_session_globals(|session_globals| {
2911+
self.get_str_from_session_globals(session_globals)
2912+
.cmp(other.get_str_from_session_globals(session_globals))
2913+
})
29042914
}
29052915
}
29062916

0 commit comments

Comments
 (0)