Skip to content

Commit 9c7d28d

Browse files
committed
Pre-intern "0", "1", ..., "9", and use where appropriate.
1 parent 58c68d0 commit 9c7d28d

File tree

8 files changed

+48
-13
lines changed

8 files changed

+48
-13
lines changed

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2956,7 +2956,7 @@ impl<'a> LoweringContext<'a> {
29562956
ident: match f.ident {
29572957
Some(ident) => ident,
29582958
// FIXME(jseyfried): positional field hygiene
2959-
None => Ident::new(Symbol::intern(&index.to_string()), f.span),
2959+
None => Ident::new(sym::integer(index), f.span),
29602960
},
29612961
vis: self.lower_visibility(&f.vis, None),
29622962
ty: self.lower_ty(&f.ty, ImplTraitContext::disallowed()),

src/librustc/hir/map/def_collector.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::session::CrateDisambiguator;
55
use syntax::ast::*;
66
use syntax::ext::hygiene::Mark;
77
use syntax::visit;
8-
use syntax::symbol::kw;
9-
use syntax::symbol::Symbol;
8+
use syntax::symbol::{kw, sym};
109
use syntax::parse::token::{self, Token};
1110
use syntax_pos::Span;
1211

@@ -221,7 +220,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
221220
_: &'a Generics, _: NodeId, _: Span) {
222221
for (index, field) in data.fields().iter().enumerate() {
223222
let name = field.ident.map(|ident| ident.name)
224-
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
223+
.unwrap_or_else(|| sym::integer(index));
225224
let def = self.create_def(field.id,
226225
DefPathData::ValueNs(name.as_interned_str()),
227226
field.span);

src/librustc/middle/mem_categorization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13161316

13171317
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
13181318
let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2)
1319-
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string())));
1319+
let interior = InteriorField(FieldIndex(i, sym::integer(i)));
13201320
let subcmt = Rc::new(
13211321
self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
13221322
self.cat_pattern_(subcmt, &subpat, op)?;
@@ -1363,7 +1363,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13631363
};
13641364
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
13651365
let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2)
1366-
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string())));
1366+
let interior = InteriorField(FieldIndex(i, sym::integer(i)));
13671367
let subcmt = Rc::new(
13681368
self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
13691369
self.cat_pattern_(subcmt, &subpat, op)?;

src/librustc_macros/src/symbols.rs

+20
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
9696

9797
let mut keyword_stream = quote! {};
9898
let mut symbols_stream = quote! {};
99+
let mut digits_stream = quote! {};
99100
let mut prefill_stream = quote! {};
100101
let mut counter = 0u32;
101102
let mut keys = HashSet::<String>::new();
@@ -106,6 +107,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
106107
}
107108
};
108109

110+
// Generate the listed keywords.
109111
for keyword in &input.keywords.0 {
110112
let name = &keyword.name;
111113
let value = &keyword.value;
@@ -119,6 +121,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
119121
counter += 1;
120122
}
121123

