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