summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJizu Sun <jizu.sun@sap.com>2019-11-03 19:45:32 +0800
committerZYSzys <zyszys98@gmail.com>2019-11-04 21:25:40 +0800
commit22799be7a9a87ec32b438b22090622f9884baeb4 (patch)
tree876b4e1e74f2f8987f6aea774d5d6061fd1e0132 /lib
parentc80771d11bc7ca7001bf0fd822b239432e9a0d5b (diff)
downloadandroid-node-v8-22799be7a9a87ec32b438b22090622f9884baeb4.tar.gz
android-node-v8-22799be7a9a87ec32b438b22090622f9884baeb4.tar.bz2
android-node-v8-22799be7a9a87ec32b438b22090622f9884baeb4.zip
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: https://github.com/nodejs/node/issues/29766 Refs: https://github.com/nodejs/code-and-learn/issues/97 Refs: https://github.com/nodejs/node/pull/29633 PR-URL: https://github.com/nodejs/node/pull/30235 Refs: https://github.com/nodejs/node/issues/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>
Diffstat (limited to 'lib')
-rw-r--r--lib/buffer.js40
1 files changed, 26 insertions, 14 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index ce2d7c63b5..ab30db6701 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -21,7 +21,19 @@
'use strict';
-const { Math, Object } = primordials;
+const {
+ Object: {
+ defineProperties: ObjectDefineProperties,
+ defineProperty: ObjectDefineProperty,
+ setPrototypeOf: ObjectSetPrototypeOf,
+ create: ObjectCreate
+ },
+ Math: {
+ floor: MathFloor,
+ trunc: MathTrunc,
+ min: MathMin
+ }
+} = primordials;
const {
byteLengthUtf8,
@@ -89,7 +101,7 @@ FastBuffer.prototype.constructor = Buffer;
Buffer.prototype = FastBuffer.prototype;
addBufferPrototypeMethods(Buffer.prototype);
-const constants = Object.defineProperties({}, {
+const constants = ObjectDefineProperties({}, {
MAX_LENGTH: {
value: kMaxLength,
writable: false,
@@ -111,7 +123,7 @@ let poolSize, poolOffset, allocPool;
// do not own the ArrayBuffer allocator. Zero fill is always on in that case.
const zeroFill = bindingZeroFill || [0];
-const encodingsMap = Object.create(null);
+const encodingsMap = ObjectCreate(null);
for (let i = 0; i < encodings.length; ++i)
encodingsMap[encodings[i]] = i;
@@ -168,7 +180,7 @@ function toInteger(n, defaultVal) {
if (!Number.isNaN(n) &&
n >= Number.MIN_SAFE_INTEGER &&
n <= Number.MAX_SAFE_INTEGER) {
- return ((n % 1) === 0 ? n : Math.floor(n));
+ return ((n % 1) === 0 ? n : MathFloor(n));
}
return defaultVal;
}
@@ -253,7 +265,7 @@ function Buffer(arg, encodingOrOffset, length) {
return Buffer.from(arg, encodingOrOffset, length);
}
-Object.defineProperty(Buffer, Symbol.species, {
+ObjectDefineProperty(Buffer, Symbol.species, {
enumerable: false,
configurable: true,
get() { return FastBuffer; }
@@ -311,7 +323,7 @@ const of = (...items) => {
};
Buffer.of = of;
-Object.setPrototypeOf(Buffer, Uint8Array);
+ObjectSetPrototypeOf(Buffer, Uint8Array);
// The 'assertSize' method will remove itself from the callstack when an error
// occurs. This is done simply to keep the internal details of the
@@ -364,8 +376,8 @@ function SlowBuffer(length) {
return createUnsafeBuffer(length);
}
-Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
-Object.setPrototypeOf(SlowBuffer, Uint8Array);
+ObjectSetPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
+ObjectSetPrototypeOf(SlowBuffer, Uint8Array);
function allocate(size) {
if (size <= 0) {
@@ -712,7 +724,7 @@ function byteLength(string, encoding) {
Buffer.byteLength = byteLength;
// For backwards compatibility.
-Object.defineProperty(Buffer.prototype, 'parent', {
+ObjectDefineProperty(Buffer.prototype, 'parent', {
enumerable: true,
get() {
if (!(this instanceof Buffer))
@@ -720,7 +732,7 @@ Object.defineProperty(Buffer.prototype, 'parent', {
return this.buffer;
}
});
-Object.defineProperty(Buffer.prototype, 'offset', {
+ObjectDefineProperty(Buffer.prototype, 'offset', {
enumerable: true,
get() {
if (!(this instanceof Buffer))
@@ -789,7 +801,7 @@ let INSPECT_MAX_BYTES = 50;
// Override how buffers are presented by util.inspect().
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
const max = INSPECT_MAX_BYTES;
- const actualMax = Math.min(max, this.length);
+ const actualMax = MathMin(max, this.length);
const remaining = this.length - max;
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
if (remaining > 0)
@@ -802,7 +814,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
extras = true;
obj[key] = this[key];
return obj;
- }, Object.create(null));
+ }, ObjectCreate(null));
if (extras) {
if (this.length !== 0)
str += ', ';
@@ -1042,7 +1054,7 @@ Buffer.prototype.toJSON = function toJSON() {
function adjustOffset(offset, length) {
// Use Math.trunc() to convert offset to an integer value that can be larger
// than an Int32. Hence, don't use offset | 0 or similar techniques.
- offset = Math.trunc(offset);
+ offset = MathTrunc(offset);
if (offset === 0) {
return 0;
}
@@ -1163,7 +1175,7 @@ module.exports = {
kStringMaxLength
};
-Object.defineProperties(module.exports, {
+ObjectDefineProperties(module.exports, {
constants: {
configurable: false,
enumerable: true,