124+
// Generate the listed symbols.
122125
for symbol in &input.symbols.0 {
123126
let name = &symbol.name;
124127
let value = match &symbol.value {
@@ -135,6 +138,19 @@ pub fn symbols(input: TokenStream) -> TokenStream {
135138
counter += 1;
136139
}
137140

141+
// Generate symbols for the strings "0", "1", ..., "9".
142+
for n in 0..10 {
143+
let n = n.to_string();
144+
check_dup(&n);
145+
prefill_stream.extend(quote! {
146+
#n,
147+
});
148+
digits_stream.extend(quote! {
149+
Symbol::new(#counter),
150+
});
151+
counter += 1;
152+
}
153+
138154
let tt = TokenStream::from(quote! {
139155
macro_rules! keywords {
140156
() => {
@@ -145,6 +161,10 @@ pub fn symbols(input: TokenStream) -> TokenStream {
145161
macro_rules! symbols {
146162
() => {
147163
#symbols_stream
164+
165+
pub const digits_array: &[Symbol; 10] = &[
166+
#digits_stream
167+
];
148168
}
149169
}
150170

src/librustc_mir/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::Write;
22
use std::hash::Hash;
33
use std::ops::RangeInclusive;
44

5-
use syntax_pos::symbol::Symbol;
5+
use syntax_pos::symbol::{sym, Symbol};
66
use rustc::hir;
77
use rustc::ty::layout::{self, Size, Align, TyLayout, LayoutOf, VariantIdx};
88
use rustc::ty;
@@ -188,7 +188,7 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> ValidityVisitor<'rt, 'a, '
188188

189189
PathElem::ClosureVar(name.unwrap_or_else(|| {
190190
// Fall back to showing the field index.
191-
Symbol::intern(&field.to_string())
191+
sym::integer(field)
192192
}))
193193
}
194194

src/libsyntax/parse/lexer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ast::{self, Ident};
22
use crate::parse::ParseSess;
33
use crate::parse::token::{self, Token};
4-
use crate::symbol::Symbol;
4+
use crate::symbol::{sym, Symbol};
55
use crate::parse::unescape;
66
use crate::parse::unescape_error_reporting::{emit_unescape_error, push_escaped_char};
77

@@ -754,7 +754,7 @@ impl<'a> StringReader<'a> {
754754
}
755755
_ => {
756756
// just a 0
757-
return (token::Integer, self.name_from(start_bpos));
757+
return (token::Integer, sym::integer(0));
758758
}
759759
}
760760
} else if c.is_digit(10) {

src/libsyntax/parse/literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl LitKind {
197197
ast::LitIntType::Signed(ty) => Some(Symbol::intern(ty.ty_to_string())),
198198
ast::LitIntType::Unsuffixed => None,
199199
};
200-
(token::Integer, Symbol::intern(&n.to_string()), suffix)
200+
(token::Integer, sym::integer(n), suffix)
201201
}
202202
LitKind::Float(symbol, ty) => {
203203
(token::Float, symbol, Some(Symbol::intern(ty.ty_to_string())))

src/libsyntax_pos/symbol.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use rustc_data_structures::newtype_index;
99
use rustc_macros::symbols;
1010
use serialize::{Decodable, Decoder, Encodable, Encoder};
1111

12-
use std::fmt;
13-
use std::str;
1412
use std::cmp::{PartialEq, Ordering, PartialOrd, Ord};
13+
use std::fmt;
1514
use std::hash::{Hash, Hasher};
15+
use std::str;
1616

1717
use crate::hygiene::SyntaxContext;
1818
use crate::{Span, DUMMY_SP, GLOBALS};
@@ -102,6 +102,9 @@ symbols! {
102102
// Symbols that can be referred to with syntax_pos::sym::*. The symbol is
103103
// the stringified identifier unless otherwise specified (e.g.
104104
// `proc_dash_macro` represents "proc-macro").
105+
//
106+
// As well as the symbols listed, there are symbols for the the strings
107+
// "0", "1", ..., "9", which are accessible via `sym::integer`.
105108
Symbols {
106109
aarch64_target_feature,
107110
abi,
@@ -966,8 +969,21 @@ pub mod kw {
966969

967970
// This module has a very short name because it's used a lot.
968971
pub mod sym {
972+
use std::convert::TryInto;
969973
use super::Symbol;
974+
970975
symbols!();
976+
977+
// Get the symbol for an integer. The first few non-negative integers each
978+
// have a static symbol and therefore are fast.
979+
pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
980+
if let Result::Ok(idx) = n.try_into() {
981+
if let Option::Some(&sym) = digits_array.get(idx) {
982+
return sym;
983+
}
984+
}
985+
Symbol::intern(&n.to_string())
986+
}
971987
}
972988

973989
impl Symbol {

0 commit comments

Comments
 (0)