Skip to content

Commit 3166233

Browse files
committed
perf(linter/plugins): remove Arcs (#15431)
Remove 2 levels of `Arc`s from `ExternalLinter`. 1. Make wrapped callback functions `Box<dyn Fn>` instead of `Arc<dyn Fn>`. 2. Avoid wrapping `ThreadsafeFunction`s in `Arc`. The closures take ownership of them anyway. In `wrap_load_plugin`, make the inner closure borrow the `ThreadsafeFunction` from the outer closure. The first of these (and possibly the 2nd too) was prevented previously by `#[derive(Clone)]` on `Linter`. `Linter` doesn't appear to need to be cloned, so remove that derive.
1 parent 4286195 commit 3166233

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

apps/oxlint/src/js_plugins/external_linter.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{Arc, atomic::Ordering, mpsc::channel};
1+
use std::sync::{atomic::Ordering, mpsc::channel};
22

33
use napi::{
44
Status,
@@ -36,10 +36,9 @@ pub fn create_external_linter(
3636
///
3737
/// The returned function will panic if called outside of a Tokio runtime.
3838
fn wrap_load_plugin(cb: JsLoadPluginCb) -> ExternalLinterLoadPluginCb {
39-
let cb = Arc::new(cb);
40-
Arc::new(move |plugin_path, package_name| {
41-
let cb = Arc::clone(&cb);
42-
tokio::task::block_in_place(move || {
39+
Box::new(move |plugin_path, package_name| {
40+
let cb = &cb;
41+
tokio::task::block_in_place(|| {
4342
tokio::runtime::Handle::current().block_on(async move {
4443
let result = cb
4544
.call_async(FnArgs::from((plugin_path, package_name)))
@@ -70,14 +69,11 @@ pub enum LintFileReturnValue {
7069
/// Use an `mpsc::channel` to wait for the result from JS side, and block current thread until `lintFile`
7170
/// completes execution.
7271
fn wrap_lint_file(cb: JsLintFileCb) -> ExternalLinterLintFileCb {
73-
let cb = Arc::new(cb);
74-
Arc::new(
72+
Box::new(
7573
move |file_path: String,
7674
rule_ids: Vec<u32>,
7775
settings_json: String,
7876
allocator: &Allocator| {
79-
let cb = Arc::clone(&cb);
80-
8177
let (tx, rx) = channel();
8278

8379
// SAFETY: This function is only called when an `ExternalLinter` exists.

crates/oxc_linter/src/external_linter.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::{fmt::Debug, sync::Arc};
1+
use std::fmt::Debug;
22

33
use serde::Deserialize;
44

55
use oxc_allocator::Allocator;
66

7-
pub type ExternalLinterLoadPluginCb = Arc<
7+
pub type ExternalLinterLoadPluginCb = Box<
88
dyn Fn(
99
String,
1010
Option<String>,
@@ -13,7 +13,7 @@ pub type ExternalLinterLoadPluginCb = Arc<
1313
+ Sync,
1414
>;
1515

16-
pub type ExternalLinterLintFileCb = Arc<
16+
pub type ExternalLinterLintFileCb = Box<
1717
dyn Fn(String, Vec<u32>, String, &Allocator) -> Result<Vec<LintFileResult>, String>
1818
+ Sync
1919
+ Send,
@@ -47,7 +47,6 @@ pub struct JsFix {
4747
pub text: String,
4848
}
4949

50-
#[derive(Clone)]
5150
pub struct ExternalLinter {
5251
pub(crate) load_plugin: ExternalLinterLoadPluginCb,
5352
pub(crate) lint_file: ExternalLinterLintFileCb,

crates/oxc_linter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn size_asserts() {
9494
assert_eq!(size_of::<RuleEnum>(), 16);
9595
}
9696

97-
#[derive(Debug, Clone)]
97+
#[derive(Debug)]
9898
#[expect(clippy::struct_field_names)]
9999
pub struct Linter {
100100
options: LintOptions,

0 commit comments

Comments
 (0)