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: remove noAssert and improve performance #18395

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions benchmark/buffers/buffer-read-float.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
const common = require('../common.js');

const bench = common.createBenchmark(main, {
noAssert: ['false', 'true'],
type: ['Double', 'Float'],
endian: ['BE', 'LE'],
value: ['zero', 'big', 'small', 'inf', 'nan'],
millions: [1]
});

function main({ noAssert, millions, type, endian, value }) {
noAssert = noAssert === 'true';
function main({ millions, type, endian, value }) {
type = type || 'Double';
const buff = Buffer.alloc(8);
const fn = `read${type}${endian}`;
Expand All @@ -31,11 +29,11 @@ function main({ noAssert, millions, type, endian, value }) {
},
};

buff[`write${type}${endian}`](values[type][value], 0, noAssert);
buff[`write${type}${endian}`](values[type][value], 0);

bench.start();
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](0, noAssert);
buff[fn](0);
}
bench.end(millions);
}
13 changes: 5 additions & 8 deletions benchmark/buffers/buffer-read-with-byteLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@ const types = [
];

const bench = common.createBenchmark(main, {
noAssert: ['false', 'true'],
buffer: ['fast', 'slow'],
type: types,
millions: [1],
byteLength: [1, 2, 4, 6]
byteLength: [1, 2, 3, 4, 5, 6]
});

function main({ millions, noAssert, buf, type, byteLength }) {
noAssert = noAssert === 'true';
type = type || 'UInt8';
function main({ millions, buf, type, byteLength }) {
const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
const buff = new clazz(8);
const fn = `read${type}`;
const fn = `read${type || 'IntBE'}`;

buff.writeDoubleLE(0, 0, noAssert);
buff.writeDoubleLE(0, 0);
bench.start();
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](0, byteLength, noAssert);
buff[fn](0, byteLength);
}
bench.end(millions);
}
8 changes: 3 additions & 5 deletions benchmark/buffers/buffer-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,20 @@ const types = [
];

const bench = common.createBenchmark(main, {
noAssert: ['false', 'true'],
buffer: ['fast', 'slow'],
type: types,
millions: [1]
});

function main({ noAssert, millions, buf, type }) {
noAssert = noAssert === 'true';
function main({ millions, buf, type }) {
const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
const buff = new clazz(8);
const fn = `read${type || 'UInt8'}`;

buff.writeDoubleLE(0, 0, noAssert);
buff.writeDoubleLE(0, 0);
bench.start();
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](0, noAssert);
buff[fn](0);
}
bench.end(millions);
}
54 changes: 40 additions & 14 deletions benchmark/buffers/buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ const types = [
'UInt16BE',
'UInt32LE',
'UInt32BE',
'UIntLE',
'UIntBE',
'Int8',
'Int16LE',
'Int16BE',
'Int32LE',
'Int32BE',
'IntLE',
'IntBE',
'FloatLE',
'FloatBE',
'DoubleLE',
'DoubleBE'
];

const bench = common.createBenchmark(main, {
noAssert: ['false', 'true'],
buffer: ['fast', 'slow'],
type: types,
millions: [1]
Expand All @@ -28,9 +31,9 @@ const bench = common.createBenchmark(main, {
const INT8 = 0x7f;
const INT16 = 0x7fff;
const INT32 = 0x7fffffff;
const UINT8 = (INT8 * 2) + 1;
const UINT16 = (INT16 * 2) + 1;
const UINT32 = INT32;
const INT48 = 0x7fffffffffff;
const UINT8 = 0xff;
const UINT16 = 0xffff;

const mod = {
writeInt8: INT8,
Expand All @@ -41,34 +44,57 @@ const mod = {
writeUInt8: UINT8,
writeUInt16BE: UINT16,
writeUInt16LE: UINT16,
writeUInt32BE: UINT32,
writeUInt32LE: UINT32
writeUInt32BE: INT32,
writeUInt32LE: INT32,
writeUIntLE: INT8,
writeUIntBE: INT16,
writeIntLE: INT32,
writeIntBE: INT48
};

function main({ noAssert, millions, buf, type }) {
const byteLength = {
writeUIntLE: 1,
writeUIntBE: 2,
writeIntLE: 4,
writeIntBE: 6
};

function main({ millions, buf, type }) {
const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
const buff = new clazz(8);
const fn = `write${type || 'UInt8'}`;

if (/Int/.test(fn))
benchInt(buff, fn, millions, noAssert);
if (!/\d/.test(fn))
benchSpecialInt(buff, fn, millions);
else if (/Int/.test(fn))
benchInt(buff, fn, millions);
else
benchFloat(buff, fn, millions, noAssert);
benchFloat(buff, fn, millions);
}

function benchInt(buff, fn, millions) {
const m = mod[fn];
bench.start();
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](i & m, 0);
}
bench.end(millions);
}

function benchInt(buff, fn, millions, noAssert) {
function benchSpecialInt(buff, fn, millions) {
const m = mod[fn];
const byte = byteLength[fn];
bench.start();
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](i & m, 0, noAssert);
buff[fn](i & m, 0, byte);
}
bench.end(millions);
}

function benchFloat(buff, fn, millions, noAssert) {
function benchFloat(buff, fn, millions) {
bench.start();
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](i, 0, noAssert);
buff[fn](i, 0);
}
bench.end(millions);
}
Loading