Skip to content

Commit

Permalink
chore: benchmark merkleize() vs merkleizeInto()
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jul 16, 2024
1 parent c939ee7 commit 805b350
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/ssz/test/perf/eth2/beaconBlock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("Benchmark BeaconBlock.hashTreeRoot()", function () {
const root = ssz.deneb.BeaconBlock.hashTreeRoot(block);
console.log("BeaconBlock.hashTreeRoot() root", toHexString(root));
itBench({
id: `Deneb BeaconBlock.hashTreeRoot(), numTransaciton=${numTransaction}`,
id: `Deneb BeaconBlock.hashTreeRoot(), numTransaction=${numTransaction}`,
beforeEach: () => {
clearCachedRoots(block);
return block;
Expand Down
20 changes: 19 additions & 1 deletion packages/ssz/test/perf/merkleize.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {itBench} from "@dapplion/benchmark";
import {bitLength} from "../../src/util/merkleize";
import {bitLength, merkleize} from "../../src/util/merkleize";
import {merkleizeInto} from "@chainsafe/persistent-merkle-tree";

describe("merkleize / bitLength", () => {
for (const n of [50, 8000, 250000]) {
Expand All @@ -13,6 +14,23 @@ describe("merkleize / bitLength", () => {
}
});

describe("merkleize vs persistent-merkle-tree merkleizeInto", () => {
const chunkCounts = [4, 8, 16, 32];

for (const chunkCount of chunkCounts) {
const rootArr = Array.from({length: chunkCount}, (_, i) => Buffer.alloc(32, i));
const roots = Buffer.concat(rootArr);
const result = Buffer.alloc(32);
itBench(`merkleizeInto ${chunkCount} chunks`, () => {
merkleizeInto(roots, chunkCount, result, 0);
});

itBench(`merkleize ${chunkCount} chunks`, () => {
merkleize(rootArr, chunkCount);
});
}
});

// Previous implementation, replaced by bitLength
function bitLengthStr(n: number): number {
const bitstring = n.toString(2);
Expand Down

0 comments on commit 805b350

Please sign in to comment.