code-423n4 / 2022-07-swivel-findings

0 stars 1 forks source link

Gas Optimizations #190

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

If possible, use prefix increment instead of postfix increment

Prefix increment ++i returns the updated value after it's incremented and postfix increment i++ returns the original value then increments it. Prefix increment costs less gas compared to postfix increment.

Instances includes :

Marketplace/Erc20.sol:154:                                nonces[owner]++,
Swivel/Swivel.sol:100:      unchecked {i++;}
Swivel/Swivel.sol:269:      unchecked {i++;}
Swivel/Swivel.sol:418:        i++;
Swivel/Swivel.sol:511:        x++;
Swivel/Swivel.sol:564:        i++;
Creator/Erc20.sol:154:                                nonces[owner]++,

Recommendation

It is recommended to use prefix increment instead of postfix one when the return value is not needed, as both of them will give the same result and prefix increment costs less gas.

For example :

Swivel/Swivel.sol:418:        ++i;