Open code423n4 opened 2 years ago
++I
I++
I += 1
--I
I--
I -= 1
Pre-increments and pre-decrements are cheaper.
For a uint256 i variable, the following is true with the Optimizer enabled at 10k:
uint256 i
Increment:
i += 1
i++
++i
Decrement:
i -= 1
i--
--i
https://github.com/code-423n4/2022-08-olympus/blob/b5e139d732eb4c07102f149fb9426d356af617aa/src/policies/Operator.sol#L488
decimals++;
Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop.
https://github.com/code-423n4/2022-08-olympus/blob/b5e139d732eb4c07102f149fb9426d356af617aa/src/policies/Governance.sol#L278
for (uint256 step; step < instructions.length; ) {
Background info
https://github.com/code-423n4/2021-11-badgerzaps-findings/issues/36
G001
++I
COSTS LESS GAS COMPARED TOI++
ORI += 1
(SAME FOR--I
VSI--
ORI -= 1
)Pre-increments and pre-decrements are cheaper.
For a
uint256 i
variable, the following is true with the Optimizer enabled at 10k:Increment:
i += 1
is the most expensive formi++
costs 6 gas less thani += 1
++i
costs 5 gas less thani++
(11 gas less thani += 1
)Decrement:
i -= 1
is the most expensive formi--
costs 11 gas less thani -= 1
--i
costs 5 gas less thani--
(16 gas less thani -= 1
)https://github.com/code-423n4/2022-08-olympus/blob/b5e139d732eb4c07102f149fb9426d356af617aa/src/policies/Operator.sol#L488
G002 - Cache Array Length Outside of Loop
Description
Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop.
https://github.com/code-423n4/2022-08-olympus/blob/b5e139d732eb4c07102f149fb9426d356af617aa/src/policies/Governance.sol#L278
Background info
https://github.com/code-423n4/2021-11-badgerzaps-findings/issues/36