In the following files there are state variables that could be set immutable to save gas.
Code instance:
feenominators in Swivel.sol
Unnecessary array boundaries check when loading an array element twice
There are places in the code (especially in for-each loops) that loads the same array element more than once.
In such cases, only one array boundaries check should take place, and the rest are unnecessary.
Therefore, this array element should be cached in a local variable and then be loaded
again using this local variable, skipping the redundant second array boundaries check:
Code instance:
Swivel.sol.setFee - double load of d[x]
Storage double reading. Could save SLOAD
Reading a storage variable is gas costly (SLOAD). In cases of multiple read of a storage variable in the same scope, caching the first read (i.e saving as a local variable) can save gas and decrease the
overall gas uses. The following is a list of functions and the storage variables that you read twice:
Code instances:
ZcToken.sol: maturity is read twice in redeem
ZcToken.sol: underlying is read twice in redeem
ZcToken.sol: maturity is read twice in withdraw
ZcToken.sol: underlying is read twice in withdraw
Rearrange state variables
You can change the order of the storage variables to decrease memory uses.
Code instance:
In VaultTracker.sol,rearranging the storage fields can optimize to: 5 slots from: 6 slots.
The new order of types (you choose the actual variables):
Use unchecked to save gas for certain additive calculations that cannot overflow
You can use unchecked in the following calculations since there is no risk to overflow:
Code instances:
Swivel.sol (L#437) - uint256 when = block.timestamp + HOLD;
Swivel.sol (L#523) - uint256 when = block.timestamp + HOLD;
Swivel.sol (L#474) - uint256 when = block.timestamp + HOLD;
Consider inline the following functions to save gas
You can inline the following functions instead of writing a specific function to save gas.
(see https://github.com/code-423n4/2021-11-nested-findings/issues/167 for a similar issue.)
Some projects (e.g. Uniswap - https://github.com/Uniswap/interface/blob/main/src/hooks/useApproveCallback.ts#L88)
set the default value of the user's allowance to 2^256 - 1. Since the value 2^256 - 1 can also be represented in
hex as 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff. From Ethereum's yellow paper we know
that zeros are cheaper than non-zero values in the hex representation. Considering this fact, an alternative
choice could be now 0x8000000000000000000000000000000000000000000000000000000000000000 or 2^255 to represent
"infinity". If you do the calculations with Remix, you will see that the former costs 47'872 gas, while the latter
costs 45'888 gas. If you accept that infinity can also be represented via 2^255 (instead of 2^256-1), which almost
all projects can - you can already save about 4% gas leveraging this optimisation trick on those calculations.
Code instance:
Swivel.sol (L#549): uint256 max = 2**256 - 1;)
Do not cache msg.sender
We recommend not to cache msg.sender since calling it is 2 gas while reading a variable is more.
State variables that could be set immutable
In the following files there are state variables that could be set immutable to save gas.
Code instance:
Unnecessary array boundaries check when loading an array element twice
Code instance:
Storage double reading. Could save SLOAD
Reading a storage variable is gas costly (SLOAD). In cases of multiple read of a storage variable in the same scope, caching the first read (i.e saving as a local variable) can save gas and decrease the overall gas uses. The following is a list of functions and the storage variables that you read twice:
Code instances:
Rearrange state variables
You can change the order of the storage variables to decrease memory uses.
Code instance:
In VaultTracker.sol,rearranging the storage fields can optimize to: 5 slots from: 6 slots. The new order of types (you choose the actual variables):
Use != 0 instead of > 0
Using != 0 is slightly cheaper than > 0. (see https://github.com/code-423n4/2021-12-maple-findings/issues/75 for similar issue)
Code instances:
Unnecessary cast
Code instance:
Use unchecked to save gas for certain additive calculations that cannot overflow
You can use unchecked in the following calculations since there is no risk to overflow:
Code instances:
Consider inline the following functions to save gas
Code instances
Gas Optimization On The 2^256-1
Some projects (e.g. Uniswap - https://github.com/Uniswap/interface/blob/main/src/hooks/useApproveCallback.ts#L88) set the default value of the user's allowance to 2^256 - 1. Since the value 2^256 - 1 can also be represented in hex as 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff. From Ethereum's yellow paper we know that zeros are cheaper than non-zero values in the hex representation. Considering this fact, an alternative choice could be now 0x8000000000000000000000000000000000000000000000000000000000000000 or 2^255 to represent "infinity". If you do the calculations with Remix, you will see that the former costs 47'872 gas, while the latter costs 45'888 gas. If you accept that infinity can also be represented via 2^255 (instead of 2^256-1), which almost all projects can - you can already save about 4% gas leveraging this optimisation trick on those calculations.
Code instance:
Do not cache msg.sender
We recommend not to cache msg.sender since calling it is 2 gas while reading a variable is more.
Code instances: