Skip to content

Commit 3a7c798

Browse files
committed
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. Fixes: nodejs#29766 Refs: nodejs/code-and-learn#97 Refs: nodejs#29633
1 parent 3a076ba commit 3a7c798

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/buffer.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
'use strict';
2323

24-
const { Math, Object } = primordials;
24+
const { Object: { defineProperties, defineProperty, setPrototypeOf, create } } = primordials;
25+
const { Math: { floor, trunc, min } } = primordials;
2526

2627
const {
2728
byteLengthUtf8,
@@ -89,7 +90,7 @@ FastBuffer.prototype.constructor = Buffer;
8990
Buffer.prototype = FastBuffer.prototype;
9091
addBufferPrototypeMethods(Buffer.prototype);
9192

92-
const constants = Object.defineProperties({}, {
93+
const constants = defineProperties({}, {
9394
MAX_LENGTH: {
9495
value: kMaxLength,
9596
writable: false,
@@ -111,7 +112,7 @@ let poolSize, poolOffset, allocPool;
111112
// do not own the ArrayBuffer allocator. Zero fill is always on in that case.
112113
const zeroFill = bindingZeroFill || [0];
113114

114-
const encodingsMap = Object.create(null);
115+
const encodingsMap = create(null);
115116
for (let i = 0; i < encodings.length; ++i)
116117
encodingsMap[encodings[i]] = i;
117118

@@ -168,7 +169,7 @@ function toInteger(n, defaultVal) {
168169
if (!Number.isNaN(n) &&
169170
n >= Number.MIN_SAFE_INTEGER &&
170171
n <= Number.MAX_SAFE_INTEGER) {
171-
return ((n % 1) === 0 ? n : Math.floor(n));
172+
return ((n % 1) === 0 ? n : floor(n));
172173
}
173174
return defaultVal;
174175
}
@@ -253,7 +254,7 @@ function Buffer(arg, encodingOrOffset, length) {
253254
return Buffer.from(arg, encodingOrOffset, length);
254255
}
255256

256-
Object.defineProperty(Buffer, Symbol.species, {
257+
defineProperty(Buffer, Symbol.species, {
257258
enumerable: false,
258259
configurable: true,
259260
get() { return FastBuffer; }
@@ -311,7 +312,7 @@ const of = (...items) => {
311312
};
312313
Buffer.of = of;
313314

314-
Object.setPrototypeOf(Buffer, Uint8Array);
315+
setPrototypeOf(Buffer, Uint8Array);
315316

316317
// The 'assertSize' method will remove itself from the callstack when an error
317318
// occurs. This is done simply to keep the internal details of the
@@ -364,8 +365,8 @@ function SlowBuffer(length) {
364365
return createUnsafeBuffer(length);
365366
}
366367

367-
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
368-
Object.setPrototypeOf(SlowBuffer, Uint8Array);
368+
setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
369+
setPrototypeOf(SlowBuffer, Uint8Array);
369370

370371
function allocate(size) {
371372
if (size <= 0) {
@@ -712,15 +713,15 @@ function byteLength(string, encoding) {
712713
Buffer.byteLength = byteLength;
713714

714715
// For backwards compatibility.
715-
Object.defineProperty(Buffer.prototype, 'parent', {
716+
defineProperty(Buffer.prototype, 'parent', {
716717
enumerable: true,
717718
get() {
718719
if (!(this instanceof Buffer))
719720
return undefined;
720721
return this.buffer;
721722
}
722723
});
723-
Object.defineProperty(Buffer.prototype, 'offset', {
724+
defineProperty(Buffer.prototype, 'offset', {
724725
enumerable: true,
725726
get() {
726727
if (!(this instanceof Buffer))
@@ -789,7 +790,7 @@ let INSPECT_MAX_BYTES = 50;
789790
// Override how buffers are presented by util.inspect().
790791
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
791792
const max = INSPECT_MAX_BYTES;
792-
const actualMax = Math.min(max, this.length);
793+
const actualMax = min(max, this.length);
793794
const remaining = this.length - max;
794795
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
795796
if (remaining > 0)
@@ -802,7 +803,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
802803
extras = true;
803804
obj[key] = this[key];
804805
return obj;
805-
}, Object.create(null));
806+
}, create(null));
806807
if (extras) {
807808
if (this.length !== 0)
808809
str += ', ';
@@ -1042,7 +1043,7 @@ Buffer.prototype.toJSON = function toJSON() {
10421043
function adjustOffset(offset, length) {
10431044
// Use Math.trunc() to convert offset to an integer value that can be larger
10441045
// than an Int32. Hence, don't use offset | 0 or similar techniques.
1045-
offset = Math.trunc(offset);
1046+
offset = trunc(offset);
10461047
if (offset === 0) {
10471048
return 0;
10481049
}
@@ -1163,7 +1164,7 @@ module.exports = {
11631164
kStringMaxLength
11641165
};
11651166

1166-
Object.defineProperties(module.exports, {
1167+
defineProperties(module.exports, {
11671168
constants: {
11681169
configurable: false,
11691170
enumerable: true,

0 commit comments

Comments
 (0)