refactor: normalize for
loop declarations
#476
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Previously, I raised a concern about the possible gas savings that could be achieved by prefix incrementing or decrementing the iterator variable within declarations of
for
loops (see #468 (comment); #475). Anyways, after having implemented the suggestion, the gas snapshots didn't change.Consequently, I researched and found out that compiling
via_ir = true
already applies this optimization and so equals the gas expenditures, but the same doesn't necessarily happen when using the legacy pipeline (see ethereum/solidity#14595). Another reason for the unaltered gas snapshots may be that the savings are negligible (less than 1 % change).Moreover, explicitly setting the iterator variable to its default value while declaring it may also be a cause of increased gas consumption. Again, I haven't seen modifications in the gas snapshots after having applied this locally, so I opted for leaving such explicit initialization momentarily as it appeared to be enforced for every loop, despite it could be not. Has this been done on purpose?
Ultimately, in relation with the
for
comparisons, I tried cacheing the length of the arrays being iterated over and, similarly, the gas savings were either negligible or none.Apart from that, this PR aims to standardize every Solidity
for
loop declaration across the codebase. For so, the iterator variable has been namedi
rather than the less frequent and longerindex
, and prefix increments and decrements have been enforced where applicable. Normalizing the comparisons by cacheing didn't seem feasible due to the ad hoc nature of each logic andstack too deep
concerns.Type of change
Checklist:
main
, or there's a description of how to mergeIssue Resolution
Closes #475.