Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] rustc_span: Symbol perf experiments #80420

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3910,6 +3910,7 @@ dependencies = [
name = "rustc_macros"
version = "0.1.0"
dependencies = [
"phf_codegen",
"proc-macro2",
"quote",
"syn",
Expand Down Expand Up @@ -4197,6 +4198,7 @@ version = "0.0.0"
dependencies = [
"cfg-if 0.1.10",
"md-5",
"phf",
"rustc_arena",
"rustc_data_structures",
"rustc_index",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ proc-macro = true
[dependencies]
synstructure = "0.12.1"
syn = { version = "1", features = ["full"] }
phf_codegen = "0.8"
proc-macro2 = "1"
quote = "1"
19 changes: 17 additions & 2 deletions compiler/rustc_macros/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use std::collections::HashMap;
use syn::parse::{Parse, ParseStream, Result};
use syn::{braced, punctuated::Punctuated, Ident, LitStr, Token};

#[cfg(test)]
mod tests;
// #[cfg(test)]
// mod tests;

mod kw {
syn::custom_keyword!(Keywords);
Expand Down Expand Up @@ -127,6 +127,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
let mut keyword_stream = quote! {};
let mut symbols_stream = quote! {};
let mut prefill_stream = quote! {};
let mut symbol_strings = Vec::new();
let mut counter = 0u32;
let mut keys =
HashMap::<String, Span>::with_capacity(input.keywords.len() + input.symbols.len() + 10);
Expand Down Expand Up @@ -163,6 +164,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
keyword_stream.extend(quote! {
pub const #name: Symbol = Symbol::new(#counter);
});
symbol_strings.push(value_string);
counter += 1;
}

Expand All @@ -182,6 +184,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
symbols_stream.extend(quote! {
pub const #name: Symbol = Symbol::new(#counter);
});
symbol_strings.push(value);
counter += 1;
}

Expand All @@ -194,9 +197,19 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
prefill_stream.extend(quote! {
#n,
});
symbol_strings.push(n);
}
let _ = counter; // for future use

// Build the PHF map. This translates from strings to Symbol values.
let mut phf_map = phf_codegen::Map::<&str>::new();
for (symbol_index, symbol) in symbol_strings.iter().enumerate() {
phf_map.entry(symbol, format!("Symbol::new({})", symbol_index as u32).as_str());
}
let phf_map_built = phf_map.build();
let phf_map_text = phf_map_built.to_string();
let phf_map_expr = syn::parse_str::<syn::Expr>(&phf_map_text).unwrap();

let output = quote! {
const SYMBOL_DIGITS_BASE: u32 = #digits_base;

Expand All @@ -222,6 +235,8 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
])
}
}

static STATIC_SYMBOLS_PHF: ::phf::Map<&'static str, Symbol> = #phf_map_expr;
};

(output, errors.list)
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tracing = "0.1"
sha-1 = "0.9"
sha2 = "0.9"
md-5 = "0.9"
phf = "0.8"
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
#![feature(crate_visibility_modifier)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(hash_raw_entry)]
#![feature(negative_impls)]
#![feature(nll)]
#![feature(min_specialization)]
#![feature(option_expect_none)]
#![feature(zzz)]

#[macro_use]
extern crate rustc_macros;
Expand Down
Loading