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 — Read TypeScript's 64-bit integers through a scratch DataView (#285) · snovader/tafla@892a69c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • typescript/core
    • CHANGELOG.md
    • source
      • utils.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎typescript/core/CHANGELOG.md‎

Copy file name to clipboardExpand all lines: typescript/core/CHANGELOG.md
+9Lines changed: 9 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ repository's first commits, for code built from its source before the release.
110110
0.087 µs rather than 0.108 in Node 27. `writeFloat64()`, `writeInt64()`
111111
and `writeUint64()` took 3% to 4% less, and `TableBuilder`'s setters 4% to
112112
9% less.
113+
- `readInt64()` and `readUint64()` copy a value's bytes to a scratch
114+
`DataView` and read it from there, as `readFloat64()` does, rather than
115+
adding its two 32-bit words as bigints, which allocated a bigint for each.
116+
They return the same values and throw the same `DecodeError`. With the
117+
package bundled in Node 26, 100 reads at unaligned offsets took 4.8 ns
118+
each rather than 26.2 for `readInt64()`, and 9.7 ns rather than 26.3 for
119+
`readUint64()`. `readFloat64()` took 2.75 ns, as before. The code that
120+
tafla-gen generates for a struct of every scalar type, which has one field
121+
of each, read its fields in 0.050 µs rather than 0.089.
113122

114123
### Removed
115124

Collapse file

‎typescript/core/source/utils.ts‎

Copy file name to clipboardExpand all lines: typescript/core/source/utils.ts
+16-10Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import { outOfRange, writeOutOfRange } from './errors.ts';
88

99
const int32s = new Int32Array(1);
1010
const float32s = new Float32Array(int32s.buffer);
11-
// A float64's words are set little-endian, whatever the host's byte order
12-
const float64s = new DataView(new ArrayBuffer(SIZEOF_64));
11+
// A 64-bit value's words are set little-endian, whatever the host's byte
12+
// order, and the value read from them. Adding an int64's words as bigints
13+
// allocates each of them, and took 26 ns a read rather than 5 to 10 in
14+
// Node 26. Each read sets the words itself: through a function that they
15+
// shared, readFloat64() took 6% longer.
16+
const words64 = new DataView(new ArrayBuffer(SIZEOF_64));
1317

1418
export function readInt8(bytes: Uint8Array, offset: number): number {
1519
return (readUint8(bytes, offset) << 24) >> 24;
@@ -44,15 +48,17 @@ export function readUint32(bytes: Uint8Array, offset: number): number {
4448
}
4549

4650
export function readInt64(bytes: Uint8Array, offset: number): bigint {
47-
return BigInt.asIntN(64, readUint64(bytes, offset));
51+
assertReadable(bytes, offset, SIZEOF_64);
52+
words64.setInt32(0, readInt32(bytes, offset), true);
53+
words64.setInt32(SIZEOF_32, readInt32(bytes, offset + SIZEOF_32), true);
54+
return words64.getBigInt64(0, true);
4855
}
4956

5057
export function readUint64(bytes: Uint8Array, offset: number): bigint {
5158
assertReadable(bytes, offset, SIZEOF_64);
52-
return (
53-
BigInt(readUint32(bytes, offset)) +
54-
(BigInt(readUint32(bytes, offset + 4)) << 32n)
55-
);
59+
words64.setInt32(0, readInt32(bytes, offset), true);
60+
words64.setInt32(SIZEOF_32, readInt32(bytes, offset + SIZEOF_32), true);
61+
return words64.getBigUint64(0, true);
5662
}
5763

5864
export function readFloat32(bytes: Uint8Array, offset: number): number {
@@ -62,9 +68,9 @@ export function readFloat32(bytes: Uint8Array, offset: number): number {
6268

6369
export function readFloat64(bytes: Uint8Array, offset: number): number {
6470
assertReadable(bytes, offset, SIZEOF_64);
65-
float64s.setInt32(0, readInt32(bytes, offset), true);
66-
float64s.setInt32(4, readInt32(bytes, offset + 4), true);
67-
return float64s.getFloat64(0, true);
71+
words64.setInt32(0, readInt32(bytes, offset), true);
72+
words64.setInt32(SIZEOF_32, readInt32(bytes, offset + SIZEOF_32), true);
73+
return words64.getFloat64(0, true);
6874
}
6975

7076
function assertReadable(

0 commit comments

Comments
0 (0)
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.