@@ -8,8 +8,12 @@ import { outOfRange, writeOutOfRange } from './errors.ts'; |
8 | 8 |
|
9 | 9 | const int32s = new Int32Array(1); |
10 | 10 | 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)); |
13 | 17 |
|
14 | 18 | export function readInt8(bytes: Uint8Array, offset: number): number { |
15 | 19 | return (readUint8(bytes, offset) << 24) >> 24; |
@@ -44,15 +48,17 @@ export function readUint32(bytes: Uint8Array, offset: number): number { |
44 | 48 | } |
45 | 49 |
|
46 | 50 | 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); |
48 | 55 | } |
49 | 56 |
|
50 | 57 | export function readUint64(bytes: Uint8Array, offset: number): bigint { |
51 | 58 | 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); |
56 | 62 | } |
57 | 63 |
|
58 | 64 | export function readFloat32(bytes: Uint8Array, offset: number): number { |
@@ -62,9 +68,9 @@ export function readFloat32(bytes: Uint8Array, offset: number): number { |
62 | 68 |
|
63 | 69 | export function readFloat64(bytes: Uint8Array, offset: number): number { |
64 | 70 | 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); |
68 | 74 | } |
69 | 75 |
|
70 | 76 | function assertReadable( |
|