Skip to content

Commit 680e9cc

Browse files
jizusuntargos
authored andcommitted
buffer: improve performance caused by primordials
This is my first PR, and it's based on the code-and-learn guidances This restore some performance after introducing primordialias. Refs: #29766 Refs: nodejs/code-and-learn#97 Refs: #29633 PR-URL: #30235 Refs: #29766 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent b1529c6 commit 680e9cc

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

‎lib/buffer.js‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@
2121

2222
'use strict';
2323

24-
const{ Math, Object }=primordials;
24+
const{
25+
Object: {
26+
defineProperties: ObjectDefineProperties,
27+
defineProperty: ObjectDefineProperty,
28+
setPrototypeOf: ObjectSetPrototypeOf,
29+
create: ObjectCreate
30+
},
31+
Math: {
32+
floor: MathFloor,
33+
trunc: MathTrunc,
34+
min: MathMin
35+
}
36+
}=primordials;
2537

2638
const{
2739
byteLengthUtf8,
@@ -89,7 +101,7 @@ FastBuffer.prototype.constructor = Buffer;
89101
Buffer.prototype=FastBuffer.prototype;
90102
addBufferPrototypeMethods(Buffer.prototype);
91103

92-
constconstants=Object.defineProperties({},{
104+
constconstants=ObjectDefineProperties({},{
93105
MAX_LENGTH: {
94106
value: kMaxLength,
95107
writable: false,
@@ -111,7 +123,7 @@ let poolSize, poolOffset, allocPool;
111123
// do not own the ArrayBuffer allocator. Zero fill is always on in that case.
112124
constzeroFill=bindingZeroFill||[0];
113125

114-
constencodingsMap=Object.create(null);
126+
constencodingsMap=ObjectCreate(null);
115127
for(leti=0;i<encodings.length;++i)
116128
encodingsMap[encodings[i]]=i;
117129

@@ -168,7 +180,7 @@ function toInteger(n, defaultVal) {
168180
if(!Number.isNaN(n)&&
169181
n>=Number.MIN_SAFE_INTEGER&&
170182
n<=Number.MAX_SAFE_INTEGER){
171-
return((n%1)===0 ? n : Math.floor(n));
183+
return((n%1)===0 ? n : MathFloor(n));
172184
}
173185
returndefaultVal;
174186
}
@@ -253,7 +265,7 @@ function Buffer(arg, encodingOrOffset, length) {
253265
returnBuffer.from(arg,encodingOrOffset,length);
254266
}
255267

256-
Object.defineProperty(Buffer,Symbol.species,{
268+
ObjectDefineProperty(Buffer,Symbol.species,{
257269
enumerable: false,
258270
configurable: true,
259271
get(){returnFastBuffer;}
@@ -311,7 +323,7 @@ const of = (...items) => {
311323
};
312324
Buffer.of=of;
313325

314-
Object.setPrototypeOf(Buffer,Uint8Array);
326+
ObjectSetPrototypeOf(Buffer,Uint8Array);
315327

316328
// The 'assertSize' method will remove itself from the callstack when an error
317329
// occurs. This is done simply to keep the internal details of the
@@ -364,8 +376,8 @@ function SlowBuffer(length) {
364376
returncreateUnsafeBuffer(length);
365377
}
366378

367-
Object.setPrototypeOf(SlowBuffer.prototype,Uint8Array.prototype);
368-
Object.setPrototypeOf(SlowBuffer,Uint8Array);
379+
ObjectSetPrototypeOf(SlowBuffer.prototype,Uint8Array.prototype);
380+
ObjectSetPrototypeOf(SlowBuffer,Uint8Array);
369381

370382
functionallocate(size){
371383
if(size<=0){
@@ -712,15 +724,15 @@ function byteLength(string, encoding) {
712724
Buffer.byteLength=byteLength;
713725

714726
// For backwards compatibility.
715-
Object.defineProperty(Buffer.prototype,'parent',{
727+
ObjectDefineProperty(Buffer.prototype,'parent',{
716728
enumerable: true,
717729
get(){
718730
if(!(thisinstanceofBuffer))
719731
returnundefined;
720732
returnthis.buffer;
721733
}
722734
});
723-
Object.defineProperty(Buffer.prototype,'offset',{
735+
ObjectDefineProperty(Buffer.prototype,'offset',{
724736
enumerable: true,
725737
get(){
726738
if(!(thisinstanceofBuffer))
@@ -789,7 +801,7 @@ let INSPECT_MAX_BYTES = 50;
789801
// Override how buffers are presented by util.inspect().
790802
Buffer.prototype[customInspectSymbol]=functioninspect(recurseTimes,ctx){
791803
constmax=INSPECT_MAX_BYTES;
792-
constactualMax=Math.min(max,this.length);
804+
constactualMax=MathMin(max,this.length);
793805
constremaining=this.length-max;
794806
letstr=this.hexSlice(0,actualMax).replace(/(.{2})/g,'$1 ').trim();
795807
if(remaining>0)
@@ -802,7 +814,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
802814
extras=true;
803815
obj[key]=this[key];
804816
returnobj;
805-
},Object.create(null));
817+
},ObjectCreate(null));
806818
if(extras){
807819
if(this.length!==0)
808820
str+=', ';
@@ -1042,7 +1054,7 @@ Buffer.prototype.toJSON = function toJSON() {
10421054
functionadjustOffset(offset,length){
10431055
// Use Math.trunc() to convert offset to an integer value that can be larger
10441056
// than an Int32. Hence, don't use offset | 0 or similar techniques.
1045-
offset=Math.trunc(offset);
1057+
offset=MathTrunc(offset);
10461058
if(offset===0){
10471059
return0;
10481060
}
@@ -1163,7 +1175,7 @@ module.exports = {
11631175
kStringMaxLength
11641176
};
11651177

1166-
Object.defineProperties(module.exports,{
1178+
ObjectDefineProperties(module.exports,{
11671179
constants: {
11681180
configurable: false,
11691181
enumerable: true,

0 commit comments

Comments
 (0)