Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MerkleTreeSha256.sol Gas optimization #72

Closed
klmcdon opened this issue Sep 3, 2019 · 1 comment
Closed

MerkleTreeSha256.sol Gas optimization #72

klmcdon opened this issue Sep 3, 2019 · 1 comment
Labels
optimization Optimization task solidity Task related to the Solidity part of the code base

Comments

@klmcdon
Copy link

klmcdon commented Sep 3, 2019

Currently, the global array "leaves" is defined dynamically as "bytes32[] leaves", and then explicitly set to size nbLeaves in the constructor. The entries of leaves are also manually initialized to their null value using a for loop in the constructor. This explicit initialization increases the gas cost of deployment by a non-trivial amount (especially for deep trees).

An alternative approach is to define a local array of fixed size nbLeaves, that is automatically initialized to its null value, in the constructor and then set the global array equal to this fixed-length array. E.g.

contract MerkleTreeSha256 {
...
bytes32[] leaves

constructor(uint treeDepth) public {
    ...   
    bytes32[] memory leavesBuilder = new bytes32[](nbLeaves);
    leaves = leavesBuilder;
}
...

}
Remix tests indicate that this reduces the cost of deployment.

@klmcdon klmcdon added solidity Task related to the Solidity part of the code base optimization Optimization task labels Sep 3, 2019
@rrtoledo
Copy link
Contributor

Done in fix-todo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization Optimization task solidity Task related to the Solidity part of the code base
Projects
None yet
Development

No branches or pull requests

2 participants