Skip to content

Commit

Permalink
refactor(linter): less a global hashmap to reduce rule timer macro ex…
Browse files Browse the repository at this point in the history
…pansion

closes #819
  • Loading branch information
Boshen committed Aug 31, 2023
1 parent 777cc00 commit 55e0c97
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 35 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions crates/oxc_cli/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ mod test {
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}

#[test]
fn timing() {
let args = &["--timing", "fixtures"];
// make sure this doesn't crash
test(args);
}
}
1 change: 1 addition & 0 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rustc-hash = { workspace = true }
phf = { workspace = true, features = ["macros"] }
num-traits = { workspace = true }
itertools = { workspace = true }
dashmap = { workspace = true }

rust-lapper = "1.1.0"
once_cell = "1.18.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ mod service;

use std::{self, fs, io::Write, rc::Rc, time::Duration};

pub use fixer::{FixResult, Fixer, Message};
pub(crate) use oxc_semantic::AstNode;
use rustc_hash::FxHashMap;

pub use crate::{
context::LintContext,
fixer::Fix,
fixer::{FixResult, Fixer, Message},
options::{AllowWarnDeny, LintOptions},
rule::RuleCategory,
service::{LintService, PathWork},
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rule_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl RuleTimer {
Self { secs: AtomicU64::new(0), nanos: AtomicU32::new(0) }
}

pub fn update(&mut self, duration: &Duration) {
pub fn update(&self, duration: &Duration) {
self.secs.fetch_add(duration.as_secs(), Ordering::SeqCst);
self.nanos.fetch_add(duration.subsec_nanos(), Ordering::SeqCst);
}
Expand Down
60 changes: 27 additions & 33 deletions crates/oxc_macros/src/declare_all_lint_rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod trie;

use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
Result,
Expand Down Expand Up @@ -62,10 +62,6 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
.collect::<Vec<_>>()
.join("/")
});
let rule_timer = rules
.iter()
.map(|node| format_ident!("RuleTimer{}", node.name.to_string().to_case(Case::Pascal)))
.collect::<Vec<_>>();

quote! {
#(#use_stmts)*
Expand Down Expand Up @@ -115,47 +111,39 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {

pub fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>, print_execution_times: bool) {
let start = print_execution_times.then(|| Instant::now());
match self {
#(Self::#struct_names(rule) => {
let diagnostics = rule.run(node, ctx);
if let Some(start) = start {
unsafe { #rule_timer.update(&start.elapsed()) };
}
diagnostics
}),*
let result = match self {
#(Self::#struct_names(rule) => rule.run(node, ctx)),*
};
if let Some(start) = start {
RULE_TIMERS.get(self.name()).unwrap().update(&start.elapsed());
}
result
}

pub fn run_on_symbol<'a>(&self, symbol_id: SymbolId, ctx: &LintContext<'a>, print_execution_times: bool) {
let start = print_execution_times.then(|| Instant::now());
match self {
#(Self::#struct_names(rule) => {
let diagnostics = rule.run_on_symbol(symbol_id, ctx);
if let Some(start) = start {
unsafe { #rule_timer.update(&start.elapsed()) };
}
diagnostics
}),*
let result = match self {
#(Self::#struct_names(rule) => rule.run_on_symbol(symbol_id, ctx)),*
};
if let Some(start) = start {
RULE_TIMERS.get(self.name()).unwrap().update(&start.elapsed());
}
result
}

pub fn run_once<'a>(&self, ctx: &LintContext<'a>, print_execution_times: bool) {
let start = print_execution_times.then(|| Instant::now());
match self {
#(Self::#struct_names(rule) => {
let diagnostics = rule.run_once(ctx);
if let Some(start) = start {
unsafe { #rule_timer.update(&start.elapsed()) };
}
diagnostics
}),*
let result = match self {
#(Self::#struct_names(rule) => rule.run_once(ctx)),*
};
if let Some(start) = start {
RULE_TIMERS.get(self.name()).unwrap().update(&start.elapsed());
}
result
}

pub fn execute_time(&self) -> Duration {
match self {
#(Self::#struct_names(_) => unsafe { #rule_timer.duration() }),*
}
RULE_TIMERS.get(self.name()).unwrap().duration()
}
}

Expand Down Expand Up @@ -185,7 +173,13 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
}
}

#(pub static mut #rule_timer : RuleTimer = RuleTimer::new());*;
use once_cell::sync::Lazy;
use std::collections::HashMap;
pub static RULE_TIMERS: Lazy<HashMap<&'static str, RuleTimer>> = Lazy::new(|| {
let mut m = HashMap::new();
#(m.insert(#struct_names::NAME, RuleTimer::new());)*
m
});

lazy_static::lazy_static! {
pub static ref RULES: Vec<RuleEnum> = vec![
Expand Down

0 comments on commit 55e0c97

Please sign in to comment.