Skip to content

Commit b6021ab

Browse files
H4adtargos
authored andcommitted
lib: reduce overhead of blob clone
PR-URL: #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>
1 parent bac872c commit b6021ab

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎benchmark/blob/clone.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ const assert = require('assert');
77

88
constbench=common.createBenchmark(main,{
99
n: [50e3],
10+
bytes: [128,1024,1024**2],
1011
});
1112

1213
let_cloneResult;
1314

14-
functionmain({ n }){
15+
functionmain({ n, bytes }){
16+
constbuff=Buffer.allocUnsafe(bytes);
17+
constblob=newBlob(buff);
1518
bench.start();
1619
for(leti=0;i<n;++i)
17-
_cloneResult=structuredClone(newBlob(['hello']));
20+
_cloneResult=structuredClone(blob);
1821
bench.end(n);
1922

2023
// Avoid V8 deadcode (elimination)

‎lib/internal/blob.js‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
ObjectDefineProperty,
99
ObjectSetPrototypeOf,
1010
PromiseReject,
11-
ReflectConstruct,
1211
RegExpPrototypeExec,
1312
RegExpPrototypeSymbolReplace,
1413
StringPrototypeToLowerCase,
@@ -200,7 +199,7 @@ class Blob {
200199
constlength=this[kLength];
201200
return{
202201
data: { handle, type, length },
203-
deserializeInfo: 'internal/blob:ClonedBlob',
202+
deserializeInfo: 'internal/blob:Blob',
204203
};
205204
}
206205

@@ -397,25 +396,18 @@ class Blob {
397396
}
398397
}
399398

400-
functionClonedBlob(){
401-
returnReflectConstruct(function(){
402-
markTransferMode(this,true,false);
403-
},[],Blob);
404-
}
405-
ClonedBlob.prototype[kDeserialize]=()=>{};
406-
407-
functionTransferrableBlob(handle,length,type=''){
399+
functionTransferableBlob(handle,length,type=''){
408400
markTransferMode(this,true,false);
409401
this[kHandle]=handle;
410402
this[kType]=type;
411403
this[kLength]=length;
412404
}
413405

414-
ObjectSetPrototypeOf(TransferrableBlob.prototype,Blob.prototype);
415-
ObjectSetPrototypeOf(TransferrableBlob,Blob);
406+
ObjectSetPrototypeOf(TransferableBlob.prototype,Blob.prototype);
407+
ObjectSetPrototypeOf(TransferableBlob,Blob);
416408

417409
functioncreateBlob(handle,length,type=''){
418-
consttransferredBlob=newTransferrableBlob(handle,length,type);
410+
consttransferredBlob=newTransferableBlob(handle,length,type);
419411

420412
// Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053
421413
transferredBlob.constructor=Blob;
@@ -489,7 +481,6 @@ function createBlobFromFilePath(path, options) {
489481

490482
module.exports={
491483
Blob,
492-
ClonedBlob,
493484
createBlob,
494485
createBlobFromFilePath,
495486
isBlob,

‎test/parallel/test-blob.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,13 @@ assert.throws(() => new Blob({}), {
480480
assert.ok(blob.slice(0,1).constructor===Blob);
481481
assert.ok(blob.slice(0,1)instanceofBlob);
482482
}
483+
484+
(async()=>{
485+
constblob=newBlob(['hello']);
486+
487+
assert.ok(structuredClone(blob).constructor===Blob);
488+
assert.ok(structuredClone(blob)instanceofBlob);
489+
assert.ok(structuredClone(blob).size===blob.size);
490+
assert.ok(structuredClone(blob).size===blob.size);
491+
assert.ok((awaitstructuredClone(blob).text())===(awaitblob.text()));
492+
})().then(common.mustCall());

0 commit comments

Comments
 (0)