buffer: improve allocation performance
assertSize() is adjusted to be inlineable according to V8's default function size limits when determining inlineability. This results in up to 11% performance gains when allocating any kind of Buffer. Avoid avoids use of in, resulting in ~50% improvement when creating a Buffer from an array-like object. PR-URL: https://github.com/nodejs/node/pull/10443 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
595b22affa
commit
13a4887ee9
@ -10,7 +10,8 @@ const bench = common.createBenchmark(main, {
|
||||
'buffer',
|
||||
'uint8array',
|
||||
'string',
|
||||
'string-base64'
|
||||
'string-base64',
|
||||
'object'
|
||||
],
|
||||
len: [10, 2048],
|
||||
n: [1024]
|
||||
@ -25,6 +26,7 @@ function main(conf) {
|
||||
const str = 'a'.repeat(len);
|
||||
const buffer = Buffer.allocUnsafe(len);
|
||||
const uint8array = new Uint8Array(len);
|
||||
const obj = { length: null }; // Results in a new, empty Buffer
|
||||
|
||||
var i;
|
||||
|
||||
@ -80,6 +82,13 @@ function main(conf) {
|
||||
}
|
||||
bench.end(n);
|
||||
break;
|
||||
case 'object':
|
||||
bench.start();
|
||||
for (i = 0; i < n * 1024; i++) {
|
||||
Buffer.from(obj);
|
||||
}
|
||||
bench.end(n);
|
||||
break;
|
||||
default:
|
||||
assert.fail(null, null, 'Should not get here');
|
||||
}
|
||||
|
@ -109,6 +109,9 @@ Buffer.from = function(value, encodingOrOffset, length) {
|
||||
|
||||
Object.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
|
||||
// implementation from bleeding out to users.
|
||||
function assertSize(size) {
|
||||
let err = null;
|
||||
|
||||
@ -121,9 +124,6 @@ function assertSize(size) {
|
||||
'than ' + binding.kMaxLength);
|
||||
|
||||
if (err) {
|
||||
// The following hides the 'assertSize' method from the
|
||||
// callstack. This is done simply to hide the internal
|
||||
// details of the implementation from bleeding out to users.
|
||||
Error.captureStackTrace(err, assertSize);
|
||||
throw err;
|
||||
}
|
||||
@ -263,7 +263,7 @@ function fromObject(obj) {
|
||||
}
|
||||
|
||||
if (obj) {
|
||||
if ('length' in obj || isArrayBuffer(obj.buffer) ||
|
||||
if (obj.length !== undefined || isArrayBuffer(obj.buffer) ||
|
||||
isSharedArrayBuffer(obj.buffer)) {
|
||||
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
|
||||
return new FastBuffer();
|
||||
|
Loading…
x
Reference in New Issue
Block a user