GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: use typed arrays for 64-bit BigInt accessors by gurgunday · Pull Request #67549 · nodejs/node

buffer: use typed arrays for 64-bit BigInt accessors #67549

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.

Sign up for GitHub

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

Merged
nodejs-github-bot merged 1 commit into nodejs:main from gurgunday:perf/buffer-bigint64
Nov 19, 2026
+74 −109
Merged

buffer: use typed arrays for 64-bit BigInt accessors #67549

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
6d52376
buffer: use typed arrays for 64-bit BigInt accessors
gurgunday Nov 15, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
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 <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@gurgunday
gurgunday committed Nov 17, 2026
commit 6d5237645ce2f00d753998722f0a8ae50e425e24

There are no files selected for viewing

5 changes: 4 additions & 1 deletion 5 benchmark/buffers/buffer-read.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const common = require('../common.js');
const assert = require('assert');
const { Buffer } = require('buffer');

const types = [
Expand Down Expand Up @@ -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);
}
178 changes: 70 additions & 108 deletions 178 lib/internal/buffer.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const {
BigInt,
BigInt64Array,
BigUint64Array,
Float32Array,
Float64Array,
MathFloor,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.