From 018ad9b3a4f2e5c8597274f61262c6035bdb560d Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Mon, 11 Jul 2022 17:27:33 +0200 Subject: [PATCH] chore(web, worker): Use `DetachedBuffer` for `postMessage` ops (#15133) This commit uses `DetachedBuffer` instead of `ZeroCopyBuf` in the ops that back `Worker.prototype.postMessage` and `MessagePort.prototype.postMessage`. This is done because the serialized buffer is then copied to the destination isolate, even though it is internal to runtime code and not used for anything else, so detaching it and transferring it instead saves an unnecessary copy. --- core/lib.rs | 1 + ext/web/message_port.rs | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/lib.rs b/core/lib.rs index 064c15fc13..ab22392c49 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -27,6 +27,7 @@ pub use serde; pub use serde_json; pub use serde_v8; pub use serde_v8::ByteString; +pub use serde_v8::DetachedBuffer; pub use serde_v8::StringOrBuffer; pub use serde_v8::U16String; pub use serde_v8::ZeroCopyBuf; diff --git a/ext/web/message_port.rs b/ext/web/message_port.rs index 06f7e3c522..48c899216a 100644 --- a/ext/web/message_port.rs +++ b/ext/web/message_port.rs @@ -6,7 +6,7 @@ use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op; -use deno_core::ZeroCopyBuf; +use deno_core::DetachedBuffer; use deno_core::{CancelFuture, Resource}; use deno_core::{CancelHandle, OpState}; use deno_core::{RcRef, ResourceId}; @@ -21,7 +21,7 @@ enum Transferable { ArrayBuffer(u32), } -type MessagePortMessage = (Vec, Vec); +type MessagePortMessage = (DetachedBuffer, Vec); pub struct MessagePort { rx: RefCell>, @@ -40,7 +40,7 @@ impl MessagePort { // Swallow the failed to send error. It means the channel was disentangled, // but not cleaned up. if let Some(tx) = &*self.tx.borrow() { - tx.send((data.data.to_vec(), transferables)).ok(); + tx.send((data.data, transferables)).ok(); } Ok(()) @@ -59,7 +59,7 @@ impl MessagePort { let js_transferables = serialize_transferables(&mut state.borrow_mut(), transferables); return Ok(Some(JsMessageData { - data: ZeroCopyBuf::from(data), + data, transferables: js_transferables, })); } @@ -182,7 +182,7 @@ fn serialize_transferables( #[derive(Deserialize, Serialize)] pub struct JsMessageData { - data: ZeroCopyBuf, + data: DetachedBuffer, transferables: Vec, }