Skip to content

Commit

Permalink
buffer: improve write(U)Int functions
Browse files Browse the repository at this point in the history
This improves the performance of some write functions by around 5-15%.

PR-URL: #19289
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and lpinca committed Mar 24, 2018
1 parent d59e249 commit f4e5f96
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
5 changes: 3 additions & 2 deletions benchmark/buffers/buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const INT32 = 0x7fffffff;
const INT48 = 0x7fffffffffff;
const UINT8 = 0xff;
const UINT16 = 0xffff;
const UINT32 = 0xffffffff;

const mod = {
writeInt8: INT8,
Expand All @@ -44,8 +45,8 @@ const mod = {
writeUInt8: UINT8,
writeUInt16BE: UINT16,
writeUInt16LE: UINT16,
writeUInt32BE: INT32,
writeUInt32LE: INT32,
writeUInt32BE: UINT32,
writeUInt32LE: UINT32,
writeUIntLE: INT8,
writeUIntBE: INT16,
writeIntLE: INT32,
Expand Down
82 changes: 52 additions & 30 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,12 @@ function writeU_Int48LE(buf, value, offset, min, max) {

const newVal = Math.floor(value * 2 ** -32);
buf[offset++] = value;
buf[offset++] = (value >>> 8);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 24);
value = value >>> 8;
buf[offset++] = value;
value = value >>> 8;
buf[offset++] = value;
value = value >>> 8;
buf[offset++] = value;
buf[offset++] = newVal;
buf[offset++] = (newVal >>> 8);
return offset;
Expand All @@ -495,9 +498,12 @@ function writeU_Int40LE(buf, value, offset, min, max) {

const newVal = value;
buf[offset++] = value;
buf[offset++] = (value >>> 8);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 24);
value = value >>> 8;
buf[offset++] = value;
value = value >>> 8;
buf[offset++] = value;
value = value >>> 8;
buf[offset++] = value;
buf[offset++] = Math.floor(newVal * 2 ** -32);
return offset;
}
Expand All @@ -507,9 +513,12 @@ function writeU_Int32LE(buf, value, offset, min, max) {
checkInt(value, min, max, buf, offset, 3);

buf[offset++] = value;
buf[offset++] = (value >>> 8);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 24);
value = value >>> 8;
buf[offset++] = value;
value = value >>> 8;
buf[offset++] = value;
value = value >>> 8;
buf[offset++] = value;
return offset;
}

Expand All @@ -522,8 +531,10 @@ function writeU_Int24LE(buf, value, offset, min, max) {
checkInt(value, min, max, buf, offset, 2);

buf[offset++] = value;
buf[offset++] = (value >>> 8);
buf[offset++] = (value >>> 16);
value = value >>> 8;
buf[offset++] = value;
value = value >>> 8;
buf[offset++] = value;
return offset;
}

Expand Down Expand Up @@ -582,34 +593,43 @@ function writeU_Int48BE(buf, value, offset, min, max) {
const newVal = Math.floor(value * 2 ** -32);
buf[offset++] = (newVal >>> 8);
buf[offset++] = newVal;
buf[offset++] = (value >>> 24);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
buf[offset + 3] = value;
value = value >>> 8;
buf[offset + 2] = value;
value = value >>> 8;
buf[offset + 1] = value;
value = value >>> 8;
buf[offset] = value;
return offset + 4;
}

function writeU_Int40BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 4);

buf[offset++] = Math.floor(value * 2 ** -32);
buf[offset++] = (value >>> 24);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
buf[offset + 3] = value;
value = value >>> 8;
buf[offset + 2] = value;
value = value >>> 8;
buf[offset + 1] = value;
value = value >>> 8;
buf[offset] = value;
return offset + 4;
}

function writeU_Int32BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 3);

buf[offset++] = (value >>> 24);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
buf[offset + 3] = value;
value = value >>> 8;
buf[offset + 2] = value;
value = value >>> 8;
buf[offset + 1] = value;
value = value >>> 8;
buf[offset] = value;
return offset + 4;
}

function writeUInt32BE(value, offset) {
Expand All @@ -620,10 +640,12 @@ function writeU_Int24BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 2);

buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
buf[offset + 2] = value;
value = value >>> 8;
buf[offset + 1] = value;
value = value >>> 8;
buf[offset] = value;
return offset + 3;
}

function writeU_Int16BE(buf, value, offset, min, max) {
Expand Down

0 comments on commit f4e5f96

Please sign in to comment.