Skip to content

Commit 47656a3

Browse files
committed
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%.
1 parent 8bf5e85 commit 47656a3

File tree

8 files changed

+27
-20
lines changed

8 files changed

+27
-20
lines changed

src/librustc/hir/map/definitions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ impl DefPathData {
593593
ImplTrait => "{{opaque}}",
594594
};
595595

596-
Symbol::intern(s).as_interned_str()
596+
InternedString::intern(s)
597597
}
598598

599599
pub fn to_string(&self) -> String {

src/librustc/mir/mono.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
22
use crate::hir::HirId;
3-
use syntax::symbol::{Symbol, InternedString};
3+
use syntax::symbol::InternedString;
44
use crate::ty::{Instance, TyCtxt};
55
use crate::util::nodemap::FxHashMap;
66
use rustc_data_structures::base_n;
@@ -280,7 +280,7 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
280280
cgu_name
281281
} else {
282282
let cgu_name = &cgu_name.as_str()[..];
283-
Symbol::intern(&CodegenUnit::mangle_name(cgu_name)).as_interned_str()
283+
InternedString::intern(&CodegenUnit::mangle_name(cgu_name))
284284
}
285285
}
286286

@@ -336,6 +336,6 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
336336
write!(cgu_name, ".{}", special_suffix).unwrap();
337337
}
338338

339-
Symbol::intern(&cgu_name[..]).as_interned_str()
339+
InternedString::intern(&cgu_name[..])
340340
}
341341
}

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3405,7 +3405,7 @@ impl_stable_hash_for!(struct self::SymbolName {
34053405
impl SymbolName {
34063406
pub fn new(name: &str) -> SymbolName {
34073407
SymbolName {
3408-
name: Symbol::intern(name).as_interned_str()
3408+
name: InternedString::intern(name)
34093409
}
34103410
}
34113411

src/librustc/ty/query/values.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ty::{self, Ty, TyCtxt, AdtSizedConstraint};
22
use crate::ty::util::NeedsDrop;
33

4-
use syntax::symbol::Symbol;
4+
use syntax::symbol::InternedString;
55

66
pub(super) trait Value<'tcx>: Sized {
77
fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self;
@@ -28,7 +28,7 @@ impl<'tcx> Value<'tcx> for Ty<'tcx> {
2828

2929
impl<'tcx> Value<'tcx> for ty::SymbolName {
3030
fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
31-
ty::SymbolName { name: Symbol::intern("<error>").as_interned_str() }
31+
ty::SymbolName { name: InternedString::intern("<error>") }
3232
}
3333
}
3434

src/librustc_codegen_utils/symbol_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) ->
322322
let _ = printer.write_str("{{vtable-shim}}");
323323
}
324324

325-
Symbol::intern(&printer.path.finish(hash)).as_interned_str()
325+
InternedString::intern(&printer.path.finish(hash))
326326
}
327327

328328
// Follow C++ namespace-mangling style, see

src/librustc_mir/monomorphize/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::attr::InlineAttr;
1010
use std::fmt::{self, Write};
1111
use std::iter;
1212
use rustc::mir::mono::Linkage;
13-
use syntax_pos::symbol::Symbol;
13+
use syntax_pos::symbol::InternedString;
1414
use syntax::source_map::Span;
1515
pub use rustc::mir::mono::MonoItem;
1616

@@ -61,7 +61,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
6161
MonoItem::GlobalAsm(hir_id) => {
6262
let def_id = tcx.hir().local_def_id_from_hir_id(hir_id);
6363
ty::SymbolName {
64-
name: Symbol::intern(&format!("global_asm_{:?}", def_id)).as_interned_str()
64+
name: InternedString::intern(&format!("global_asm_{:?}", def_id))
6565
}
6666
}
6767
}

src/librustc_mir/transform/check_unsafety.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc::lint::builtin::{SAFE_EXTERN_STATICS, SAFE_PACKED_BORROWS, UNUSED_UNSA
1212
use rustc::mir::*;
1313
use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext};
1414

