Skip to content

Commit 86cc326

Browse files
committed
Avoid unnecessary interning in Ident::from_str() calls.
A lot of these static symbols are pre-interned.
1 parent be3724f commit 86cc326

File tree

13 files changed

+45
-36
lines changed

13 files changed

+45
-36
lines changed

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4801,7 +4801,7 @@ impl<'a> LoweringContext<'a> {
48014801
let attr = {
48024802
// `allow(unreachable_code)`
48034803
let allow = {
4804-
let allow_ident = Ident::from_str("allow").with_span_pos(e.span);
4804+
let allow_ident = Ident::with_empty_ctxt(sym::allow).with_span_pos(e.span);
48054805
let uc_ident = Ident::from_str("unreachable_code").with_span_pos(e.span);
48064806
let uc_nested = attr::mk_nested_word_item(uc_ident);
48074807
attr::mk_list_item(e.span, allow_ident, vec![uc_nested])

src/librustc_allocator/expand.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::{
1919
mut_visit::{self, MutVisitor},
2020
parse::ParseSess,
2121
ptr::P,
22-
symbol::{Symbol, sym}
22+
symbol::{keywords, Symbol, sym}
2323
};
2424
use syntax_pos::Span;
2525

@@ -110,13 +110,14 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
110110
span,
111111
kind: AllocatorKind::Global,
112112
global: item.ident,
113-
core: Ident::from_str("core"),
113+
core: Ident::with_empty_ctxt(sym::core),
114114
cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
115115
};
116116

117117
// We will generate a new submodule. To `use` the static from that module, we need to get
118118
// the `super::...` path.
119-
let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
119+
let super_path =
120+
f.cx.path(f.span, vec![Ident::with_empty_ctxt(keywords::Super.name()), f.global]);
120121

121122
// Generate the items in the submodule
122123
let mut items = vec![
@@ -236,7 +237,7 @@ impl AllocFnFactory<'_> {
236237
) -> P<Expr> {
237238
match *ty {
238239
AllocatorTy::Layout => {
239-
let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
240+
let usize = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::usize));
240241
let ty_usize = self.cx.ty_path(usize);
241242
let size = ident();
242243
let align = ident();
@@ -298,12 +299,12 @@ impl AllocFnFactory<'_> {
298299
}
299300

300301
fn usize(&self) -> P<Ty> {
301-
let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
302+
let usize = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::usize));
302303
self.cx.ty_path(usize)
303304
}
304305

305306
fn ptr_u8(&self) -> P<Ty> {
306-
let u8 = self.cx.path_ident(self.span, Ident::from_str("u8"));
307+
let u8 = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::u8));
307308
let ty_u8 = self.cx.ty_path(u8);
308309
self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)
309310
}

