Skip to content

Commit 56a99d5

Browse files
committed
refactor(oxfmt): simplify wrap_format_embeded (#15798)
`call_async` does the same thing as `call_with_return_value` + `channel`, so use `call_async` to simplify it and reduce some unnecessary overhead ### Benchmark I benchmarked this PR locally, and the results indicate that it is 9% faster than before in the `Outline` repo. EDIT: Maybe just a noise since `Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interference from other programs.` was displayed. ```shell Setup complete! Run 'pnpm run bench' to start benchmarking. ========================================= JavaScript/TypeScript Formatter Benchmark ========================================= Formatters: Prettier, Biome, Oxfmt Starting benchmark with: - 3 warmup runs - 10 benchmark runs - Git reset before each run ========================================= Benchmarking parser.ts (single large file) ========================================= Benchmark 1: biome Time (mean ± σ): 69.5 ms ± 0.9 ms [User: 55.4 ms, System: 9.7 ms] Range (min … max): 68.3 ms … 71.2 ms 10 runs Benchmark 2: oxfmt-new Time (mean ± σ): 30.7 ms ± 0.3 ms [User: 23.7 ms, System: 11.0 ms] Range (min … max): 30.3 ms … 31.3 ms 10 runs Benchmark 3: oxfmt-old Time (mean ± σ): 30.4 ms ± 0.3 ms [User: 23.6 ms, System: 10.6 ms] Range (min … max): 30.1 ms … 30.9 ms 10 runs Summary oxfmt-old ran 1.01 ± 0.01 times faster than oxfmt-new 2.29 ± 0.04 times faster than biome ========================================= Benchmarking Outline repository ========================================= Benchmark 1: biome Time (mean ± σ): 154.6 ms ± 6.0 ms [User: 825.2 ms, System: 555.8 ms] Range (min … max): 147.5 ms … 164.0 ms 10 runs Benchmark 2: oxfmt-new Time (mean ± σ): 65.8 ms ± 2.3 ms [User: 196.0 ms, System: 265.3 ms] Range (min … max): 60.7 ms … 68.8 ms 10 runs Benchmark 3: oxfmt-old Time (mean ± σ): 72.0 ms ± 17.7 ms [User: 195.0 ms, System: 277.3 ms] Range (min … max): 62.6 ms … 121.1 ms 10 runs Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. Summary oxfmt-new ran 1.09 ± 0.27 times faster than oxfmt-old 2.35 ± 0.12 times faster than biome ```
1 parent 4fe3aac commit 56a99d5

File tree

1 file changed

+15
-36
lines changed

1 file changed

+15
-36
lines changed

apps/oxfmt/src/prettier_plugins/external_formatter.rs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use napi::{
44
Status,
55
bindgen_prelude::{FnArgs, Promise, block_on},
6-
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode},
6+
threadsafe_function::ThreadsafeFunction,
77
};
88
use oxc_formatter::EmbeddedFormatterCallback;
99

@@ -51,43 +51,22 @@ impl ExternalFormatter {
5151
///
5252
/// Uses a channel to capture the result from the JS callback.
5353
pub fn wrap_format_embedded(cb: JsFormatEmbeddedCb) -> EmbeddedFormatterCallback {
54-
let cb = Arc::new(cb);
5554
Arc::new(move |tag_name: &str, code: &str| {
56-
let cb = Arc::clone(&cb);
57-
58-
// Use a channel to capture the result from the JS callback
59-
let (tx, rx) = std::sync::mpsc::channel();
60-
61-
let tag_name_str = tag_name.to_string();
62-
let code_str = code.to_string();
63-
64-
// Call the JS function with separate arguments
65-
let status = cb.call_with_return_value(
66-
FnArgs::from((tag_name_str.clone(), code_str)),
67-
ThreadsafeFunctionCallMode::Blocking,
68-
move |result: Result<Promise<String>, napi::Error>, _env| {
69-
// Send the result through the channel
70-
let _ = tx.send(result);
71-
Ok(())
72-
},
73-
);
74-
75-
if status != napi::Status::Ok {
76-
return Err(format!(
77-
"Failed to call JS formatter for tag '{tag_name_str}': {status:?}"
78-
));
79-
}
80-
81-
// Wait for the result from the channel
82-
match rx.recv() {
83-
Ok(Ok(promise)) => block_on(promise).map_err(|e| {
84-
format!("JS formatter promise rejected for tag '{tag_name_str}': {e}")
85-
}),
86-
Ok(Err(e)) => Err(format!("JS formatter failed for tag '{tag_name_str}': {e}")),
87-
Err(_) => {
88-
Err(format!("Failed to receive result from JS formatter for tag '{tag_name_str}'"))
55+
block_on(async {
56+
let status =
57+
cb.call_async(FnArgs::from((tag_name.to_string(), code.to_string()))).await;
58+
match status {
59+
Ok(promise) => match promise.await {
60+
Ok(formatted_code) => Ok(formatted_code),
61+
Err(err) => {
62+
Err(format!("JS formatter promise rejected for tag '{tag_name}': {err}"))
63+
}
64+
},
65+
Err(err) => Err(format!(
66+
"Failed to call JS formatting callback for tag '{tag_name}': {err}"
67+
)),
8968
}
90-
}
69+
})
9170
})
9271
}
9372

0 commit comments

Comments
 (0)