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

Save gas by caching array length used in for loops #7

Open
code423n4 opened this issue Nov 12, 2021 · 2 comments
Open

Save gas by caching array length used in for loops #7

code423n4 opened this issue Nov 12, 2021 · 2 comments
Assignees
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

0x0x0x

Vulnerability details

Proof of Concept

Example:

for (uint i = 0; i < arr.length; i++) {
//Operations not effecting the length of the array.
}

Loading length of array costs gas. Therefore, the length should be cached, if the length of the array doesn't change inside the loop.
Recommended implementation:

uint length = arr.length;
for (uint i = 0; i < length; i++) {
//Operations not effecting the length of the array.
}

By doing so the length is only loaded once rather than loading it as many times as iterations (Therefore, less gas is spent).

Occurences

contracts/FeeSplitter.sol:108:        for (uint256 i = 0; i < _accounts.length; i++) {
contracts/FeeSplitter.sol:125:        for (uint256 i = 0; i < _tokens.length; i++) {
contracts/FeeSplitter.sol:210:        for (uint256 i = 0; i < shareholders.length; i++) {
contracts/FeeSplitter.sol:227:        for (uint256 i = 0; i < shareholders.length; i++) {
contracts/MixinOperatorResolver.sol:32:        for (uint256 i = 0; i < requiredAddresses.length; i++) {
contracts/MixinOperatorResolver.sol:48:        for (uint256 i = 0; i < requiredAddresses.length; i++) {
contracts/NestedFactory.sol:203:        for (uint256 i = 0; i < tokens.length; i++) {
contracts/NestedFactory.sol:280:        for (uint256 i = 0; i < _orders.length; i++) {
contracts/NestedFactory.sol:316:        for (uint256 i = 0; i < _orders.length; i++) {
contracts/OperatorResolver.sol:33:        for (uint256 i = 0; i < names.length; i++) {
contracts/OperatorResolver.sol:45:        for (uint256 i = 0; i < names.length; i++) {
contracts/OperatorResolver.sol:56:        for (uint256 i = 0; i < destinations.length; i++) {
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Nov 12, 2021
code423n4 added a commit that referenced this issue Nov 12, 2021
@maximebrugel
Copy link
Collaborator

maximebrugel commented Nov 17, 2021

Some examples :

With FeeSplitter::releaseTokens (doesn't optimize with one iteration)
Before :
image

After :
image

With NestedFactory::destroy (doesn't optimize with one iteration)
Before:
image

After:
image

With feeSplitter::setShareholders (optimize with two interations)
Before:
image

After:
image

@maximebrugel
Copy link
Collaborator

maximebrugel commented Nov 17, 2021

In most cases, this will not change anything (or even make it worse) if there is mostly one or two iterations.

We will implement this if there are frequently several iterations.

@maximebrugel maximebrugel added the sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue label Nov 17, 2021
@maximebrugel maximebrugel added sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") and removed sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue labels Nov 17, 2021
@maximebrugel maximebrugel self-assigned this Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

2 participants