Unverified Commit 9b263e98 authored by xusd320's avatar xusd320 Committed by GitHub

perf(turbopack-node): zero-copy Buffer transfer from node to rust (#91376)

Use `Bytes::from_owner(napi::Buffer)` to eliminate the `Buffer →
Vec<u8>` memcpy on the JS→Rust path in the worker pool.

`bytes::Bytes::from_owner` (>=1.9.0) wraps any `T: AsRef<[u8]> + Send +
'static` without copying — the data pointer comes from `T::as_ref()` and
`T` is dropped when the `Bytes` is dropped. Since napi@2's `Buffer`
implements `AsRef<[u8]>`, this gives true zero-copy.

Also unifies the `Operation` trait to use `Bytes` for both `send` and
`recv`.

This also helps reduce memory consumption, without zero-copy, the memory
consumption of the data returned by the loader would be doubled until GC
on the JS side or dropped on the Rust side.
parent ad66dbc8
......@@ -10230,6 +10230,7 @@ dependencies = [
"async-trait",
"base64 0.21.4",
"bincode 2.0.1",
"bytes",
"const_format",
"dashmap 6.1.0",
"either",
......
......@@ -26,6 +26,7 @@ async-trait = { workspace = true }
dashmap = { workspace = true }
base64 = "0.21.0"
bincode = { workspace = true }
bytes = { workspace = true }
const_format = { workspace = true }
either = { workspace = true, features = ["serde"] }
futures = { workspace = true }
......
......@@ -5,6 +5,7 @@ use std::{
use anyhow::{Result, bail};
use bincode::{Decode, Encode};
use bytes::Bytes;
use futures_retry::{FutureRetry, RetryPolicy};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_json::Value as JsonValue;
......@@ -121,9 +122,9 @@ pub trait EvaluateOperation: Send + Sync {
#[async_trait::async_trait]
pub trait Operation: Send {
async fn recv(&mut self) -> Result<Vec<u8>>;
async fn recv(&mut self) -> Result<Bytes>;
async fn send(&mut self, data: Vec<u8>) -> Result<()>;
async fn send(&mut self, data: Bytes) -> Result<()>;
async fn wait_or_kill(&mut self) -> Result<ExitStatus>;
......@@ -374,11 +375,11 @@ pub async fn custom_evaluate(evaluate_context: impl EvaluateContext) -> Result<V
|| async {
let mut operation = pool.operation().await?;
operation
.send(serde_json::to_vec(
.send(Bytes::from(serde_json::to_vec(
&EvalJavaScriptOutgoingMessage::Evaluate {
args: args.iter().map(|v| &**v).collect(),
},
)?)
)?))
.await?;
Ok(operation)
},
......@@ -567,24 +568,24 @@ async fn pull_operation<T: EvaluateContext>(
{
Ok(response) => {
operation
.send(serde_json::to_vec(
.send(Bytes::from(serde_json::to_vec(
&EvalJavaScriptOutgoingMessage::Result {
id,
error: None,
data: Some(serde_json::to_value(response)?),
},
)?)
)?))
.await?;
}
Err(e) => {
operation
.send(serde_json::to_vec(
.send(Bytes::from(serde_json::to_vec(
&EvalJavaScriptOutgoingMessage::Result {
id,
error: Some(PrettyPrintError(&e).to_string()),
data: None,
},
)?)
)?))
.await?;
}
}
......
......@@ -8,6 +8,7 @@ use std::{
};
use anyhow::{Context, Result, bail};
use bytes::Bytes;
use futures::join;
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
......@@ -416,7 +417,7 @@ impl NodeJsPoolProcess {
Ok(process)
}
async fn recv(&mut self) -> Result<Vec<u8>> {
async fn recv(&mut self) -> Result<Bytes> {
let connection = &mut self.connection;
async fn with_timeout<T, E: Into<anyhow::Error>>(
debug: bool,
......@@ -448,20 +449,19 @@ impl NodeJsPoolProcess {
with_timeout(debug, true, connection.read_exact(&mut packet_data))
.await
.context("reading packet data")?;
Ok::<_, anyhow::Error>(packet_data)
Ok::<_, anyhow::Error>(packet_data.into())
};
let (result, stdout, stderr) = join!(
recv_future,
self.stdout_handler.handle_operation(),
self.stderr_handler.handle_operation(),
);
let result = result?;
let result: Bytes = result?;
stdout.context("unable to handle stdout from the Node.js process in a structured way")?;
stderr.context("unable to handle stderr from the Node.js process in a structured way")?;
Ok(result)
}
async fn send(&mut self, packet_data: Vec<u8>) -> Result<()> {
async fn send(&mut self, packet_data: Bytes) -> Result<()> {
self.connection
.write_u32(
packet_data
......@@ -798,16 +798,16 @@ pub struct ChildProcessOperation {
#[async_trait::async_trait]
impl Operation for ChildProcessOperation {
async fn recv(&mut self) -> Result<Vec<u8>> {
let vec = self
async fn recv(&mut self) -> Result<Bytes> {
let bytes = self
.with_process(|process| async move {
process.recv().await.context("failed to receive message")
})
.await?;
Ok(vec)
Ok(bytes)
}
async fn send(&mut self, message: Vec<u8>) -> Result<()> {
async fn send(&mut self, message: Bytes) -> Result<()> {
self.with_process(|process| async move {
timeout(Duration::from_secs(30), process.send(message))
.await
......
......@@ -4,6 +4,7 @@ use std::{
};
use anyhow::{Context, Result};
use bytes::Bytes;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use tokio::sync::{
......@@ -67,15 +68,15 @@ pub(super) struct WorkerOptions {
#[allow(dead_code)]
pub(super) struct TaskMessage {
pub task_id: u32,
pub data: Vec<u8>,
pub data: Bytes,
}
#[derive(Default)]
pub(crate) struct WorkerPoolOperation {
#[allow(clippy::type_complexity)]
worker_routed_channel: Mutex<FxHashMap<u32, Arc<MessageChannel<(u32, Vec<u8>)>>>>,
worker_routed_channel: Mutex<FxHashMap<u32, Arc<MessageChannel<(u32, Bytes)>>>>,
#[allow(clippy::type_complexity)]
task_routed_channel: Mutex<FxHashMap<u32, Arc<MessageChannel<Vec<u8>>>>>,
task_routed_channel: Mutex<FxHashMap<u32, Arc<MessageChannel<Bytes>>>>,
pub(crate) pools: Mutex<FxHashMap<Arc<WorkerOptions>, Arc<PoolState>>>,
}
......@@ -151,10 +152,7 @@ impl WorkerPoolOperation {
self.worker_routed_channel.lock().remove(&worker_id);
}
pub(crate) async fn recv_task_message_in_worker(
&self,
worker_id: u32,
) -> Result<(u32, Vec<u8>)> {
pub(crate) async fn recv_task_message_in_worker(&self, worker_id: u32) -> Result<(u32, Bytes)> {
let channel = {
let mut map = self.worker_routed_channel.lock();
map.entry(worker_id)
......@@ -196,9 +194,9 @@ pub(crate) async fn get_pool_state(worker_options: Arc<WorkerOptions>) -> Arc<Po
/// Holds Arc references to avoid HashMap lookups during send/recv.
pub(crate) struct TaskChannels {
/// Channel for Rust -> Worker communication (task_id, data)
worker_channel: Arc<MessageChannel<(u32, Vec<u8>)>>,
worker_channel: Arc<MessageChannel<(u32, Bytes)>>,
/// Channel for Worker -> Rust communication (data)
task_channel: Arc<MessageChannel<Vec<u8>>>,
task_channel: Arc<MessageChannel<Bytes>>,
task_id: u32,
}
......@@ -228,7 +226,7 @@ impl TaskChannels {
}
/// Send message to worker (Rust -> JS Worker)
pub(crate) async fn send_to_worker(&self, message: Vec<u8>) -> Result<()> {
pub(crate) async fn send_to_worker(&self, message: Bytes) -> Result<()> {
self.worker_channel
.send((self.task_id, message))
.await
......@@ -236,7 +234,7 @@ impl TaskChannels {
}
/// Receive message from worker (JS Worker -> Rust)
pub(crate) async fn recv_from_worker(&self) -> Result<Vec<u8>> {
pub(crate) async fn recv_from_worker(&self) -> Result<Bytes> {
self.task_channel
.recv()
.await
......@@ -275,11 +273,11 @@ impl Drop for WorkerOperation {
#[async_trait::async_trait]
impl Operation for WorkerOperation {
async fn recv(&mut self) -> Result<Vec<u8>> {
async fn recv(&mut self) -> Result<Bytes> {
self.channels.recv_from_worker().await
}
async fn send(&mut self, message: Vec<u8>) -> Result<()> {
async fn send(&mut self, message: Bytes) -> Result<()> {
self.channels.send_to_worker(message).await
}
......
use std::{collections::VecDeque, sync::Arc};
use bytes::Bytes;
use napi::{
Env,
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
......@@ -152,7 +153,7 @@ impl From<NapiTaskMessage> for TaskMessage {
let NapiTaskMessage { task_id, data } = message;
TaskMessage {
task_id,
data: data.into(),
data: Bytes::from_owner(data),
}
}
}
......@@ -166,7 +167,7 @@ pub async fn recv_task_message_in_worker(worker_id: u32) -> napi::Result<NapiTas
.await?;
Ok(NapiTaskMessage {
task_id,
data: message.into(),
data: Vec::from(message).into(),
})
}
......
......@@ -5,6 +5,7 @@
use std::path::PathBuf;
use anyhow::Result;
use bytes::Bytes;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
......@@ -71,9 +72,10 @@ struct Echo {
pid: u32,
}
/// Helper: send a message and return the echo response.
async fn send_recv(op: &mut Box<dyn Operation>, msg: serde_json::Value) -> Echo {
op.send(serde_json::to_vec(&msg).unwrap()).await.unwrap();
op.send(Bytes::from(serde_json::to_vec(&msg).unwrap()))
.await
.unwrap();
serde_json::from_slice(&op.recv().await.unwrap()).unwrap()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment