Skip to content

Commit

Permalink
perf: ⚡️ improve further genVarInt performance
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Oct 5, 2020
1 parent fa3f4a4 commit 34a4196
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions benchmarks/genVarInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const {genVarInt: v4} = require('../es6/util/genVarInt/v4');
const {genVarInt: v5} = require('../es6/util/genVarInt/v5');
const {parseVarInt} = require('../es6/util/parse');

const max = 1e5;
const max = 5e6;
const suite = new Benchmark.Suite;
const run = (genVarInt) => {
for (let i = 0; i < max; i++) {
for (let i = 0; i < max; i += 11) {
if (i !== parseVarInt(genVarInt(i), 0)[0])
throw Error('Invalid parsing');
}
Expand Down
10 changes: 5 additions & 5 deletions src/util/genVarInt/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
```
node benchmarks/genVarInt.js
genVarInt v1 x 50.24 ops/sec ±1.75% (65 runs sampled)
genVarInt v2 x 111 ops/sec ±0.48% (81 runs sampled)
genVarInt v3 x 123 ops/sec ±1.16% (79 runs sampled)
genVarInt v4 x 124 ops/sec ±1.39% (79 runs sampled)
genVarInt v5 x 171 ops/sec ±0.41% (87 runs sampled)
genVarInt v1 x 10.74 ops/sec ±2.54% (31 runs sampled)
genVarInt v2 x 23.68 ops/sec ±0.55% (43 runs sampled)
genVarInt v3 x 27.15 ops/sec ±0.53% (49 runs sampled)
genVarInt v4 x 27.11 ops/sec ±1.28% (49 runs sampled)
genVarInt v5 x 38.55 ops/sec ±0.50% (67 runs sampled)
Fastest is genVarInt v5
```
4 changes: 2 additions & 2 deletions src/util/genVarInt/v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const genVarInt = (num: number): Buffer => {
}

const buf = Buffer.allocUnsafe(4);
buf.writeUInt16LE(((0b100000000000000 | (num & 0b011111110000000)) << 1) | (0b10000000 | (num & 0b01111111)), 0);
buf.writeUInt16LE((((num >> 21) & 0b01111111) << 8) | (0b10000000 | ((num >> 14) & 0b01111111)), 2);
buf.writeUInt32LE((((((num >> 21) & 0b01111111) << 8) | (0b10000000 | ((num >> 14) & 0b01111111))) << 16) |
((0b100000000000000 | (num & 0b011111110000000)) << 1) | (0b10000000 | (num & 0b01111111)), 0);
return buf;
};

0 comments on commit 34a4196

Please sign in to comment.