Skip to content

Commit

Permalink
chore: add getHashComputations() benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed May 26, 2024
1 parent 6d04f86 commit bc3eef8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
36 changes: 21 additions & 15 deletions packages/persistent-merkle-tree/src/hasher/as-sha256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,29 @@ export const hasher: Hasher = {
const batch = Math.floor(hcArr.length / 4);
for (let i = 0; i < batch; i++) {
const index = i * 4;
const outs = batchHash4HashObjectInputs([
hcArr[index].src0,
hcArr[index].src1,
hcArr[index + 1].src0,
hcArr[index + 1].src1,
hcArr[index + 2].src0,
hcArr[index + 2].src1,
hcArr[index + 3].src0,
hcArr[index + 3].src1,
// access array once
const {src0: src0_0, src1: src1_0, dest: dest_0} = hcArr[index];
const {src0: src0_1, src1: src1_1, dest: dest_1} = hcArr[index + 1];
const {src0: src0_2, src1: src1_2, dest: dest_2} = hcArr[index + 2];
const {src0: src0_3, src1: src1_3, dest: dest_3} = hcArr[index + 3];

const [o0, o1, o2, o3] = batchHash4HashObjectInputs([
src0_0,
src1_0,
src0_1,
src1_1,
src0_2,
src1_2,
src0_3,
src1_3,
]);
if (outs.length !== 4) {
throw Error(`batchHash4HashObjectInputs returned ${outs.length} outputs, expected 4`);
if (o0 == null || o1 == null || o2 == null || o3 == null) {
throw Error(`batchHash4HashObjectInputs return null at batch ${i} level ${level}`);
}
hcArr[index].dest.applyHash(outs[0]);
hcArr[index + 1].dest.applyHash(outs[1]);
hcArr[index + 2].dest.applyHash(outs[2]);
hcArr[index + 3].dest.applyHash(outs[3]);
dest_0.applyHash(o0);
dest_1.applyHash(o1);
dest_2.applyHash(o2);
dest_3.applyHash(o3);
}

// remaining
Expand Down
11 changes: 10 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,5 @@
import {itBench} from "@dapplion/benchmark";
import {BranchNode, getNodeH, LeafNode} from "../../src/node";
import {BranchNode, getHashComputations, getNodeH, HashComputation, LeafNode} from "../../src/node";
import {countToDepth, subtreeFillToContents} from "../../src";

describe("HashObject LeafNode", () => {
Expand Down Expand Up @@ -46,6 +46,15 @@ describe("Node batchHash", () => {
const numNodes = [250_000, 500_000, 1_000_000];

for (const numNode of numNodes) {
itBench({
id: `getHashComputations ${numNode} nodes`,
beforeEach: () => createList(numNode),
fn: (rootNode: BranchNode) => {
const hashComputations: HashComputation[][] = [];
getHashComputations(rootNode, 0, hashComputations);
},
});

itBench({
id: `batchHash ${numNode} nodes`,
beforeEach: () => createList(numNode),
Expand Down

0 comments on commit bc3eef8

Please sign in to comment.