src/librustc_resolve/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1979,11 +1979,11 @@ impl<'a> Resolver<'a> {
19791979
.collect();
19801980

19811981
if !attr::contains_name(&krate.attrs, sym::no_core) {
1982-
extern_prelude.insert(Ident::from_str("core"), Default::default());
1982+
extern_prelude.insert(Ident::with_empty_ctxt(sym::core), Default::default());
19831983
if !attr::contains_name(&krate.attrs, sym::no_std) {
1984-
extern_prelude.insert(Ident::from_str("std"), Default::default());
1984+
extern_prelude.insert(Ident::with_empty_ctxt(sym::std), Default::default());
19851985
if session.rust_2018() {
1986-
extern_prelude.insert(Ident::from_str("meta"), Default::default());
1986+
extern_prelude.insert(Ident::with_empty_ctxt(sym::meta), Default::default());
19871987
}
19881988
}
19891989
}
@@ -3374,7 +3374,7 @@ impl<'a> Resolver<'a> {
33743374
self.trait_map.insert(id, traits);
33753375
}
33763376

3377-
let mut std_path = vec![Segment::from_ident(Ident::from_str("std"))];
3377+
let mut std_path = vec![Segment::from_ident(Ident::with_empty_ctxt(sym::std))];
33783378
std_path.extend(path);
33793379
if self.primitive_type_table.primitive_types.contains_key(&path[0].ident.name) {
33803380
let cl = CrateLint::No;

src/librustdoc/clean/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,9 @@ impl Attributes {
929929
for attr in attrs.lists(sym::target_feature) {
930930
if attr.check_name(sym::enable) {
931931
if let Some(feat) = attr.value_str() {
932-
let meta = attr::mk_name_value_item_str(Ident::from_str("target_feature"),
933-
dummy_spanned(feat));
932+
let meta = attr::mk_name_value_item_str(
933+
Ident::with_empty_ctxt(sym::target_feature),
934+
dummy_spanned(feat));
934935
if let Ok(feat_cfg) = Cfg::parse(&meta) {
935936
cfg &= feat_cfg;
936937
}

src/libsyntax/attr/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::parse::parser::Parser;
2222
use crate::parse::{self, ParseSess, PResult};
2323
use crate::parse::token::{self, Token};
2424
use crate::ptr::P;
25-
use crate::symbol::{keywords, Symbol};
25+
use crate::symbol::{keywords, Symbol, sym};
2626
use crate::ThinVec;
2727
use crate::tokenstream::{TokenStream, TokenTree, DelimSpan};
2828
use crate::GLOBALS;
@@ -323,7 +323,7 @@ impl Attribute {
323323
if self.is_sugared_doc {
324324
let comment = self.value_str().unwrap();
325325
let meta = mk_name_value_item_str(
326-
Ident::from_str("doc"),
326+
Ident::with_empty_ctxt(sym::doc),
327327
dummy_spanned(Symbol::intern(&strip_doc_comment_decoration(&comment.as_str()))));
328328
let mut attr = if self.style == ast::AttrStyle::Outer {
329329
mk_attr_outer(self.span, self.id, meta)
@@ -414,7 +414,7 @@ pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute {
414414
Attribute {
415415
id,
416416
style,
417-
path: Path::from_ident(Ident::from_str("doc").with_span_pos(span)),
417+
path: Path::from_ident(Ident::with_empty_ctxt(sym::doc).with_span_pos(span)),
418418
tokens: MetaItemKind::NameValue(lit).tokens(span),
419419
is_sugared_doc: true,
420420
span,

src/libsyntax/ext/expand.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1522,19 +1522,19 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
15221522
let include_info = vec![
15231523
ast::NestedMetaItem::MetaItem(
15241524
attr::mk_name_value_item_str(
1525-
Ident::from_str("file"),
1525+
Ident::with_empty_ctxt(sym::file),
15261526
dummy_spanned(file),
15271527
),
15281528
),
15291529
ast::NestedMetaItem::MetaItem(
15301530
attr::mk_name_value_item_str(
1531-
Ident::from_str("contents"),
1531+
Ident::with_empty_ctxt(sym::contents),
15321532
dummy_spanned(src_interned),
15331533
),
15341534
),
15351535
];
15361536

1537-
let include_ident = Ident::from_str("include");
1537+
let include_ident = Ident::with_empty_ctxt(sym::include);
15381538
let item = attr::mk_list_item(DUMMY_SP, include_ident, include_info);
15391539
items.push(ast::NestedMetaItem::MetaItem(item));
15401540
}
@@ -1600,7 +1600,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
16001600
}
16011601
}
16021602

1603-
let meta = attr::mk_list_item(DUMMY_SP, Ident::from_str("doc"), items);
1603+
let meta = attr::mk_list_item(DUMMY_SP, Ident::with_empty_ctxt(sym::doc), items);
16041604
match at.style {
16051605
ast::AttrStyle::Inner => *at = attr::mk_spanned_attr_inner(at.span, at.id, meta),
16061606
ast::AttrStyle::Outer => *at = attr::mk_spanned_attr_outer(at.span, at.id, meta),

src/libsyntax/parse/parser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl TokenCursor {
352352
let body = TokenTree::Delimited(
353353
delim_span,
354354
token::Bracket,
355-
[TokenTree::Token(sp, token::Ident(ast::Ident::from_str("doc"), false)),
355+
[TokenTree::Token(sp, token::Ident(ast::Ident::with_empty_ctxt(sym::doc), false)),
356356
TokenTree::Token(sp, token::Eq),
357357
TokenTree::Token(sp, token::Literal(
358358
token::StrRaw(Symbol::intern(&stripped), num_of_hashes), None))
@@ -7012,7 +7012,8 @@ impl<'a> Parser<'a> {
70127012
let attr = Attribute {
70137013
id: attr::mk_attr_id(),
70147014
style: ast::AttrStyle::Outer,
7015-
path: ast::Path::from_ident(Ident::from_str("warn_directory_ownership")),
7015+
path: ast::Path::from_ident(
7016+
Ident::with_empty_ctxt(sym::warn_directory_ownership)),
70167017
tokens: TokenStream::empty(),
70177018
is_sugared_doc: false,
70187019
span: syntax_pos::DUMMY_SP,

src/libsyntax/print/pprust.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::print::pp::{self, Breaks};
1313
use crate::print::pp::Breaks::{Consistent, Inconsistent};
1414
use crate::ptr::P;
1515
use crate::std_inject;
16-
use crate::symbol::keywords;
16+
use crate::symbol::{keywords, sym};
1717
use crate::tokenstream::{self, TokenStream, TokenTree};
1818

1919
use rustc_target::spec::abi::{self, Abi};
@@ -89,13 +89,14 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
8989
// of the feature gate, so we fake them up here.
9090

9191
// #![feature(prelude_import)]
92-
let pi_nested = attr::mk_nested_word_item(ast::Ident::from_str("prelude_import"));
93-
let list = attr::mk_list_item(DUMMY_SP, ast::Ident::from_str("feature"), vec![pi_nested]);
92+
let pi_nested = attr::mk_nested_word_item(ast::Ident::with_empty_ctxt(sym::prelude_import));
93+
let list = attr::mk_list_item(
94+
DUMMY_SP, ast::Ident::with_empty_ctxt(sym::feature), vec![pi_nested]);
9495
let fake_attr = attr::mk_attr_inner(DUMMY_SP, attr::mk_attr_id(), list);
9596
s.print_attribute(&fake_attr)?;
9697

9798
// #![no_std]
98-
let no_std_meta = attr::mk_word_item(ast::Ident::from_str("no_std"));
99+
let no_std_meta = attr::mk_word_item(ast::Ident::with_empty_ctxt(sym::no_std));
99100
let fake_attr = attr::mk_attr_inner(DUMMY_SP, attr::mk_attr_id(), no_std_meta);
100101
s.print_attribute(&fake_attr)?;
101102
}

src/libsyntax/std_inject.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ pub fn maybe_inject_crates_ref(
7777
None
7878
};
7979
krate.module.items.insert(0, P(ast::Item {
80-
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
81-
attr::mk_attr_id(),
82-
attr::mk_word_item(ast::Ident::from_str("macro_use")))],
80+
attrs: vec![attr::mk_attr_outer(
81+
DUMMY_SP,
82+
attr::mk_attr_id(),
83+
attr::mk_word_item(ast::Ident::with_empty_ctxt(sym::macro_use))
84+
)],
8385
vis: dummy_spanned(ast::VisibilityKind::Inherited),
8486
node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
8587
ident: ast::Ident::with_empty_ctxt(rename),

src/libsyntax/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl MutVisitor for EntryPointCleaner {
171171
EntryPointType::MainAttr |
172172
EntryPointType::Start =>
173173
item.map(|ast::Item {id, ident, attrs, node, vis, span, tokens}| {
174-
let allow_ident = Ident::from_str("allow");
174+
let allow_ident = Ident::with_empty_ctxt(sym::allow);
175175
let dc_nested = attr::mk_nested_word_item(Ident::from_str("dead_code"));
176176
let allow_dead_code_item = attr::mk_list_item(DUMMY_SP, allow_ident,
177177
vec![dc_nested]);
@@ -215,7 +215,7 @@ fn mk_reexport_mod(cx: &mut TestCtxt<'_>,
215215
tests: Vec<Ident>,
216216
tested_submods: Vec<(Ident, Ident)>)
217217
-> (P<ast::Item>, Ident) {
218-
let super_ = Ident::from_str("super");
218+
let super_ = Ident::with_empty_ctxt(keywords::Super.name());
219219

220220
let items = tests.into_iter().map(|r| {
221221
cx.ext_cx.item_use_simple(DUMMY_SP, dummy_spanned(ast::VisibilityKind::Public),

src/libsyntax_ext/env.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use syntax::ast::{self, Ident, GenericArg};
77
use syntax::ext::base::{self, *};
88
use syntax::ext::build::AstBuilder;
9-
use syntax::symbol::{keywords, Symbol};
9+
use syntax::symbol::{keywords, Symbol, sym};
1010
use syntax_pos::Span;
1111
use syntax::tokenstream;
1212

@@ -29,7 +29,8 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
2929
true,
3030
cx.std_path(&["option", "Option", "None"]),
3131
vec![GenericArg::Type(cx.ty_rptr(sp,
32-
cx.ty_ident(sp, Ident::from_str("str")),
32+
cx.ty_ident(sp,
33+
Ident::with_empty_ctxt(sym::str)),
3334
Some(lt),
3435
ast::Mutability::Immutable))],
3536
vec![]))

src/libsyntax_ext/proc_macro_decls.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,11 @@ fn mk_decls(
362362
});
363363
let span = DUMMY_SP.apply_mark(mark);
364364

365-
let hidden = cx.meta_list_item_word(span, Symbol::intern("hidden"));
366-
let doc = cx.meta_list(span, Symbol::intern("doc"), vec![hidden]);
365+
let hidden = cx.meta_list_item_word(span, sym::hidden);
366+
let doc = cx.meta_list(span, sym::doc, vec![hidden]);
367367
let doc_hidden = cx.attribute(span, doc);
368368

369-
let proc_macro = Ident::from_str("proc_macro");
369+
let proc_macro = Ident::with_empty_ctxt(sym::proc_macro);
370370
let krate = cx.item(span,
371371
proc_macro,
372372
Vec::new(),

src/libsyntax_pos/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ symbols! {
336336
match_default_bindings,
337337
may_dangle,
338338
message,
339+
meta,
339340
min_const_fn,
340341
min_const_unsafe_fn,
341342
mips_target_feature,
@@ -531,6 +532,7 @@ symbols! {
531532
static_nobundle,
532533
static_recursion,
533534
std,
535+
str,
534536
stmt_expr_attributes,
535537
stop_after_dataflow,
536538
struct_field_attributes,

0 commit comments

Comments
 (0)