From 91b2df02b28714a4cf9dac55c8d0927784fd07ad Mon Sep 17 00:00:00 2001 From: Ulrich Stark Date: Thu, 8 May 2025 21:08:18 +0200 Subject: [PATCH] refactor(language_server): avoid cloning linter by taking reference in LintService --- apps/oxlint/src/lint.rs | 12 +++++------- .../src/linter/isolated_lint_handler.rs | 4 ++-- crates/oxc_linter/src/service/mod.rs | 12 ++++-------- crates/oxc_linter/src/service/runtime.rs | 8 ++++---- crates/oxc_linter/src/tester.rs | 2 +- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 3fea36e452f3e..8208581bced80 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -293,18 +293,16 @@ impl Runner for LintRunner { } } - let mut lint_service = LintService::new(linter, options); let mut diagnostic_service = Self::get_diagnostic_service(&output_formatter, &warning_options, &misc_options); + let tx_error = diagnostic_service.sender().clone(); - let number_of_rules = lint_service.linter().number_of_rules(); + let number_of_rules = linter.number_of_rules(); // Spawn linting in another thread so diagnostics can be printed immediately from diagnostic_service.run. - rayon::spawn({ - let tx_error = diagnostic_service.sender().clone(); - move || { - lint_service.run(&tx_error); - } + rayon::spawn(move || { + let mut lint_service = LintService::new(&linter, options); + lint_service.run(&tx_error); }); let diagnostic_result = diagnostic_service.run(stdout); diff --git a/crates/oxc_language_server/src/linter/isolated_lint_handler.rs b/crates/oxc_language_server/src/linter/isolated_lint_handler.rs index 92f2c1cf19754..252df8a9dee53 100644 --- a/crates/oxc_language_server/src/linter/isolated_lint_handler.rs +++ b/crates/oxc_language_server/src/linter/isolated_lint_handler.rs @@ -137,9 +137,9 @@ impl IsolatedLintHandler { vec![Arc::from(path.as_os_str())], ) .with_cross_module(self.options.use_cross_module); - // ToDo: do not clone the linter + let mut lint_service = - LintService::new(self.linter.clone(), lint_service_options).with_file_system(Box::new( + LintService::new(&self.linter, lint_service_options).with_file_system(Box::new( IsolatedLintHandlerFileSystem::new(path.to_path_buf(), source_text), )); let result = lint_service.run_source(allocator); diff --git a/crates/oxc_linter/src/service/mod.rs b/crates/oxc_linter/src/service/mod.rs index 11b6ba2b1c90a..9d8c79f31f276 100644 --- a/crates/oxc_linter/src/service/mod.rs +++ b/crates/oxc_linter/src/service/mod.rs @@ -65,12 +65,12 @@ impl LintServiceOptions { } } -pub struct LintService { - runtime: Runtime, +pub struct LintService<'l> { + runtime: Runtime<'l>, } -impl LintService { - pub fn new(linter: Linter, options: LintServiceOptions) -> Self { +impl<'l> LintService<'l> { + pub fn new(linter: &'l Linter, options: LintServiceOptions) -> Self { let runtime = Runtime::new(linter, options); Self { runtime } } @@ -84,10 +84,6 @@ impl LintService { self } - pub fn linter(&self) -> &Linter { - &self.runtime.linter - } - /// # Panics pub fn run(&mut self, tx_error: &DiagnosticSender) { self.runtime.run(tx_error); diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index ee06fbef518a8..17cb06ad141a1 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -38,11 +38,11 @@ use crate::fixer::{FixWithPosition, MessageWithPosition}; #[cfg(feature = "language_server")] use crate::service::offset_to_position::{SpanPositionMessage, offset_to_position}; -pub struct Runtime { +pub struct Runtime<'l> { cwd: Box, /// All paths to lint paths: IndexSet, FxBuildHasher>, - pub(super) linter: Linter, + pub(super) linter: &'l Linter, resolver: Option, pub(super) file_system: Box, @@ -163,8 +163,8 @@ impl RuntimeFileSystem for OsFileSystem { } } -impl Runtime { - pub(super) fn new(linter: Linter, options: LintServiceOptions) -> Self { +impl<'l> Runtime<'l> { + pub(super) fn new(linter: &'l Linter, options: LintServiceOptions) -> Self { let resolver = options.cross_module.then(|| { Self::get_resolver(options.tsconfig.or_else(|| Some(options.cwd.join("tsconfig.json")))) }); diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index c0dbc2c19474b..09001da8827bb 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -515,7 +515,7 @@ impl Tester { let paths = vec![Arc::::from(path_to_lint.as_os_str())]; let options = LintServiceOptions::new(cwd, paths).with_cross_module(self.plugins.has_import()); - let mut lint_service = LintService::new(linter, options).with_file_system(Box::new( + let mut lint_service = LintService::new(&linter, options).with_file_system(Box::new( TesterFileSystem::new(path_to_lint, source_text.to_string()), ));