diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-03-12 01:21:17 +0300 |
---|---|---|
committer | Luigi Pinca <luigipinca@gmail.com> | 2018-03-24 18:50:01 +0300 |
commit | f4e5f969baba15a566bb30a1ba11e64fe61470a5 (patch) | |
tree | 710d0793fff2cee7dc2081b6537a62a76673ea90 /lib/internal/buffer.js | |
parent | d59e2492f5142d554a8716ec03f68a0df041ead1 (diff) |
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>
Diffstat (limited to 'lib/internal/buffer.js')
-rw-r--r-- | lib/internal/buffer.js | 82 |
1 files changed, 52 insertions, 30 deletions
diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index fe10b20c805..2c65194bfa0 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -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) { |