You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use uint256(1) and uint256(2) for true/false to avoid a Gwarmaccess (100 gas) for the extra SLOAD, and to avoid Gsset (20000 gas) when changing from ‘false’ to ‘true’, after having been ‘true’ in the past.
See more here
[G-03] Set to payable permissionened functions that are guaranteed to revert for normal users
It will skip test that will lower the deployment and usage costs.
The extra opcodes avoided are CALLVALUE(2), DUP1(3), ISZERO(3), PUSH2(3), JUMPI(10), PUSH1(3), DUP1(3), REVERT(0), JUMPDEST(1), POP(2), which costs an average of about 21 gas per call to the function, in addition to the extra deployment cost
uint variables will never be lower than 0. Therefore, > 0 and != 0 have same meanings. Using != 0 can reduce the gas deployment cost, so it is worth using != 0 wherever possible.
Recommended mitigation steps:
Do not initialize them to 0 since it is the default value.
[G-07] ++i/i++ should be unchecked{++i}/unchecked{i++} when it is not possible to overflow
In solidity 0.8.0 and above, checked arithmetic is implemented but can be deactivated by using unchecked keyword. This saves 30-40gas per loop. Since i is bounded in the loop, there is no need for checked arithmetic if orders.length, fee.length < 28-1 and if orders.length, length, proof.length < 2256-1
Gas optimizations
[G-01] Usage of boolean state variable in storage
Use
uint256(1)
anduint256(2)
for true/false to avoid aGwarmaccess
(100 gas) for the extraSLOAD
, and to avoidGsset
(20000 gas) when changing from ‘false’ to ‘true’, after having been ‘true’ in the past.See more here
[G-02] Avoid emitting a storage variable when a memory vairable is available
When the argument of the function and the memory value are the same, you should rather emit the argument variable (memory value).
executionDelegate
by_executionDelegate
policyManager
by_policyManager
oracle
by_oracle
blockRange
by_blockRange
[G-03] Set to
payable
permissionened functions that are guaranteed to revert for normal usersIt will skip test that will lower the deployment and usage costs.
The extra opcodes avoided are
CALLVALUE(2)
,DUP1(3)
,ISZERO(3)
,PUSH2(3)
,JUMPI(10)
,PUSH1(3)
,DUP1(3)
,REVERT(0)
,JUMPDEST(1)
,POP(2)
, which costs an average of about 21 gas per call to the function, in addition to the extra deployment cost[G-04] Use
!= 0
instead of> 0
on uint variablesuint variables will never be lower than 0. Therefore, > 0 and != 0 have same meanings. Using != 0 can reduce the gas deployment cost, so it is worth using != 0 wherever possible.
[G-05] Use prefix not postfix especially in loops
Saves 6 gas per loop.
[G-06] Do not initialize non constant/non immutable vairable to zero
Not overwriting the default value for stack variable saves 8 gas per loop.
Recommended mitigation steps:
Do not initialize them to 0 since it is the default value.
[G-07]
++i/i++
should be unchecked{++i}/unchecked{i++} when it is not possible to overflowIn solidity 0.8.0 and above, checked arithmetic is implemented but can be deactivated by using
unchecked
keyword. This saves 30-40gas per loop. Sincei
is bounded in the loop, there is no need for checked arithmetic iforders.length
,fee.length
< 28-1 and iforders.length
,length
,proof.length
< 2256-1[G-8]
variable.length
should not be looked up in every loop of a for-loopThe overheads outlined below are PER LOOP, excluding the first loop
MLOAD
(3 gas)CALLDATALOAD
(3 gas)Caching the length changes each of these to a
DUP<N>
(3 gas), and gets rid of the extraDUP<N>
needed to store the stack offset[G-09] require()/revert() strings longer than 32 bytes cost extra gas
Each extra memory word of bytes past the original 32 incurs an
MSTORE
which cost 3 gas.The text was updated successfully, but these errors were encountered: