From 3234bb61eac82ebbc40925df084793efbb0f0186 Mon Sep 17 00:00:00 2001 From: Cayman Date: Wed, 31 Jan 2024 05:19:31 -0500 Subject: [PATCH] fix: improve uint64 perf (#122) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ``` --- packages/protons-runtime/package.json | 1 + packages/protons-runtime/src/utils/reader.ts | 5 ++++- packages/protons-runtime/src/utils/writer.ts | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/protons-runtime/package.json b/packages/protons-runtime/package.json index bd09929..09dc9e2 100644 --- a/packages/protons-runtime/package.json +++ b/packages/protons-runtime/package.json @@ -126,6 +126,7 @@ "release": "aegir release" }, "dependencies": { + "uint8-varint": "^2.0.2", "uint8arraylist": "^2.4.3", "uint8arrays": "^5.0.1" }, diff --git a/packages/protons-runtime/src/utils/reader.ts b/packages/protons-runtime/src/utils/reader.ts index a594c01..abb9767 100644 --- a/packages/protons-runtime/src/utils/reader.ts +++ b/packages/protons-runtime/src/utils/reader.ts @@ -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' @@ -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 } /** diff --git a/packages/protons-runtime/src/utils/writer.ts b/packages/protons-runtime/src/utils/writer.ts index 9ebc6d5..1842024 100644 --- a/packages/protons-runtime/src/utils/writer.ts +++ b/packages/protons-runtime/src/utils/writer.ts @@ -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' @@ -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) } /**