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

perf(linter-macros): only iterate over all rules once #4672

Merged
Merged
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ debug = false
# Compile macros with some optimizations to make consuming crates build faster
oxc_macros.opt-level = 1
oxc_ast_macros.opt-level = 1
# Compile insta and its dependencies in release mode for faster snapshot tests
# See: https://insta.rs/docs/quickstart/#optional-faster-runs
insta.opt-level = 3
similar.opt-level = 3

[profile.release.package.oxc_wasm]
opt-level = 'z'
Expand Down
32 changes: 20 additions & 12 deletions crates/oxc_macros/src/declare_all_lint_rules.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use convert_case::{Case, Casing};
use itertools::Itertools as _;
use proc_macro::TokenStream;
use quote::quote;
use syn::{
Expand Down Expand Up @@ -37,18 +38,25 @@ impl Parse for AllLintRulesMeta {
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
let AllLintRulesMeta { rules } = metadata;
let use_stmts = rules.iter().map(|rule| &rule.path).collect::<Vec<_>>();
let struct_names = rules.iter().map(|rule| &rule.name).collect::<Vec<_>>();
let plugin_names = rules.iter().map(|node| {
node.path
.segments
.iter()
.take(node.path.segments.len() - 1)
.map(|s| format!("{}", s.ident))
.collect::<Vec<_>>()
.join("/")
});
let ids = rules.iter().enumerate().map(|(i, _)| i).collect::<Vec<_>>();

let mut use_stmts = Vec::with_capacity(rules.len());
let mut struct_names = Vec::with_capacity(rules.len());
let mut plugin_names = Vec::with_capacity(rules.len());
let mut ids = Vec::with_capacity(rules.len());

for (i, rule) in rules.iter().enumerate() {
use_stmts.push(&rule.path);
struct_names.push(&rule.name);
plugin_names.push(
rule.path
.segments
.iter()
.take(rule.path.segments.len() - 1)
.map(|s| format!("{}", s.ident))
.join("/"),
);
ids.push(i);
}

let expanded = quote! {
#(pub use self::#use_stmts::#struct_names;)*
Expand Down