diff options
| author | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-05-14 14:55:15 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-05-20 09:17:00 +1000 |
| commit | 257eaf523f7faabfc9845a238ec3776fc45fcd81 (patch) | |
| tree | 1d863bfd60039acbd2611d740653bfdd8277a355 /src/libsyntax_pos | |
| parent | b96be5b1889a28d0d44b54a18c8d0467da109656 (diff) | |
| download | rust-257eaf523f7faabfc9845a238ec3776fc45fcd81.tar.gz rust-257eaf523f7faabfc9845a238ec3776fc45fcd81.zip | |
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%.
Diffstat (limited to 'src/libsyntax_pos')
| -rw-r--r-- | src/libsyntax_pos/symbol.rs | 9 |
1 files changed, 8 insertions, 1 deletions
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<F: FnOnce(&str) -> 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: Decoder>(d: &mut D) -> Result<InternedString, D::Error> { - Ok(Symbol::intern(&d.read_str()?).as_interned_str()) + Ok(InternedString::intern(&d.read_str()?)) } } |
