Skip to content

Commit

Permalink
docs(imt.sol): add code comments in the lean imt
Browse files Browse the repository at this point in the history
add code comments in the lean imt when the variables tree size and depth are created to optimize gas

re privacy-scaling-explorations#209
  • Loading branch information
vplasencia committed Mar 20, 2024
1 parent 9e0d2e3 commit 3b353fa
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/imt.sol/contracts/internal/InternalLeanIMT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ library InternalLeanIMT {
revert LeafAlreadyExists();
}

// Cache tree size to optimize gas
uint256 treeSize = self.size;

while (2 ** self.depth < treeSize + 1) {
self.depth += 1;
}

// Cache tree depth to optimize gas
uint256 treeDepth = self.depth;

uint256 index = treeSize;
Expand Down Expand Up @@ -84,6 +86,7 @@ library InternalLeanIMT {
/// @param leaves: The values of the new leaves to be inserted into the tree.
/// @return The root after the leaves have been inserted.
function _insertMany(LeanIMTData storage self, uint256[] calldata leaves) internal returns (uint256) {
// Cache tree size to optimize gas
uint256 treeSize = self.size;

// Check that all the new values are correct to be added.
Expand Down Expand Up @@ -235,6 +238,7 @@ library InternalLeanIMT {
uint256 lastIndex = self.size - 1;
uint256 i = 0;

// Cache tree depth to optimize gas
uint256 treeDepth = self.depth;

for (uint256 level = 0; level < treeDepth; ) {
Expand Down

0 comments on commit 3b353fa

Please sign in to comment.