Skip to content

Use uint256 iterators in MerkleTree loops #5414

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

Closed
heueristik opened this issue Jan 6, 2025 · 6 comments · Fixed by #5415
Closed

Use uint256 iterators in MerkleTree loops #5414

heueristik opened this issue Jan 6, 2025 · 6 comments · Fixed by #5415

Comments

@heueristik
Copy link
Contributor

🧐 Motivation

The current MerkleTree.sol contract contains two for loops, both of which use uint32 as an iterator when iterating from 0 to uint8 treeDepth. This is not optimal and uint256 should be used to minimize conversions.

📝 Details

Use uint256 for the iterators in MerkleTree.sol

for (uint32 i = 0; i < treeDepth; ++i) {

for (uint32 i = 0; i < treeDepth; i++) {

to save gas.

While on it, you can also change the for loops in the Heap.t.sol test.

for (uint32 i = 1; i < heap.length(); ++i) {

@heueristik
Copy link
Contributor Author

Let me know if I should add the change in Heap.t.sol to #5415.

@ernestognw
Copy link
Member

Hi @heueristik, thanks for sharing.

There are some gas savings during construction (I wasn't expecting it) but not much during runtime.

Captura de pantalla 2025-01-06 a la(s) 9 22 34 a m Captura de pantalla 2025-01-06 a la(s) 9 22 42 a m

Although the savings seem considerable, applying this change is considered a breaking change given the arguments will be encoded differently after merging. I'm not 100% convinced the change is worth changing the interface

@heueristik
Copy link
Contributor Author

applying this change is considered a breaking change given the arguments will be encoded differently after merging

Thank you @ernestognw for taking a look. Can you help me to understand which arguments will be encoded differently?
The loop counters i are only passed to

function unsafeAccess(address[] storage arr, uint256 pos) internal pure returns (StorageSlot.AddressSlot storage) {

for uint256 pos. What will change and break because of this?

@ernestognw
Copy link
Member

Hey @heueristik, sorry my bad, I thought these were arguments to a function. That is not the case.

Have you done some benchmarking? I suspect there may be no gas difference when using --via-ir

@arr00
Copy link
Contributor

arr00 commented Jan 6, 2025

I'm under the impression that there are no breaking changes here. I think we should merge it since using uint256 iterators is best practice.

@ernestognw
Copy link
Member

Let me know if I should add the change in Heap.t.sol to #5415.

Yeah I think this make sense too, feel free to send a PR. It's just a change in tests so I don't think it's a blocker. I moved on and merged #5415

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants