From 257eaf523f7faabfc9845a238ec3776fc45fcd81 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 14 May 2019 14:55:15 +1000 Subject: Introduce `InternedString::intern`. `InternedString::intern(x)` is preferable to `Symbol::intern(x).as_interned_str()`, because the former involves one call to `with_interner` while the latter involves two. The case within InternedString::decode() is particularly hot, and this change reduces the number of `with_interner` calls by up to 13%. --- src/libsyntax_pos/symbol.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/libsyntax_pos') diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 01b126f48b3..e0425220c3f 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -1134,6 +1134,13 @@ pub struct InternedString { } impl InternedString { + /// Maps a string to its interned representation. + pub fn intern(string: &str) -> Self { + InternedString { + symbol: Symbol::intern(string) + } + } + pub fn with R, R>(self, f: F) -> R { let str = with_interner(|interner| { interner.get(self.symbol) as *const str @@ -1236,7 +1243,7 @@ impl fmt::Display for InternedString { impl Decodable for InternedString { fn decode(d: &mut D) -> Result { - Ok(Symbol::intern(&d.read_str()?).as_interned_str()) + Ok(InternedString::intern(&d.read_str()?)) } } -- cgit 1.4.1-3-g733a5