Skip to content

Commit 3234bb6

Browse files
authored
fix: improve uint64 perf (#122)
Running `/packages/protons-benchmark/src/numbers/index.ts` shows a nice speed up for `uint64number` performance: Before: ``` -- read -- uint64 (BigInt) x 73,711,416 ops/sec ±0.57% (88 runs sampled) uint64number x 75,126,986 ops/sec ±0.26% (96 runs sampled) uint64string x 31,420,170 ops/sec ±0.43% (94 runs sampled) Fastest is uint64number -- write -- uint64 (BigInt) x 41,097,033 ops/sec ±0.61% (94 runs sampled) uint64number x 89,523,652 ops/sec ±0.60% (97 runs sampled) uint64string x 18,610,059 ops/sec ±0.19% (98 runs sampled) Fastest is uint64number ``` After: ``` -- read -- uint64 (BigInt) x 73,310,378 ops/sec ±0.60% (91 runs sampled) uint64number x 134,356,515 ops/sec ±0.48% (95 runs sampled) uint64string x 31,575,496 ops/sec ±0.53% (90 runs sampled) Fastest is uint64number -- write -- uint64 (BigInt) x 41,444,957 ops/sec ±0.47% (95 runs sampled) uint64number x 114,640,310 ops/sec ±0.81% (94 runs sampled) uint64string x 18,531,535 ops/sec ±0.37% (97 runs sampled) Fastest is uint64number ```
1 parent 9f03e47 commit 3234bb6

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

packages/protons-runtime/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
"release": "aegir release"
127127
},
128128
"dependencies": {
129+
"uint8-varint": "^2.0.2",
129130
"uint8arraylist": "^2.4.3",
130131
"uint8arrays": "^5.0.1"
131132
},

packages/protons-runtime/src/utils/reader.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { decodeUint8Array, encodingLength } from 'uint8-varint'
12
import { readFloatLE, readDoubleLE } from './float.js'
23
import { LongBits } from './longbits.js'
34
import * as utf8 from './utf8.js'
@@ -304,7 +305,9 @@ export class Uint8ArrayReader implements Reader {
304305
* JavaScript number
305306
*/
306307
uint64Number (): number {
307-
return this.readLongVarint().toNumber(true)
308+
const value = decodeUint8Array(this.buf, this.pos)
309+
this.pos += encodingLength(value)
310+
return value
308311
}
309312

310313
/**

packages/protons-runtime/src/utils/writer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { encodeUint8Array, encodingLength } from 'uint8-varint'
12
import { allocUnsafe } from 'uint8arrays/alloc'
23
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
34
import { writeFloatLE, writeDoubleLE } from './float.js'
@@ -186,8 +187,7 @@ class Uint8ArrayWriter implements Writer {
186187
* Writes an unsigned 64 bit value as a varint
187188
*/
188189
uint64Number (value: number): this {
189-
const bits = LongBits.fromNumber(value)
190-
return this._push(writeVarint64, bits.length(), bits)
190+
return this._push(encodeUint8Array, encodingLength(value), value)
191191
}
192192

193193
/**

0 commit comments

Comments
 (0)