perf(turbopack-node): sync zero-copy Buffer transfer from upstream PR #91376#120
perf(turbopack-node): sync zero-copy Buffer transfer from upstream PR #91376#120
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the performance of data transfer between JavaScript and Rust components within the Turbopack Node.js worker pool. By adopting a zero-copy strategy for Buffer transfers and standardizing data handling with the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a performance optimization by enabling zero-copy buffer transfers from JavaScript to Rust using bytes::Bytes and Bytes::from_owner. The core change to unify the Operation trait is well-implemented. However, this unification introduces new memory copies on other code paths, particularly for ChildProcessOperation and for data transfers from Rust back to JavaScript workers. I've added specific comments highlighting these performance regressions for your consideration. Additionally, I've suggested a minor change in a test file to improve code consistency.
| async fn send(&mut self, message: Bytes) -> Result<()> { | ||
| self.with_process(|process| async move { | ||
| timeout(Duration::from_secs(30), process.send(message)) | ||
| timeout(Duration::from_secs(30), process.send(message.to_vec())) | ||
| .await | ||
| .context("timeout while sending message")? | ||
| .context("failed to send message")?; |
There was a problem hiding this comment.
The unification of the Operation trait to use bytes::Bytes appears to introduce a performance regression for ChildProcessOperation. The send method now needs to copy the Bytes into a Vec<u8> using message.to_vec(), which adds an allocation and copy for every message sent. Was this trade-off considered? Given this is a performance-focused PR, it would be good to clarify if this regression is acceptable.
| @@ -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(), | |||
There was a problem hiding this comment.
This change from message.into() (on what was a Vec<u8>) to Vec::from(message).into() (where message is now Bytes) introduces a copy for data transfer from Rust to the JS worker. While the JS-to-Rust path is now zero-copy, this change regresses performance on the Rust-to-JS path. Is this an intended trade-off?
| op.send(bytes::Bytes::from(serde_json::to_vec(&msg).unwrap())) | ||
| .await | ||
| .unwrap(); |
There was a problem hiding this comment.
For consistency with other files in this PR, it would be better to add use bytes::Bytes; at the top of the file and use Bytes::from(...) here instead of the fully qualified bytes::Bytes::from(...). This aligns with one of the upstream commits (refactor: use Bytes import instead of fully-qualified bytes::Bytes).
| op.send(bytes::Bytes::from(serde_json::to_vec(&msg).unwrap())) | |
| .await | |
| .unwrap(); | |
| op.send(Bytes::from(serde_json::to_vec(&msg).unwrap())) | |
| .await | |
| .unwrap(); |
a1ebf5f to
8ce8349
Compare
Cherry-picked from PR vercel#91376: - a6ae253 feat(turbopack-node): zero-copy Buffer transfer using Bytes::from_owner - 8791718 refactor: use Bytes import instead of fully-qualified bytes::Bytes Use Bytes::from_owner(napi::Buffer) to eliminate the Buffer → Vec<u8> memcpy on the JS→Rust path in the worker pool. Unifies the Operation trait to use Bytes for both send and recv.
8ce8349 to
d247ae4
Compare
Syncs changes from upstream PR vercel#91376 into the utoo branch.
Changes
Cherry-picked the two upstream commits:
a6ae253feat(turbopack-node): zero-copy Buffer transfer usingBytes::from_owner8791718refactor: useBytesimport instead of fully-qualifiedbytes::BytesPlus an additional fix for the utoo-specific wasm worker (
web_worker.rs):use bytes::BytesimportUint8Array::to_vec()inBytes::from()insend_task_messageto match the newTaskMessage.data: BytestypeSummary
Uses
Bytes::from_owner(napi::Buffer)to eliminate theBuffer → Vec<u8>memcpy on the JS→Rust path in the worker pool. Unifies theOperationtrait to useBytesfor bothsendandrecv.