mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
buffer: improve write(U)Int functions
This improves the performance of some write functions by around 5-15%. PR-URL: https://github.com/nodejs/node/pull/19289 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
d59e2492f5
commit
f4e5f969ba
@ -34,6 +34,7 @@ const INT32 = 0x7fffffff;
|
||||
const INT48 = 0x7fffffffffff;
|
||||
const UINT8 = 0xff;
|
||||
const UINT16 = 0xffff;
|
||||
const UINT32 = 0xffffffff;
|
||||
|
||||
const mod = {
|
||||
writeInt8: INT8,
|
||||
@ -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,
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -582,11 +593,14 @@ 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) {
|
||||
@ -594,22 +608,28 @@ function writeU_Int40BE(buf, value, offset, min, max) {
|
||||
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) {
|
||||
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user