Skip to content

Commit 257eaf5

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 b96be5b commit 257eaf5

File tree

13 files changed

+60
-66
lines changed

13 files changed

+60
-66
lines changed

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/traits/object_safety.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::ty::{self, Ty, TyCtxt, TypeFoldable, Predicate, ToPredicate};
1818
use crate::ty::subst::{Subst, InternalSubsts};
1919
use std::borrow::Cow;
2020
use std::iter::{self};
21-
use syntax::ast::{self, Name};
21+
use syntax::ast::{self};
22+
use syntax::symbol::InternedString;
2223
use syntax_pos::Span;
2324

2425
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@@ -539,7 +540,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
539540
// are implemented
540541
let unsized_self_ty: Ty<'tcx> = self.mk_ty_param(
541542
::std::u32::MAX,
542-
Name::intern("RustaceansAreAwesome").as_interned_str(),
543+
InternedString::intern("RustaceansAreAwesome"),
543544
);
544545

545546
// `Receiver[Self => U]`

src/librustc/traits/structural_impls.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,15 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
312312
}
313313

314314
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
315-
use syntax::symbol::Symbol;
316-
317315
match t.sty {
318316
ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => {
319317
self.types.insert(
320318
bound_ty.var.as_u32(),
321319
match bound_ty.kind {
322320
ty::BoundTyKind::Param(name) => name,
323-
ty::BoundTyKind::Anon => Symbol::intern(
324-
&format!("^{}", bound_ty.var.as_u32())
325-
).as_interned_str(),
321+
ty::BoundTyKind::Anon =>
322+
InternedString::intern(&format!("^{}", bound_ty.var.as_u32()),
323+
),
326324
}
327325
);
328326
}
@@ -334,8 +332,6 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
334332
}
335333

336334
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
337-
use syntax::symbol::Symbol;
338-
339335
match r {
340336
ty::ReLateBound(index, br) if *index == self.binder_index => {
341337
match br {
@@ -344,9 +340,7 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
344340
}
345341

346342
ty::BoundRegion::BrAnon(var) => {
347-
self.regions.insert(Symbol::intern(
348-
&format!("'^{}", var)
349-
).as_interned_str());
343+
self.regions.insert(InternedString::intern(&format!("'^{}", var)));
350344
}
351345

352346
_ => (),

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/print/pretty.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ use crate::middle::region;
77
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
88
use crate::ty::subst::{Kind, Subst, UnpackedKind};
99
use crate::mir::interpret::ConstValue;
10-
use syntax::symbol::{keywords, Symbol};
11-
1210
use rustc_target::spec::abi::Abi;
13-
use syntax::symbol::InternedString;
11+
use syntax::symbol::{keywords, InternedString};
1412

1513
use std::cell::Cell;
1614
use std::fmt::{self, Write as _};
@@ -1285,10 +1283,10 @@ impl<F: fmt::Write> FmtPrinter<'_, 'gcx, 'tcx, F> {
12851283
{
12861284
fn name_by_region_index(index: usize) -> InternedString {
12871285
match index {
1288-
0 => Symbol::intern("'r"),
1289-
1 => Symbol::intern("'s"),
1290-
i => Symbol::intern(&format!("'t{}", i-2)),
1291-
}.as_interned_str()
1286+
0 => InternedString::intern("'r"),
1287+
1 => InternedString::intern("'s"),
1288+
i => InternedString::intern(&format!("'t{}", i-2)),
1289+
}
12921290
}
12931291

12941292
// Replace any anonymous late-bound regions with named

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
101101
use rustc_mir::monomorphize::item::{InstantiationMode, MonoItem, MonoItemExt};
102102
use rustc_mir::monomorphize::Instance;
103103

104-
use syntax_pos::symbol::{Symbol, InternedString};
104+
use syntax_pos::symbol::InternedString;
105105

106106
use log::debug;
107107

@@ -238,13 +238,13 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) ->
238238
if def_id.is_local() {
239239
if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) {
240240
let disambiguator = tcx.sess.local_crate_disambiguator();
241-
return Symbol::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator))
242-
.as_interned_str();
241+
return
242+
InternedString::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator));
243243
}
244244
if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) {
245245
let disambiguator = tcx.sess.local_crate_disambiguator();
246-
return Symbol::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator))
247-
.as_interned_str();
246+
return
247+
InternedString::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator));
248248
}
249249
}
250250

