summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreu Botella <andreu@andreubotella.com>2022-07-11 17:27:33 +0200
committerGitHub <noreply@github.com>2022-07-11 17:27:33 +0200
commit018ad9b3a4f2e5c8597274f61262c6035bdb560d (patch)
tree5dba5fd5a9a9e8fd818532b66ead5c5721e32508
parent83c9714fb2f401e82a6c2e784a43130818e8282d (diff)
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.
-rw-r--r--core/lib.rs1
-rw-r--r--ext/web/message_port.rs10
2 files changed, 6 insertions, 5 deletions
diff --git a/core/lib.rs b/core/lib.rs
index 064c15fc1..ab22392c4 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 06f7e3c52..48c899216 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<u8>, Vec<Transferable>);
+type MessagePortMessage = (DetachedBuffer, Vec<Transferable>);
pub struct MessagePort {
rx: RefCell<UnboundedReceiver<MessagePortMessage>>,
@@ -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<JsTransferable>,
}