Skip to content

Commit

Permalink
feat: set default hasher to hashtree for benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jul 23, 2024
1 parent 339398e commit 40e1a4d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
10 changes: 5 additions & 5 deletions packages/persistent-merkle-tree/test/perf/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {itBench} from "@dapplion/benchmark";
import {BranchNode, getHashComputations, getNodeH, HashComputation, LeafNode} from "../../src/node";
import {countToDepth, subtreeFillToContents} from "../../src";
import {BranchNode, getNodeH, LeafNode} from "../../src/node";
import {countToDepth, getHashComputations, HashComputation, subtreeFillToContents} from "../../src";
import {batchHash} from "../utils/batchHash";

describe("HashObject LeafNode", () => {
// Number of new nodes created in processAttestations() on average
Expand Down Expand Up @@ -50,16 +51,15 @@ describe("Node batchHash", () => {
id: `getHashComputations ${numNode} nodes`,
beforeEach: () => createList(numNode),
fn: (rootNode: BranchNode) => {
const hashComputations: HashComputation[][] = [];
getHashComputations(rootNode, 0, hashComputations);
getHashComputations(rootNode, 0, []);
},
});

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

Expand Down
9 changes: 4 additions & 5 deletions packages/persistent-merkle-tree/test/perf/validators.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {itBench, setBenchOpts} from "@dapplion/benchmark";
import {
BranchNode,
LeafNode,
subtreeFillToContents,
Node,
Expand All @@ -9,6 +8,7 @@ import {
getHashComputations,
} from "../../src";
import {MemoryTracker} from "../utils/memTracker";
import {batchHash} from "../utils/batchHash";

/**
* Below is measured on Mac M1.
Expand Down Expand Up @@ -36,8 +36,7 @@ describe("Track the performance of validators", () => {

const tracker = new MemoryTracker();
tracker.logDiff("Start");
// const vc = 250_000;
const vc = 1_600_000;
const vc = 250_000;
// see createValidatorList
const depth = countToDepth(BigInt(vc)) + 1;
// cache roots of zero nodes
Expand Down Expand Up @@ -65,7 +64,7 @@ describe("Track the performance of validators", () => {
return node;
},
fn: (node) => {
(node as BranchNode).batchHash();
batchHash(node);
},
});

Expand All @@ -76,7 +75,7 @@ describe("Track the performance of validators", () => {
return node;
},
fn: (node) => {
(node as BranchNode).hashComputations;
getHashComputations(node, 0, []);
},
});
});
Expand Down
15 changes: 1 addition & 14 deletions packages/persistent-merkle-tree/test/unit/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import {
uint8ArrayToHashObject,
setNodesAtDepth,
findDiffDepthi,
BranchNode,
HashComputation,
getHashComputations,
HashComputationLevel,
executeHashComputations,
} from "../../src";
import {batchHash} from "../utils/batchHash";

describe("fixed-depth tree iteration", () => {
it("should properly navigate the zero tree", () => {
Expand Down Expand Up @@ -294,14 +292,3 @@ function toHex(bytes: Buffer | Uint8Array): string {
return Buffer.from(bytes).toString("hex");
}

/**
* This is only a test utility function, don't want to use it in production because it allocates memory every time.
*/
function batchHash(node: Node): void {
const hashComputations: HashComputationLevel[] = [];
getHashComputations(node, 0, hashComputations);
executeHashComputations(hashComputations);
if (node.h0 === null) {
throw Error("Root node h0 is null");
}
}
15 changes: 15 additions & 0 deletions packages/persistent-merkle-tree/test/utils/batchHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HashComputationLevel, getHashComputations } from "../../src/hashComputation";
import { executeHashComputations } from "../../src/hasher";
import { Node } from "../../src/node";

/**
* This is only a test utility function, don't want to use it in production because it allocates memory every time.
*/
export function batchHash(node: Node): void {
const hashComputations: HashComputationLevel[] = [];
getHashComputations(node, 0, hashComputations);
executeHashComputations(hashComputations);
if (node.h0 === null) {
throw Error("Root node h0 is null");
}
}
6 changes: 3 additions & 3 deletions setHasher.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Set the hasher to as-sha256
// Used to run benchmarks with with visibility into as-sha256 performance, useful for Lodestar
// Set the hasher to hashtree
// Used to run benchmarks with with visibility into hashtree performance, useful for Lodestar
import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js";
import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js";
import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/hashtree.js";
setHasher(hasher);

0 comments on commit 40e1a4d

Please sign in to comment.