@@ -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/borrow_check/nll/region_infer/error_reporting/region_name.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc::ty::subst::{SubstsRef, UnpackedKind};
1212
use rustc::ty::{self, RegionKind, RegionVid, Ty, TyCtxt};
1313
use rustc::ty::print::RegionHighlightMode;
1414
use rustc_errors::DiagnosticBuilder;
15-
use syntax::ast::Name;
1615
use syntax::symbol::keywords;
1716
use syntax_pos::Span;
1817
use syntax_pos::symbol::InternedString;
@@ -791,6 +790,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
791790
let c = *counter;
792791
*counter += 1;
793792

794-
Name::intern(&format!("'{:?}", c)).as_interned_str()
793+
InternedString::intern(&format!("'{:?}", c))
795794
}
796795
}

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

+19-24
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, sym};
1616

1717
use std::ops::Bound;
1818

@@ -167,9 +167,9 @@ 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(),
171-
details: Symbol::intern("casting pointers to integers in constants")
172-
.as_interned_str(),
170+
description: InternedString::intern("cast of pointer to int"),
171+
details: InternedString::intern(
172+
"casting pointers to integers in constants"),
173173
kind: UnsafetyViolationKind::General,
174174
}], &[]);
175175
},
@@ -185,9 +185,8 @@ 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(),
189-
details: Symbol::intern("operations on pointers in constants")
190-
.as_interned_str(),
188+
description: InternedString::intern("pointer operation"),
189+
details: InternedString::intern("operations on pointers in constants"),
191190
kind: UnsafetyViolationKind::General,
192191
}], &[]);
193192
}
@@ -212,13 +211,11 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
212211
self.source_scope_local_data[source_info.scope].lint_root;
213212
self.register_violations(&[UnsafetyViolation {
214213
source_info,
215-
description: Symbol::intern("borrow of packed field").as_interned_str(),
216-
details:
217-
Symbol::intern("fields of packed structs might be misaligned: \
218-
dereferencing a misaligned pointer or even just \
219-
creating a misaligned reference is undefined \
220-
behavior")
221-
.as_interned_str(),
214+
description: InternedString::intern("borrow of packed field"),
215+
details: InternedString::intern(
216+
"fields of packed structs might be misaligned: dereferencing a \
217+
misaligned pointer or even just creating a misaligned reference \
218+
is undefined behavior"),
222219
kind: UnsafetyViolationKind::BorrowPacked(lint_root)
223220
}], &[]);
224221
}
@@ -315,12 +312,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
315312
self.source_scope_local_data[source_info.scope].lint_root;
316313
self.register_violations(&[UnsafetyViolation {
317314
source_info,
318-
description: Symbol::intern("use of extern static").as_interned_str(),
319-
details:
320-
Symbol::intern("extern statics are not controlled by the Rust type \
321-
system: invalid data, aliasing violations or data \
322-
races will cause undefined behavior")
323-
.as_interned_str(),
315+
description: InternedString::intern("use of extern static"),
316+
details: InternedString::intern(
317+
"extern statics are not controlled by the Rust type system: invalid \
318+
data, aliasing violations or data races will cause undefined behavior"),
324319
kind: UnsafetyViolationKind::ExternStatic(lint_root)
325320
}], &[]);
326321
}
@@ -340,8 +335,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
340335
let source_info = self.source_info;
341336
self.register_violations(&[UnsafetyViolation {
342337
source_info,
343-
description: Symbol::intern(description).as_interned_str(),
344-
details: Symbol::intern(details).as_interned_str(),
338+
description: InternedString::intern(description),
339+
details: InternedString::intern(details),
345340
kind,
346341
}], &[]);
347342
}
@@ -441,8 +436,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
441436
let source_info = self.source_info;
442437
self.register_violations(&[UnsafetyViolation {
443438
source_info,
444-
description: Symbol::intern(description).as_interned_str(),
445-
details: Symbol::intern(details).as_interned_str(),
439+
description: InternedString::intern(description),
440+
details: InternedString::intern(details),
446441
kind: UnsafetyViolationKind::GeneralAndConstFn,
447442
}], &[]);
448443
}

