Skip to content

Commit

Permalink
Introduce InternedString::intern.
Browse files Browse the repository at this point in the history
`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%.
  • Loading branch information
nnethercote committed May 19, 2019
1 parent b96be5b commit 257eaf5
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 66 deletions.
6 changes: 3 additions & 3 deletions src/librustc/mir/mono.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
use crate::hir::HirId;
use syntax::symbol::{Symbol, InternedString};
use syntax::symbol::InternedString;
use crate::ty::{Instance, TyCtxt};
use crate::util::nodemap::FxHashMap;
use rustc_data_structures::base_n;
Expand Down Expand Up @@ -280,7 +280,7 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
cgu_name
} else {
let cgu_name = &cgu_name.as_str()[..];
Symbol::intern(&CodegenUnit::mangle_name(cgu_name)).as_interned_str()
InternedString::intern(&CodegenUnit::mangle_name(cgu_name))
}
}

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

Symbol::intern(&cgu_name[..]).as_interned_str()
InternedString::intern(&cgu_name[..])
}
}
5 changes: 3 additions & 2 deletions src/librustc/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::ty::{self, Ty, TyCtxt, TypeFoldable, Predicate, ToPredicate};
use crate::ty::subst::{Subst, InternalSubsts};
use std::borrow::Cow;
use std::iter::{self};
use syntax::ast::{self, Name};
use syntax::ast::{self};
use syntax::symbol::InternedString;
use syntax_pos::Span;

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

// `Receiver[Self => U]`
Expand Down
14 changes: 4 additions & 10 deletions src/librustc/traits/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,15 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
}

fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
use syntax::symbol::Symbol;

match t.sty {
ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => {
self.types.insert(
bound_ty.var.as_u32(),
match bound_ty.kind {
ty::BoundTyKind::Param(name) => name,
ty::BoundTyKind::Anon => Symbol::intern(
&format!("^{}", bound_ty.var.as_u32())
).as_interned_str(),
ty::BoundTyKind::Anon =>
InternedString::intern(&format!("^{}", bound_ty.var.as_u32()),
),
}
);
}
Expand All @@ -334,8 +332,6 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
}

fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
use syntax::symbol::Symbol;

match r {
ty::ReLateBound(index, br) if *index == self.binder_index => {
match br {
Expand All @@ -344,9 +340,7 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
}

ty::BoundRegion::BrAnon(var) => {
self.regions.insert(Symbol::intern(
&format!("'^{}", var)
).as_interned_str());
self.regions.insert(InternedString::intern(&format!("'^{}", var)));
}

_ => (),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3405,7 +3405,7 @@ impl_stable_hash_for!(struct self::SymbolName {
impl SymbolName {
pub fn new(name: &str) -> SymbolName {
SymbolName {
name: Symbol::intern(name).as_interned_str()
name: InternedString::intern(name)
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use crate::middle::region;
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
use crate::ty::subst::{Kind, Subst, UnpackedKind};
use crate::mir::interpret::ConstValue;
use syntax::symbol::{keywords, Symbol};

use rustc_target::spec::abi::Abi;
use syntax::symbol::InternedString;
use syntax::symbol::{keywords, InternedString};

use std::cell::Cell;
use std::fmt::{self, Write as _};
Expand Down Expand Up @@ -1285,10 +1283,10 @@ impl<F: fmt::Write> FmtPrinter<'_, 'gcx, 'tcx, F> {
{
fn name_by_region_index(index: usize) -> InternedString {
match index {
0 => Symbol::intern("'r"),
1 => Symbol::intern("'s"),
i => Symbol::intern(&format!("'t{}", i-2)),
}.as_interned_str()
0 => InternedString::intern("'r"),
1 => InternedString::intern("'s"),
i => InternedString::intern(&format!("'t{}", i-2)),
}
}

// Replace any anonymous late-bound regions with named
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/query/values.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ty::{self, Ty, TyCtxt, AdtSizedConstraint};
use crate::ty::util::NeedsDrop;

use syntax::symbol::Symbol;
use syntax::symbol::InternedString;

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

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

Expand Down
12 changes: 6 additions & 6 deletions src/librustc_codegen_utils/symbol_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_mir::monomorphize::item::{InstantiationMode, MonoItem, MonoItemExt};
use rustc_mir::monomorphize::Instance;

use syntax_pos::symbol::{Symbol, InternedString};
use syntax_pos::symbol::InternedString;

use log::debug;

Expand Down Expand Up @@ -238,13 +238,13 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) ->
if def_id.is_local() {
if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) {
let disambiguator = tcx.sess.local_crate_disambiguator();
return Symbol::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator))
.as_interned_str();
return
InternedString::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator));
}
if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) {
let disambiguator = tcx.sess.local_crate_disambiguator();
return Symbol::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator))
.as_interned_str();
return
InternedString::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator));
}
}

Expand Down Expand Up @@ -322,7 +322,7 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) ->
let _ = printer.write_str("{{vtable-shim}}");
}

Symbol::intern(&printer.path.finish(hash)).as_interned_str()
InternedString::intern(&printer.path.finish(hash))
}

// Follow C++ namespace-mangling style, see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use rustc::ty::subst::{SubstsRef, UnpackedKind};
use rustc::ty::{self, RegionKind, RegionVid, Ty, TyCtxt};
use rustc::ty::print::RegionHighlightMode;
use rustc_errors::DiagnosticBuilder;
use syntax::ast::Name;
use syntax::symbol::keywords;
use syntax_pos::Span;
use syntax_pos::symbol::InternedString;
Expand Down Expand Up @@ -791,6 +790,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
let c = *counter;
*counter += 1;

Name::intern(&format!("'{:?}", c)).as_interned_str()
InternedString::intern(&format!("'{:?}", c))
}
}
4 changes: 2 additions & 2 deletions src/librustc_mir/monomorphize/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use syntax::attr::InlineAttr;
use std::fmt::{self, Write};
use std::iter;
use rustc::mir::mono::Linkage;
use syntax_pos::symbol::Symbol;
use syntax_pos::symbol::InternedString;
use syntax::source_map::Span;
pub use rustc::mir::mono::MonoItem;

Expand Down Expand Up @@ -61,7 +61,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
MonoItem::GlobalAsm(hir_id) => {
let def_id = tcx.hir().local_def_id_from_hir_id(hir_id);
ty::SymbolName {
name: Symbol::intern(&format!("global_asm_{:?}", def_id)).as_interned_str()
name: InternedString::intern(&format!("global_asm_{:?}", def_id))
}
}
}
Expand Down
43 changes: 19 additions & 24 deletions src/librustc_mir/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc::lint::builtin::{SAFE_EXTERN_STATICS, SAFE_PACKED_BORROWS, UNUSED_UNSA
use rustc::mir::*;
use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext};

use syntax::symbol::{Symbol, sym};
use syntax::symbol::{InternedString, sym};

use std::ops::Bound;

Expand Down Expand Up @@ -167,9 +167,9 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
(CastTy::FnPtr, CastTy::Int(_)) => {
self.register_violations(&[UnsafetyViolation {
source_info: self.source_info,
description: Symbol::intern("cast of pointer to int").as_interned_str(),
details: Symbol::intern("casting pointers to integers in constants")
.as_interned_str(),
description: InternedString::intern("cast of pointer to int"),
details: InternedString::intern(
"casting pointers to integers in constants"),
kind: UnsafetyViolationKind::General,
}], &[]);
},
Expand All @@ -185,9 +185,8 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
if let ty::RawPtr(_) | ty::FnPtr(..) = lhs.ty(self.mir, self.tcx).sty {
self.register_violations(&[UnsafetyViolation {
source_info: self.source_info,
description: Symbol::intern("pointer operation").as_interned_str(),
details: Symbol::intern("operations on pointers in constants")
.as_interned_str(),
description: InternedString::intern("pointer operation"),
details: InternedString::intern("operations on pointers in constants"),
kind: UnsafetyViolationKind::General,
}], &[]);
}
Expand All @@ -212,13 +211,11 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
self.source_scope_local_data[source_info.scope].lint_root;
self.register_violations(&[UnsafetyViolation {
source_info,
description: Symbol::intern("borrow of packed field").as_interned_str(),
details:
Symbol::intern("fields of packed structs might be misaligned: \
dereferencing a misaligned pointer or even just \
creating a misaligned reference is undefined \
behavior")
.as_interned_str(),
description: InternedString::intern("borrow of packed field"),
details: InternedString::intern(
"fields of packed structs might be misaligned: dereferencing a \
misaligned pointer or even just creating a misaligned reference \
is undefined behavior"),
kind: UnsafetyViolationKind::BorrowPacked(lint_root)
}], &[]);
}
Expand Down Expand Up @@ -315,12 +312,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
self.source_scope_local_data[source_info.scope].lint_root;
self.register_violations(&[UnsafetyViolation {
source_info,
description: Symbol::intern("use of extern static").as_interned_str(),
details:
Symbol::intern("extern statics are not controlled by the Rust type \
system: invalid data, aliasing violations or data \
races will cause undefined behavior")
.as_interned_str(),
description: InternedString::intern("use of extern static"),
details: InternedString::intern(
"extern statics are not controlled by the Rust type system: invalid \
data, aliasing violations or data races will cause undefined behavior"),
kind: UnsafetyViolationKind::ExternStatic(lint_root)
}], &[]);
}
Expand All @@ -340,8 +335,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
let source_info = self.source_info;
self.register_violations(&[UnsafetyViolation {
source_info,
description: Symbol::intern(description).as_interned_str(),
details: Symbol::intern(details).as_interned_str(),
description: InternedString::intern(description),
details: InternedString::intern(details),
kind,
}], &[]);
}
Expand Down Expand Up @@ -441,8 +436,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
let source_info = self.source_info;
self.register_violations(&[UnsafetyViolation {
source_info,
description: Symbol::intern(description).as_interned_str(),
details: Symbol::intern(details).as_interned_str(),
description: InternedString::intern(description),
details: InternedString::intern(details),
kind: UnsafetyViolationKind::GeneralAndConstFn,
}], &[]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc::ty::subst::Subst;
use crate::require_same_types;

use rustc_target::spec::abi::Abi;
use syntax::symbol::Symbol;
use syntax::symbol::InternedString;

use rustc::hir;

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

let mk_va_list_ty = || {
Expand Down Expand Up @@ -397,7 +397,7 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
it: &hir::ForeignItem) {
let param = |n| {
let name = Symbol::intern(&format!("P{}", n)).as_interned_str();
let name = InternedString::intern(&format!("P{}", n));
tcx.mk_ty_param(n, name)
};

Expand Down
6 changes: 3 additions & 3 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use syntax::ast::{Ident, MetaItemKind};
use syntax::attr::{InlineAttr, OptimizeAttr, list_contains_name, mark_used};
use syntax::source_map::Spanned;
use syntax::feature_gate;
use syntax::symbol::{keywords, Symbol, sym};
use syntax::symbol::{InternedString, keywords, Symbol, sym};
use syntax_pos::{Span, DUMMY_SP};

use rustc::hir::def::{CtorKind, Res, DefKind};
Expand Down Expand Up @@ -1082,7 +1082,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty
.enumerate()
.map(|(i, &arg)| ty::GenericParamDef {
index: type_start + i as u32,
name: Symbol::intern(arg).as_interned_str(),
name: InternedString::intern(arg),
def_id,
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type {
Expand All @@ -1097,7 +1097,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty
params.extend(upvars.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| {
ty::GenericParamDef {
index: type_start + i,
name: Symbol::intern("<upvar>").as_interned_str(),
name: InternedString::intern("<upvar>"),
def_id,
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type {
Expand Down
9 changes: 8 additions & 1 deletion src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()?))
}
}

Expand Down

0 comments on commit 257eaf5

Please sign in to comment.