Commit 41918c1d44e for nodejs
commit 41918c1d44e7156fb0525daf12693ca959f6c111
Author: Gürgün Dayıoğlu <hey@gurgun.day>
Date: Thu Nov 19 18:13:01 2026 +0100
buffer: use typed arrays for 64-bit BigInt accessors
readBig(U)Int64{LE,BE}() and writeBig(U)Int64{LE,BE}() assembled and
split values with BigInt arithmetic, allocating intermediate BigInts on
every call. Copy the bytes through BigInt64Array and BigUint64Array
views over the existing 8-byte scratch buffer instead, as the double
accessors already do.
Range and bounds checks are unchanged, and the value is masked with
`& -1n` before the store so non-BigInt values still throw the same
TypeError instead of being coerced by the typed array.
benchmark/buffers/buffer-read.js now uses the value it reads. The
loop discarded it, which let V8 remove side-effect-free reads
altogether.
Assisted-by: claude:opus-5.5
Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
PR-URL: https://github.com/nodejs/node/pull/67549
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/benchmark/buffers/buffer-read.js b/benchmark/buffers/buffer-read.js
index 19570a6b924..9d5a19d6a5a 100644
--- a/benchmark/buffers/buffer-read.js
+++ b/benchmark/buffers/buffer-read.js
@@ -1,5 +1,6 @@
'use strict';
const common = require('../common.js');
+const assert = require('assert');
const { Buffer } = require('buffer');
const types = [
@@ -32,10 +33,12 @@ function main({ n, buffer, type }) {
const fn = `read${type}`;
buff.writeDoubleLE(0, 0);
+ let result;
bench.start();
for (let i = 0; i !== n; i++) {
- buff[fn](0);
+ result = buff[fn](0);
}
bench.end(n);
+ assert.notStrictEqual(result, undefined);
}
diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js
index 3f99fe6d161..d8160a0f662 100644
--- a/lib/internal/buffer.js
+++ b/lib/internal/buffer.js
@@ -1,7 +1,8 @@
'use strict';
const {
- BigInt,
+ BigInt64Array,
+ BigUint64Array,
Float32Array,
Float64Array,
MathFloor,
@@ -50,6 +51,8 @@ const float32Array = new Float32Array(1);
const uInt8Float32Array = new Uint8Array(float32Array.buffer);
const float64Array = new Float64Array(1);
const uInt8Float64Array = new Uint8Array(float64Array.buffer);
+const bigInt64Array = new BigInt64Array(float64Array.buffer);
+const bigUint64Array = new BigUint64Array(float64Array.buffer);
// Check endianness.
float32Array[0] = -1; // 0xBF800000
@@ -97,80 +100,52 @@ function boundsError(value, length, type) {
}
// Read integers.
-function readBigUInt64LE(offset = 0) {
+function readBigInt64Bytes(buf, offset, backwards) {
validateNumber(offset, 'offset');
- const first = this[offset];
- const last = this[offset + 7];
+ const first = buf[offset];
+ const last = buf[offset + 7];
if (first === undefined || last === undefined)
- boundsError(offset, this.length - 8);
-
- const lo = first +
- this[++offset] * 2 ** 8 +
- this[++offset] * 2 ** 16 +
- this[++offset] * 2 ** 24;
-
- const hi = this[++offset] +
- this[++offset] * 2 ** 8 +
- this[++offset] * 2 ** 16 +
- last * 2 ** 24;
+ boundsError(offset, buf.length - 8);
+
+ if (backwards) {
+ uInt8Float64Array[7] = first;
+ uInt8Float64Array[6] = buf[++offset];
+ uInt8Float64Array[5] = buf[++offset];
+ uInt8Float64Array[4] = buf[++offset];
+ uInt8Float64Array[3] = buf[++offset];
+ uInt8Float64Array[2] = buf[++offset];
+ uInt8Float64Array[1] = buf[++offset];
+ uInt8Float64Array[0] = last;
+ } else {
+ uInt8Float64Array[0] = first;
+ uInt8Float64Array[1] = buf[++offset];
+ uInt8Float64Array[2] = buf[++offset];
+ uInt8Float64Array[3] = buf[++offset];
+ uInt8Float64Array[4] = buf[++offset];
+ uInt8Float64Array[5] = buf[++offset];
+ uInt8Float64Array[6] = buf[++offset];
+ uInt8Float64Array[7] = last;
+ }
+}
- return BigInt(lo) + (BigInt(hi) << 32n);
+function readBigUInt64LE(offset = 0) {
+ readBigInt64Bytes(this, offset, bigEndian);
+ return bigUint64Array[0];
}
function readBigUInt64BE(offset = 0) {
- validateNumber(offset, 'offset');
- const first = this[offset];
- const last = this[offset + 7];
- if (first === undefined || last === undefined)
- boundsError(offset, this.length - 8);
-
- const hi = first * 2 ** 24 +
- this[++offset] * 2 ** 16 +
- this[++offset] * 2 ** 8 +
- this[++offset];
-
- const lo = this[++offset] * 2 ** 24 +
- this[++offset] * 2 ** 16 +
- this[++offset] * 2 ** 8 +
- last;
-
- return (BigInt(hi) << 32n) + BigInt(lo);
+ readBigInt64Bytes(this, offset, !bigEndian);
+ return bigUint64Array[0];
}
function readBigInt64LE(offset = 0) {
- validateNumber(offset, 'offset');
- const first = this[offset];
- const last = this[offset + 7];
- if (first === undefined || last === undefined)
- boundsError(offset, this.length - 8);
-
- const val = this[offset + 4] +
- this[offset + 5] * 2 ** 8 +
- this[offset + 6] * 2 ** 16 +
- (last << 24); // Overflow
- return (BigInt(val) << 32n) +
- BigInt(first +
- this[++offset] * 2 ** 8 +
- this[++offset] * 2 ** 16 +
- this[++offset] * 2 ** 24);
+ readBigInt64Bytes(this, offset, bigEndian);
+ return bigInt64Array[0];
}
function readBigInt64BE(offset = 0) {
- validateNumber(offset, 'offset');
- const first = this[offset];
- const last = this[offset + 7];
- if (first === undefined || last === undefined)
- boundsError(offset, this.length - 8);
-
- const val = (first << 24) + // Overflow
- this[++offset] * 2 ** 16 +
- this[++offset] * 2 ** 8 +
- this[++offset];
- return (BigInt(val) << 32n) +
- BigInt(this[++offset] * 2 ** 24 +
- this[++offset] * 2 ** 16 +
- this[++offset] * 2 ** 8 +
- last);
+ readBigInt64Bytes(this, offset, !bigEndian);
+ return bigInt64Array[0];
}
function readUIntLE(offset, byteLength) {
@@ -583,66 +558,53 @@ function readDoubleForwards(offset = 0) {
}
// Write integers.
-function writeBigU_Int64LE(buf, value, offset, min, max) {
+function writeBigU_Int64(buf, value, offset, min, max, backwards) {
checkInt(value, min, max, buf, offset, 7);
- let lo = Number(value & 0xffffffffn);
- buf[offset++] = lo;
- lo = lo >> 8;
- buf[offset++] = lo;
- lo = lo >> 8;
- buf[offset++] = lo;
- lo = lo >> 8;
- buf[offset++] = lo;
- let hi = Number(value >> 32n & 0xffffffffn);
- buf[offset++] = hi;
- hi = hi >> 8;
- buf[offset++] = hi;
- hi = hi >> 8;
- buf[offset++] = hi;
- hi = hi >> 8;
- buf[offset++] = hi;
+ // `& -1n` keeps the TypeError for non-BigInt values.
+ bigInt64Array[0] = value & -1n;
+ if (backwards) {
+ buf[offset++] = uInt8Float64Array[7];
+ buf[offset++] = uInt8Float64Array[6];
+ buf[offset++] = uInt8Float64Array[5];
+ buf[offset++] = uInt8Float64Array[4];
+ buf[offset++] = uInt8Float64Array[3];
+ buf[offset++] = uInt8Float64Array[2];
+ buf[offset++] = uInt8Float64Array[1];
+ buf[offset++] = uInt8Float64Array[0];
+ } else {
+ buf[offset++] = uInt8Float64Array[0];
+ buf[offset++] = uInt8Float64Array[1];
+ buf[offset++] = uInt8Float64Array[2];
+ buf[offset++] = uInt8Float64Array[3];
+ buf[offset++] = uInt8Float64Array[4];
+ buf[offset++] = uInt8Float64Array[5];
+ buf[offset++] = uInt8Float64Array[6];
+ buf[offset++] = uInt8Float64Array[7];
+ }
return offset;
}
function writeBigUInt64LE(value, offset = 0) {
- return writeBigU_Int64LE(this, value, offset, 0n, 0xffffffffffffffffn);
-}
-
-function writeBigU_Int64BE(buf, value, offset, min, max) {
- checkInt(value, min, max, buf, offset, 7);
-
- let lo = Number(value & 0xffffffffn);
- buf[offset + 7] = lo;
- lo = lo >> 8;
- buf[offset + 6] = lo;
- lo = lo >> 8;
- buf[offset + 5] = lo;
- lo = lo >> 8;
- buf[offset + 4] = lo;
- let hi = Number(value >> 32n & 0xffffffffn);
- buf[offset + 3] = hi;
- hi = hi >> 8;
- buf[offset + 2] = hi;
- hi = hi >> 8;
- buf[offset + 1] = hi;
- hi = hi >> 8;
- buf[offset] = hi;
- return offset + 8;
+ return writeBigU_Int64(
+ this, value, offset, 0n, 0xffffffffffffffffn, bigEndian);
}
function writeBigUInt64BE(value, offset = 0) {
- return writeBigU_Int64BE(this, value, offset, 0n, 0xffffffffffffffffn);
+ return writeBigU_Int64(
+ this, value, offset, 0n, 0xffffffffffffffffn, !bigEndian);
}
function writeBigInt64LE(value, offset = 0) {
- return writeBigU_Int64LE(
- this, value, offset, -0x8000000000000000n, 0x7fffffffffffffffn);
+ return writeBigU_Int64(
+ this, value, offset, -0x8000000000000000n, 0x7fffffffffffffffn,
+ bigEndian);
}
function writeBigInt64BE(value, offset = 0) {
- return writeBigU_Int64BE(
- this, value, offset, -0x8000000000000000n, 0x7fffffffffffffffn);
+ return writeBigU_Int64(
+ this, value, offset, -0x8000000000000000n, 0x7fffffffffffffffn,
+ !bigEndian);
}
function writeUIntLE(value, offset, byteLength) {