Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: improve performance caused by primordials #30235

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 15 additions & 14 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

'use strict';

const { Math, Object } = primordials;
const { Object: { defineProperties, defineProperty, setPrototypeOf, create } } = primordials;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run make lint? It would usually complain about a line longer than 80 characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good afternoon 😃
Oh what a quick feedback!
Thank you♥️
I'll fix it when I'm back home.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing to ask for help, what's the correct way to do prettify? Normally I have prettier set up with eslint, but I didn't find similar in the Makefile.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make lint-js-fix :)

Copy link
Contributor Author

@jizusun jizusun Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I mean, for the max-len rule, the eslint --fix cannot help.
So I have to correct it by myself, right?

const { Math: { floor, trunc, min } } = primordials;

const {
byteLengthUtf8,
Expand Down Expand Up @@ -89,7 +90,7 @@ FastBuffer.prototype.constructor = Buffer;
Buffer.prototype = FastBuffer.prototype;
addBufferPrototypeMethods(Buffer.prototype);

const constants = Object.defineProperties({}, {
const constants = defineProperties({}, {
MAX_LENGTH: {
value: kMaxLength,
writable: false,
Expand All @@ -111,7 +112,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 = create(null);
for (let i = 0; i < encodings.length; ++i)
encodingsMap[encodings[i]] = i;

Expand Down Expand Up @@ -168,7 +169,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 : floor(n));
}
return defaultVal;
}
Expand Down Expand Up @@ -253,7 +254,7 @@ function Buffer(arg, encodingOrOffset, length) {
return Buffer.from(arg, encodingOrOffset, length);
}

Object.defineProperty(Buffer, Symbol.species, {
defineProperty(Buffer, Symbol.species, {
enumerable: false,
configurable: true,
get() { return FastBuffer; }
Expand Down Expand Up @@ -311,7 +312,7 @@ const of = (...items) => {
};
Buffer.of = of;

Object.setPrototypeOf(Buffer, Uint8Array);
setPrototypeOf(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
Expand Down Expand Up @@ -364,8 +365,8 @@ function SlowBuffer(length) {
return createUnsafeBuffer(length);
}

Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
Object.setPrototypeOf(SlowBuffer, Uint8Array);
setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
setPrototypeOf(SlowBuffer, Uint8Array);

function allocate(size) {
if (size <= 0) {
Expand Down Expand Up @@ -712,15 +713,15 @@ function byteLength(string, encoding) {
Buffer.byteLength = byteLength;

// For backwards compatibility.
Object.defineProperty(Buffer.prototype, 'parent', {
defineProperty(Buffer.prototype, 'parent', {
enumerable: true,
get() {
if (!(this instanceof Buffer))
return undefined;
return this.buffer;
}
});
Object.defineProperty(Buffer.prototype, 'offset', {
defineProperty(Buffer.prototype, 'offset', {
enumerable: true,
get() {
if (!(this instanceof Buffer))
Expand Down Expand Up @@ -789,7 +790,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 = min(max, this.length);
const remaining = this.length - max;
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
if (remaining > 0)
Expand All @@ -802,7 +803,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
extras = true;
obj[key] = this[key];
return obj;
}, Object.create(null));
}, create(null));
if (extras) {
if (this.length !== 0)
str += ', ';
Expand Down Expand Up @@ -1042,7 +1043,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 = trunc(offset);
if (offset === 0) {
return 0;
}
Expand Down Expand Up @@ -1163,7 +1164,7 @@ module.exports = {
kStringMaxLength
};

Object.defineProperties(module.exports, {
defineProperties(module.exports, {
constants: {
configurable: false,
enumerable: true,
Expand Down