1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

perf(ext/web): optimize structuredClone without transferables (#20730)

This PR optimizes `structuredClone` when it's called without
transferables.

### Benchmarks

**main**
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.37.1 (x86_64-unknown-linux-gnu)

benchmark                          time (avg)        iter/s             (min … max)       p75       p99      p995
----------------------------------------------------------------------------------- -----------------------------
structuredClone object              1.64 µs/iter     611,086.0     (1.58 µs … 1.84 µs)   1.66 µs   1.84 µs   1.84 µs
structuredClone transferables       2.82 µs/iter     354,281.4     (2.78 µs … 2.92 µs)   2.84 µs   2.92 µs   2.92 µs
```

**this PR**

```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.37.1 (x86_64-unknown-linux-gnu)

structuredClone object                 1 µs/iter     998,383.5    (971.28 ns … 1.2 µs)      1 µs    1.2 µs    1.2 µs
structuredClone transferables       2.82 µs/iter     355,087.5      (2.7 µs … 3.07 µs)   2.83 µs   3.07 µs   3.07 µs
```

```js
Deno.bench("structuredClone object", () => {
  structuredClone({ foo: "bar" });
});

Deno.bench("structuredClone transferables", () => {
  const buf = new Uint8Array([97]);
  structuredClone(buf, {
    transfer: [buf.buffer],
  });
});
```
This commit is contained in:
Marcos Casagrande 2023-10-06 23:21:48 +02:00 committed by GitHub
parent cba5ae45c2
commit ceecd8c495
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -205,10 +205,12 @@ function opCreateEntangledMessagePort() {
function deserializeJsMessageData(messageData) {
/** @type {object[]} */
const transferables = [];
const hostObjects = [];
const arrayBufferIdsInTransferables = [];
const transferredArrayBuffers = [];
let options;
if (messageData.transferables.length > 0) {
const hostObjects = [];
for (let i = 0; i < messageData.transferables.length; ++i) {
const transferable = messageData.transferables[i];
switch (transferable.kind) {
@ -229,10 +231,13 @@ function deserializeJsMessageData(messageData) {
}
}
const data = core.deserialize(messageData.data, {
options = {
hostObjects,
transferredArrayBuffers,
});
};
}
const data = core.deserialize(messageData.data, options);
for (let i = 0; i < arrayBufferIdsInTransferables.length; ++i) {
const id = arrayBufferIdsInTransferables[i];
@ -248,13 +253,16 @@ function deserializeJsMessageData(messageData) {
* @returns {messagePort.MessageData}
*/
function serializeJsMessageData(data, transferables) {
let options;
const transferredArrayBuffers = [];
if (transferables.length > 0) {
const hostObjects = [];
for (let i = 0, j = 0; i < transferables.length; i++) {
const ab = transferables[i];
if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, ab)) {
const t = transferables[i];
if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, t)) {
if (
ArrayBufferPrototypeGetByteLength(ab) === 0 &&
ops.op_arraybuffer_was_detached(ab)
ArrayBufferPrototypeGetByteLength(t) === 0 &&
ops.op_arraybuffer_was_detached(t)
) {
throw new DOMException(
`ArrayBuffer at index ${j} is already detached`,
@ -262,17 +270,19 @@ function serializeJsMessageData(data, transferables) {
);
}
j++;
ArrayPrototypePush(transferredArrayBuffers, ab);
ArrayPrototypePush(transferredArrayBuffers, t);
} else if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, t)) {
ArrayPrototypePush(hostObjects, t);
}
}
const serializedData = core.serialize(data, {
hostObjects: ArrayPrototypeFilter(
transferables,
(a) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, a),
),
options = {
hostObjects,
transferredArrayBuffers,
}, (err) => {
};
}
const serializedData = core.serialize(data, options, (err) => {
throw new DOMException(err, "DataCloneError");
});