Closed 0xbok closed 2 years ago
Hey @0xbok, thank you very much! The ZK-kit solidity packages don't use Jest, they uses Mocha. You can run yarn test:report-gas
to generate a report and check your performance improvements, from the packages/incremental-merkle-tree.sol
folder. Let me know if it works!
Thanks @cedoor, I checked locally and there were significant gas savings, created the PR.
I think the following will give gas savings in the decreasing order of savings:
self.depth
is read several times in a function and in for loops, which can be avoided by caching it in memory first. It avoids expensiveSLOAD
s. An example here: https://github.com/privacy-scaling-explorations/zk-kit/blob/db0af5cc0bc1c50691a306e951ddca345a99702e/packages/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol#L162-L168depth
can be madeuint256
instead ofuint8
. This can give gas savings as while readingself.depth
, it won't need to unpack touint8
since storage reads the entire slot. https://github.com/privacy-scaling-explorations/zk-kit/blob/db0af5cc0bc1c50691a306e951ddca345a99702e/packages/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol#L9i++
in the for loops can be instead incremented through:I tried verifying that these steps actually give savings, but didn't find a way to generate gas report since it uses
jest
, which I'm not familiar with.