src/librustc_typeck/check/intrinsic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::ty::subst::Subst;
77
use crate::require_same_types;
88

99
use rustc_target::spec::abi::Abi;
10-
use syntax::symbol::Symbol;
10+
use syntax::symbol::InternedString;
1111

1212
use rustc::hir;
1313

@@ -80,7 +80,7 @@ pub fn intrisic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
8080
/// and in libcore/intrinsics.rs
8181
pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
8282
it: &hir::ForeignItem) {
83-
let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)).as_interned_str());
83+
let param = |n| tcx.mk_ty_param(n, InternedString::intern(&format!("P{}", n)));
8484
let name = it.ident.as_str();
8585

8686
let mk_va_list_ty = || {
@@ -397,7 +397,7 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
397397
pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
398398
it: &hir::ForeignItem) {
399399
let param = |n| {
400-
let name = Symbol::intern(&format!("P{}", n)).as_interned_str();
400+
let name = InternedString::intern(&format!("P{}", n));
401401
tcx.mk_ty_param(n, name)
402402
};
403403

src/librustc_typeck/collect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use syntax::ast::{Ident, MetaItemKind};
3939
use syntax::attr::{InlineAttr, OptimizeAttr, list_contains_name, mark_used};
4040
use syntax::source_map::Spanned;
4141
use syntax::feature_gate;
42-
use syntax::symbol::{keywords, Symbol, sym};
42+
use syntax::symbol::{InternedString, keywords, Symbol, sym};
4343
use syntax_pos::{Span, DUMMY_SP};
4444

4545
use rustc::hir::def::{CtorKind, Res, DefKind};
@@ -1082,7 +1082,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty
10821082
.enumerate()
10831083
.map(|(i, &arg)| ty::GenericParamDef {
10841084
index: type_start + i as u32,
1085-
name: Symbol::intern(arg).as_interned_str(),
1085+
name: InternedString::intern(arg),
10861086
def_id,
10871087
pure_wrt_drop: false,
10881088
kind: ty::GenericParamDefKind::Type {
@@ -1097,7 +1097,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty
10971097
params.extend(upvars.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| {
10981098
ty::GenericParamDef {
10991099
index: type_start + i,
1100-
name: Symbol::intern("<upvar>").as_interned_str(),
1100+
name: InternedString::intern("<upvar>"),
11011101
def_id,
11021102
pure_wrt_drop: false,
11031103
kind: ty::GenericParamDefKind::Type {

src/libsyntax_pos/symbol.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,13 @@ pub struct InternedString {
11341134
}
11351135

11361136
impl InternedString {
1137+
/// Maps a string to its interned representation.
1138+
pub fn intern(string: &str) -> Self {
1139+
InternedString {
1140+
symbol: Symbol::intern(string)
1141+
}
1142+
}
1143+
11371144
pub fn with<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
11381145
let str = with_interner(|interner| {
11391146
interner.get(self.symbol) as *const str
@@ -1236,7 +1243,7 @@ impl fmt::Display for InternedString {
12361243

12371244
impl Decodable for InternedString {
12381245
fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> {
1239-
Ok(Symbol::intern(&d.read_str()?).as_interned_str())
1246+
Ok(InternedString::intern(&d.read_str()?))
12401247
}
12411248
}
12421249

0 commit comments

Comments
 (0)