Skip to content

Commit

Permalink
chore: add batchHash() benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed May 25, 2024
1 parent 3cf753d commit 71713ba
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/persistent-merkle-tree/src/hasher/hashtree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO - batch: use @chainsafe/hashtree
5 changes: 4 additions & 1 deletion packages/persistent-merkle-tree/src/hasher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ export * from "./types";
export * from "./util";

/**
* Hasher used across the SSZ codebase
* Default hasher used across the SSZ codebase, this does not support batch hash.
* Use `as-sha256` hasher for batch hashing using SIMD.
* TODO - batch: Use `hashtree` hasher for 20x speedup
*/
// export let hasher: Hasher = nobleHasher;
// For testing purposes, we use the as-sha256 hasher
export let hasher: Hasher = csHasher;

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/persistent-merkle-tree/test/perf/hasher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ describe("hasher", () => {
});
}
});

// TODO - batch: test more methods
43 changes: 42 additions & 1 deletion packages/persistent-merkle-tree/test/perf/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {itBench} from "@dapplion/benchmark";
import {getNodeH, LeafNode} from "../../src/node";
import {BranchNode, getNodeH, LeafNode} from "../../src/node";
import {countToDepth, subtreeFillToContents} from "../../src";

describe("HashObject LeafNode", () => {
// Number of new nodes created in processAttestations() on average
Expand Down Expand Up @@ -40,3 +41,43 @@ describe("HashObject LeafNode", () => {
}
});
});

describe("Node batchHash", () => {
const numNodes = [250_000, 500_000, 1_000_000, 2_000_000];

for (const numNode of numNodes) {
itBench({
id: `batchHash ${numNode} nodes`,
before: () => {
return createList(numNode);
},
beforeEach: (rootNode: BranchNode) => rootNode,
fn: (rootNode: BranchNode) => {
rootNode.batchHash();
},
});

itBench({
id: `get root ${numNode} nodes`,
before: () => {
return createList(numNode);
},
beforeEach: (rootNode: BranchNode) => rootNode,
fn: (rootNode: BranchNode) => {
rootNode.root;
},
});
}
});

function createList(numNode: number): BranchNode {
const nodes = Array.from({length: numNode}, (_, i) => newLeafNodeFilled(i));
// add 1 to countToDepth for mix_in_length spec
const depth = countToDepth(BigInt(numNode)) + 1;
const node = subtreeFillToContents(nodes, depth);
return node as BranchNode;
}

function newLeafNodeFilled(i: number): LeafNode {
return LeafNode.fromRoot(new Uint8Array(Array.from({length: 32}, () => i % 256)));
}
6 changes: 3 additions & 3 deletions packages/persistent-merkle-tree/test/unit/batchHash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {subtreeFillToContents} from "../../src/subtree";
import {zeroNode} from "../../src/zeroNode";

describe("batchHash", function () {
// const numNodes = [200, 201, 202, 203];
const numNodes = [32, 33, 64];
const numNodes = [200, 201, 202, 203];
// const numNodes = [32, 33, 64];
for (const numNode of numNodes) {
it(`${numNode} nodes`, () => {
const rootNode = createList(numNode);
Expand Down Expand Up @@ -36,7 +36,7 @@ function resetNodes(node: Node, depth: number): void {
}

function newLeafNodeFilled(i: number): LeafNode {
return LeafNode.fromRoot(new Uint8Array(Array.from({length: 32}, () => i % 255)));
return LeafNode.fromRoot(new Uint8Array(Array.from({length: 32}, () => i % 256)));
}

function createList(numNode: number): BranchNode {
Expand Down
4 changes: 3 additions & 1 deletion packages/persistent-merkle-tree/test/unit/hasher.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from "chai";
import {expect} from "chai";
import {uint8ArrayToHashObject, hasher, hashObjectToUint8Array} from "../../src/hasher";

describe("hasher", function () {
Expand All @@ -14,3 +14,5 @@ describe("hasher", function () {
expect(newRoot).to.be.deep.equal(root, "hash and hash2 is not equal");
});
});

// TODO - batch: test more methods
1 change: 0 additions & 1 deletion packages/persistent-merkle-tree/test/unit/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
findDiffDepthi,
BranchNode,
HashComputation,
findDiffDepthi,
getHashComputations,
} from "../../src";

Expand Down

0 comments on commit 71713ba

Please sign in to comment.