Skip to content

Commit

Permalink
fix: improve uint64 perf (#122)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
wemeetagain authored Jan 31, 2024
1 parent 9f03e47 commit 3234bb6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/protons-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"release": "aegir release"
},
"dependencies": {
"uint8-varint": "^2.0.2",
"uint8arraylist": "^2.4.3",
"uint8arrays": "^5.0.1"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/protons-runtime/src/utils/reader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { decodeUint8Array, encodingLength } from 'uint8-varint'
import { readFloatLE, readDoubleLE } from './float.js'
import { LongBits } from './longbits.js'
import * as utf8 from './utf8.js'
Expand Down Expand Up @@ -304,7 +305,9 @@ export class Uint8ArrayReader implements Reader {
* JavaScript number
*/
uint64Number (): number {
return this.readLongVarint().toNumber(true)
const value = decodeUint8Array(this.buf, this.pos)
this.pos += encodingLength(value)
return value
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/protons-runtime/src/utils/writer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { encodeUint8Array, encodingLength } from 'uint8-varint'
import { allocUnsafe } from 'uint8arrays/alloc'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { writeFloatLE, writeDoubleLE } from './float.js'
Expand Down Expand Up @@ -186,8 +187,7 @@ class Uint8ArrayWriter implements Writer {
* Writes an unsigned 64 bit value as a varint
*/
uint64Number (value: number): this {
const bits = LongBits.fromNumber(value)
return this._push(writeVarint64, bits.length(), bits)
return this._push(encodeUint8Array, encodingLength(value), value)
}

/**
Expand Down

0 comments on commit 3234bb6

Please sign in to comment.