Skip to content

Commit 8860bd6

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 <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 46c9194 commit 8860bd6

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

lib/buffer.js

+26-14
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-
const constants = Object.defineProperties({}, {
104+
const constants = 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
const zeroFill = bindingZeroFill || [0];
113125

114-
const encodingsMap = Object.create(null);
126+
const encodingsMap = ObjectCreate(null);
115127
for (let i = 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
return defaultVal;
174186
}
@@ -253,7 +265,7 @@ function Buffer(arg, encodingOrOffset, length) {
253265
return Buffer.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() { return FastBuffer; }
@@ -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
return createUnsafeBuffer(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
function allocate(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 (!(this instanceof Buffer))
719731
return undefined;
720732
return this.buffer;
721733
}
722734
});
723-
Object.defineProperty(Buffer.prototype, 'offset', {
735+
ObjectDefineProperty(Buffer.prototype, 'offset', {
724736
enumerable: true,
725737
get() {
726738
if (!(this instanceof Buffer))
@@ -789,7 +801,7 @@ let INSPECT_MAX_BYTES = 50;
789801
// Override how buffers are presented by util.inspect().
790802
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
791803
const max = INSPECT_MAX_BYTES;
792-
const actualMax = Math.min(max, this.length);
804+
const actualMax = MathMin(max, this.length);
793805
const remaining = this.length - max;
794806
let str = 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
return obj;
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
function adjustOffset(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
return 0;
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)