提交 2b2462e8 编写于 作者: A Arlie Davis

Stop using intermediate macros in definition of symbols

Currently, the rustc_macros::symbols macro generates two
`macro_rules!` macros as its output. These two macros are
used in rustc_span/src/symbol.rs.

This means that each Symbol that we define is represented
in the AST of rustc_symbols twice: once in the definition
of the `define_symbols!` macro (similarly for the
`keywords! macro), and once in the rustc_span::symbols
definition.

That would be OK if there were only a handful of symbols,
but currently we define over 1100 symbols. The definition
of the `define_symbols!` macro contains the expanded definition
of each symbol, so that's a lot of AST storage wasted on a
macro that is used exactly once.

This commit removes the `define_symbols` macro, and simply
allows the proc macro to directly generate the
`rustc_symbols::symbol::sym` module.

The benefit is mainly in reducing memory wasted during
compilation of rustc itself. It should also reduce memory used
by Rust Analyzer.

This commit also reduces the size of the AST for symbol
definitions, by moving two `#[allow(...)]` attributes from
the symbol constants to the `sym` module. This eliminates 2200+
attribute nodes.

This commit also eliminates the need for the `digits_array`
constant. There's no need to store an array of Symbol values
for digits. We can simply define a constant of the base value,
and add to that base value.
上级 eb4fc71d
...@@ -126,7 +126,6 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) { ...@@ -126,7 +126,6 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
let mut keyword_stream = quote! {}; let mut keyword_stream = quote! {};
let mut symbols_stream = quote! {}; let mut symbols_stream = quote! {};
let mut digits_stream = quote! {};
let mut prefill_stream = quote! {}; let mut prefill_stream = quote! {};
let mut counter = 0u32; let mut counter = 0u32;
let mut keys = let mut keys =
...@@ -162,7 +161,6 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) { ...@@ -162,7 +161,6 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
#value, #value,
}); });
keyword_stream.extend(quote! { keyword_stream.extend(quote! {
#[allow(non_upper_case_globals)]
pub const #name: Symbol = Symbol::new(#counter); pub const #name: Symbol = Symbol::new(#counter);
}); });
counter += 1; counter += 1;
...@@ -182,42 +180,39 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) { ...@@ -182,42 +180,39 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
#value, #value,
}); });
symbols_stream.extend(quote! { symbols_stream.extend(quote! {
#[allow(rustc::default_hash_types)]
#[allow(non_upper_case_globals)]
pub const #name: Symbol = Symbol::new(#counter); pub const #name: Symbol = Symbol::new(#counter);
}); });
counter += 1; counter += 1;
} }
// Generate symbols for the strings "0", "1", ..., "9". // Generate symbols for the strings "0", "1", ..., "9".
let digits_base = counter;
counter += 10;
for n in 0..10 { for n in 0..10 {
let n = n.to_string(); let n = n.to_string();
check_dup(Span::call_site(), &n, &mut errors); check_dup(Span::call_site(), &n, &mut errors);
prefill_stream.extend(quote! { prefill_stream.extend(quote! {
#n, #n,
}); });
digits_stream.extend(quote! {
Symbol::new(#counter),
});
counter += 1;
} }
let _ = counter; // for future use
let output = quote! { let output = quote! {
macro_rules! keywords { const SYMBOL_DIGITS_BASE: u32 = #digits_base;
() => {
#keyword_stream
}
}
macro_rules! define_symbols { #[doc(hidden)]
() => { #[allow(non_upper_case_globals)]
#symbols_stream mod kw_generated {
use super::Symbol;
#keyword_stream
}
#[allow(non_upper_case_globals)] #[allow(rustc::default_hash_types)]
pub const digits_array: &[Symbol; 10] = &[ #[allow(non_upper_case_globals)]
#digits_stream #[doc(hidden)]
]; pub mod sym_generated {
} use super::Symbol;
#symbols_stream
} }
impl Interner { impl Interner {
......
...@@ -1577,8 +1577,7 @@ pub fn get(&self, symbol: Symbol) -> &str { ...@@ -1577,8 +1577,7 @@ pub fn get(&self, symbol: Symbol) -> &str {
/// Given that `kw` is imported, use them like `kw::keyword_name`. /// Given that `kw` is imported, use them like `kw::keyword_name`.
/// For example `kw::Loop` or `kw::Break`. /// For example `kw::Loop` or `kw::Break`.
pub mod kw { pub mod kw {
use super::Symbol; pub use super::kw_generated::*;
keywords!();
} }
// This module has a very short name because it's used a lot. // This module has a very short name because it's used a lot.
...@@ -1586,22 +1585,23 @@ pub mod kw { ...@@ -1586,22 +1585,23 @@ pub mod kw {
/// ///
/// Given that `sym` is imported, use them like `sym::symbol_name`. /// Given that `sym` is imported, use them like `sym::symbol_name`.
/// For example `sym::rustfmt` or `sym::u8`. /// For example `sym::rustfmt` or `sym::u8`.
#[allow(rustc::default_hash_types)]
pub mod sym { pub mod sym {
use super::Symbol; use super::Symbol;
use std::convert::TryInto; use std::convert::TryInto;
define_symbols!(); pub use super::sym_generated::*;
// Used from a macro in `librustc_feature/accepted.rs` // Used from a macro in `librustc_feature/accepted.rs`
pub use super::kw::MacroRules as macro_rules; pub use super::kw::MacroRules as macro_rules;
// Get the symbol for an integer. The first few non-negative integers each /// Get the symbol for an integer.
// have a static symbol and therefore are fast. ///
/// The first few non-negative integers each have a static symbol and therefore
/// are fast.
pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol { pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
if let Result::Ok(idx) = n.try_into() { if let Result::Ok(idx) = n.try_into() {
if let Option::Some(&sym_) = digits_array.get(idx) { if idx < 10 {
return sym_; return Symbol::new(super::SYMBOL_DIGITS_BASE + idx as u32);
} }
} }
Symbol::intern(&n.to_string()) Symbol::intern(&n.to_string())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册