15-
use syntax::symbol::{Symbol, sym};
15+
use syntax::symbol::{InternedString, Symbol, sym};
1616

1717
use std::ops::Bound;
1818

@@ -167,7 +167,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
167167
(CastTy::FnPtr, CastTy::Int(_)) => {
168168
self.register_violations(&[UnsafetyViolation {
169169
source_info: self.source_info,
170-
description: Symbol::intern("cast of pointer to int").as_interned_str(),
170+
description: InternedString::intern("cast of pointer to int"),
171171
details: Symbol::intern("casting pointers to integers in constants")
172172
.as_interned_str(),
173173
kind: UnsafetyViolationKind::General,
@@ -185,7 +185,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
185185
if let ty::RawPtr(_) | ty::FnPtr(..) = lhs.ty(self.mir, self.tcx).sty {
186186
self.register_violations(&[UnsafetyViolation {
187187
source_info: self.source_info,
188-
description: Symbol::intern("pointer operation").as_interned_str(),
188+
description: InternedString::intern("pointer operation"),
189189
details: Symbol::intern("operations on pointers in constants")
190190
.as_interned_str(),
191191
kind: UnsafetyViolationKind::General,
@@ -212,7 +212,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
212212
self.source_scope_local_data[source_info.scope].lint_root;
213213
self.register_violations(&[UnsafetyViolation {
214214
source_info,
215-
description: Symbol::intern("borrow of packed field").as_interned_str(),
215+
description: InternedString::intern("borrow of packed field"),
216216
details:
217217
Symbol::intern("fields of packed structs might be misaligned: \
218218
dereferencing a misaligned pointer or even just \
@@ -315,7 +315,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
315315
self.source_scope_local_data[source_info.scope].lint_root;
316316
self.register_violations(&[UnsafetyViolation {
317317
source_info,
318-
description: Symbol::intern("use of extern static").as_interned_str(),
318+
description: InternedString::intern("use of extern static"),
319319
details:
320320
Symbol::intern("extern statics are not controlled by the Rust type \
321321
system: invalid data, aliasing violations or data \
@@ -340,8 +340,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
340340
let source_info = self.source_info;
341341
self.register_violations(&[UnsafetyViolation {
342342
source_info,
343-
description: Symbol::intern(description).as_interned_str(),
344-
details: Symbol::intern(details).as_interned_str(),
343+
description: InternedString::intern(description),
344+
details: InternedString::intern(details),
345345
kind,
346346
}], &[]);
347347
}
@@ -441,8 +441,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
441441
let source_info = self.source_info;
442442
self.register_violations(&[UnsafetyViolation {
443443
source_info,
444-
description: Symbol::intern(description).as_interned_str(),
445-
details: Symbol::intern(details).as_interned_str(),
444+
description: InternedString::intern(description),
445+
details: InternedString::intern(details),
446446
kind: UnsafetyViolationKind::GeneralAndConstFn,
447447
}], &[]);
448448
}

src/libsyntax_pos/symbol.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,13 @@ pub struct InternedString {
11241124
}
11251125

11261126
impl InternedString {
1127+
/// Maps a string to its interned representation.
1128+
pub fn intern(string: &str) -> Self {
1129+
InternedString {
1130+
symbol: Symbol::intern(string)
1131+
}
1132+
}
1133+
11271134
pub fn with<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
11281135
let str = with_interner(|interner| {
11291136
interner.get(self.symbol) as *const str
@@ -1226,7 +1233,7 @@ impl fmt::Display for InternedString {
12261233

12271234
impl Decodable for InternedString {
12281235
fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> {
1229-
Ok(Symbol::intern(&d.read_str()?).as_interned_str())
1236+
Ok(InternedString::intern(&d.read_str()?))
12301237
}
12311238
}
12321239

0 commit comments

Comments
 (0)