From 603b9c2a5dcc20dcffa81909b707a614a933888a Mon Sep 17 00:00:00 2001 From: Ilyas Ridhuan Date: Mon, 18 Nov 2024 19:10:06 +0000 Subject: [PATCH] test(avm): minor benchmarking (#9869) Just a very small micro-benchmark for the a future PR to reference. This sets up the smallest "worst-case" scenario for the current ephmeral tree implementation. 1) An initial public data tree is seeded with 128 dummy leaves, occupying slots 0 to 127 2) We update slot 0 with a new value - this causes the ephemeral tree to track slot 0 as the `indexedTreeMin`. 3) Inserting 64 new slots (as per a tx), results in 126 DB accesses and a linear (in the leaf count) search complexity a) The DB reads occur because the ephemeral tree doesnt track slots 1 to 127 b) All reads are O(n) as we traverse from min leaf to the low leaf info --- .../simulator/src/avm/avm_tree.test.ts | 21 +++++++++++++++++++ yarn.lock | 4 ---- 2 files changed, 21 insertions(+), 4 deletions(-) delete mode 100644 yarn.lock diff --git a/yarn-project/simulator/src/avm/avm_tree.test.ts b/yarn-project/simulator/src/avm/avm_tree.test.ts index fbb30accfe2..e910a49ff62 100644 --- a/yarn-project/simulator/src/avm/avm_tree.test.ts +++ b/yarn-project/simulator/src/avm/avm_tree.test.ts @@ -488,3 +488,24 @@ describe('AVM Ephemeral Tree Sanity Test', () => { expect(localRoot.toBuffer()).toEqual(treeInfo.root); }); }); + +/* eslint no-console: ["error", { allow: ["time", "timeEnd"] }] */ +describe('A basic benchmark', () => { + it('Should benchmark writes', async () => { + // This random is fine for now, since the entry leaves are between 0-127 + // We just want to make sure there are minimal "updates" from this set + const leaves = Array.from({ length: 64 }, _ => Fr.random()); + const slots = leaves.map((_, i) => new Fr(i + 128)); + + const container = await AvmEphemeralForest.create(copyState); + + // Updating the first slot, triggers the index 0 to be added to the minimum stored key in the container + await container.writePublicStorage(new Fr(0), new Fr(128)); + console.time('benchmark'); + // These writes are all new leaves and should be impacted by the key sorted algorithm of the tree. + for (let i = 0; i < leaves.length; i++) { + await container.writePublicStorage(slots[i], leaves[i]); + } + console.timeEnd('benchmark'); + }); +}); diff --git a/yarn.lock b/yarn.lock deleted file mode 100644 index fb57ccd13af..00000000000 --- a/yarn.lock +++ /dev/null @@ -1,4 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - -