mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
lib: reduce overhead of blob clone
PR-URL: https://github.com/nodejs/node/pull/50110 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
parent
ea595ebbf2
commit
badba8ceb6
|
|
@ -7,14 +7,17 @@ const assert = require('assert');
|
||||||
|
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
n: [50e3],
|
n: [50e3],
|
||||||
|
bytes: [128, 1024, 1024 ** 2],
|
||||||
});
|
});
|
||||||
|
|
||||||
let _cloneResult;
|
let _cloneResult;
|
||||||
|
|
||||||
function main({ n }) {
|
function main({ n, bytes }) {
|
||||||
|
const buff = Buffer.allocUnsafe(bytes);
|
||||||
|
const blob = new Blob(buff);
|
||||||
bench.start();
|
bench.start();
|
||||||
for (let i = 0; i < n; ++i)
|
for (let i = 0; i < n; ++i)
|
||||||
_cloneResult = structuredClone(new Blob(['hello']));
|
_cloneResult = structuredClone(blob);
|
||||||
bench.end(n);
|
bench.end(n);
|
||||||
|
|
||||||
// Avoid V8 deadcode (elimination)
|
// Avoid V8 deadcode (elimination)
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ const {
|
||||||
ObjectDefineProperty,
|
ObjectDefineProperty,
|
||||||
ObjectSetPrototypeOf,
|
ObjectSetPrototypeOf,
|
||||||
PromiseReject,
|
PromiseReject,
|
||||||
ReflectConstruct,
|
|
||||||
RegExpPrototypeExec,
|
RegExpPrototypeExec,
|
||||||
RegExpPrototypeSymbolReplace,
|
RegExpPrototypeSymbolReplace,
|
||||||
StringPrototypeToLowerCase,
|
StringPrototypeToLowerCase,
|
||||||
|
|
@ -200,7 +199,7 @@ class Blob {
|
||||||
const length = this[kLength];
|
const length = this[kLength];
|
||||||
return {
|
return {
|
||||||
data: { handle, type, length },
|
data: { handle, type, length },
|
||||||
deserializeInfo: 'internal/blob:ClonedBlob',
|
deserializeInfo: 'internal/blob:Blob',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -397,25 +396,18 @@ class Blob {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ClonedBlob() {
|
function TransferableBlob(handle, length, type = '') {
|
||||||
return ReflectConstruct(function() {
|
|
||||||
markTransferMode(this, true, false);
|
|
||||||
}, [], Blob);
|
|
||||||
}
|
|
||||||
ClonedBlob.prototype[kDeserialize] = () => {};
|
|
||||||
|
|
||||||
function TransferrableBlob(handle, length, type = '') {
|
|
||||||
markTransferMode(this, true, false);
|
markTransferMode(this, true, false);
|
||||||
this[kHandle] = handle;
|
this[kHandle] = handle;
|
||||||
this[kType] = type;
|
this[kType] = type;
|
||||||
this[kLength] = length;
|
this[kLength] = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectSetPrototypeOf(TransferrableBlob.prototype, Blob.prototype);
|
ObjectSetPrototypeOf(TransferableBlob.prototype, Blob.prototype);
|
||||||
ObjectSetPrototypeOf(TransferrableBlob, Blob);
|
ObjectSetPrototypeOf(TransferableBlob, Blob);
|
||||||
|
|
||||||
function createBlob(handle, length, type = '') {
|
function createBlob(handle, length, type = '') {
|
||||||
const transferredBlob = new TransferrableBlob(handle, length, type);
|
const transferredBlob = new TransferableBlob(handle, length, type);
|
||||||
|
|
||||||
// Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053
|
// Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053
|
||||||
transferredBlob.constructor = Blob;
|
transferredBlob.constructor = Blob;
|
||||||
|
|
@ -489,7 +481,6 @@ function createBlobFromFilePath(path, options) {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Blob,
|
Blob,
|
||||||
ClonedBlob,
|
|
||||||
createBlob,
|
createBlob,
|
||||||
createBlobFromFilePath,
|
createBlobFromFilePath,
|
||||||
isBlob,
|
isBlob,
|
||||||
|
|
|
||||||
|
|
@ -480,3 +480,13 @@ assert.throws(() => new Blob({}), {
|
||||||
assert.ok(blob.slice(0, 1).constructor === Blob);
|
assert.ok(blob.slice(0, 1).constructor === Blob);
|
||||||
assert.ok(blob.slice(0, 1) instanceof Blob);
|
assert.ok(blob.slice(0, 1) instanceof Blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const blob = new Blob(['hello']);
|
||||||
|
|
||||||
|
assert.ok(structuredClone(blob).constructor === Blob);
|
||||||
|
assert.ok(structuredClone(blob) instanceof Blob);
|
||||||
|
assert.ok(structuredClone(blob).size === blob.size);
|
||||||
|
assert.ok(structuredClone(blob).size === blob.size);
|
||||||
|
assert.ok((await structuredClone(blob).text()) === (await blob.text()));
|
||||||
|
})().then(common.mustCall());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user