Skip to content

Commit 1b01f4c

Browse files
committed
perf(linter): move up rule retainment into initial collection to reduce allocation size
1 parent b27c5b9 commit 1b01f4c

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

crates/oxc_linter/src/lib.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,32 +172,43 @@ impl Linter {
172172
.is_some_and(|ext| LINT_PARTIAL_LOADER_EXTENSIONS.iter().any(|e| e == &ext));
173173

174174
loop {
175-
let mut rules = rules
175+
let semantic = ctx_host.semantic();
176+
177+
#[cfg(debug_assertions)]
178+
let all_rules = rules
176179
.iter()
177180
.filter(|(rule, _)| rule.should_run(&ctx_host) && !rule.is_tsgolint_rule())
178181
.map(|(rule, severity)| (rule, Rc::clone(&ctx_host).spawn(rule, *severity)))
179182
.collect::<Vec<_>>();
180183

181-
let semantic = ctx_host.semantic();
184+
let rules = rules
185+
.iter()
186+
.filter(|(rule, _)| {
187+
if rule.is_tsgolint_rule() {
188+
return false;
189+
}
190+
191+
// If only the `run` function is implemented, we can skip running the file entirely if the current
192+
// file does not contain any of the relevant AST node types.
193+
if rule.run_info() == RuleRunFunctionsImplemented::Run
194+
&& let Some(ast_types) = rule.types_info()
195+
&& !semantic.nodes().contains_any(ast_types)
196+
{
197+
return false;
198+
}
199+
200+
rule.should_run(&ctx_host)
201+
})
202+
.map(|(rule, severity)| (rule, Rc::clone(&ctx_host).spawn(rule, *severity)))
203+
.collect::<Vec<_>>();
182204

183205
let should_run_on_jest_node =
184206
ctx_host.plugins().has_test() && ctx_host.frameworks().is_test();
185207

186-
let mut execute_rules = |with_runtime_optimization: bool| {
187-
// If only the `run` function is implemented, we can skip running the file entirely if the current
188-
// file does not contain any of the relevant AST node types.
189-
if with_runtime_optimization {
190-
rules.retain(|(rule, _)| {
191-
let run_info = rule.run_info();
192-
if run_info == RuleRunFunctionsImplemented::Run
193-
&& let Some(ast_types) = rule.types_info()
194-
{
195-
semantic.nodes().contains_any(ast_types)
196-
} else {
197-
true
198-
}
199-
});
200-
}
208+
let execute_rules = |with_runtime_optimization: bool| {
209+
#[cfg(debug_assertions)]
210+
let rules =
211+
if with_runtime_optimization { rules.clone() } else { all_rules.clone() };
201212
// IMPORTANT: We have two branches here for performance reasons:
202213
//
203214
// 1) Branch where we iterate over each node, then each rule

0 commit comments

Comments
 (0)