0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

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.
This commit is contained in:
Andreu Botella 2022-07-11 17:27:33 +02:00 committed by GitHub
parent 83c9714fb2
commit 018ad9b3a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

View file

@ -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;

View file

@ -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>,
}