Open code423n4 opened 2 years ago
Title: State variables that could be set immutable
Already done
Title: Unused state variables
invalid, mock file out of scope as stated in the readme
Title: Unused declared local variables
invalid, mock file out of scope as stated in the readme
Title: Unnecessary array boundaries check when loading an array element twice
Before:
After:
Title: Caching array length can save gas
duplicate from the last report mentioned in the readme, it's already done where it was useful.
Title: Prefix increments are cheaper than postfix increments
duplicate from the last report mentioned in the readme and in #3 and we don't want to do this
Title: Unnecessary index init
invalid, this makes no difference in loops where the variables must be inited to 0 during the first iteration
Title: Internal functions to private
This wouldn't work.
Title: Public functions to external
Confirmed
Title: Unnecessary payable
This is untrue.
Title: Rearrange state variables
I see no difference whatsoever.
Title: Short the following require messages
Confirmed
Title: Unused imports
Invalid, this does not affect interfaces
Title: Unused inheritance
invalid, they are
Title: Use != 0 instead of > 0
The code mentioned does not exist
Title: Unnecessary constructor
invalid, It is necessary.
Title: Unnecessary functions
invalid, They are necessary.
Title: Unnecessary cast
confirmed, reserve.withdraw(IERC20(_inputToken), _inputTokenAmount);
Title: Use calldata instead of memory
confirmed for non-mock files
Title: Consider inline the following functions to save gas
confirme
Title: Inline one time use functions
True. Acknowledge or confirm?
Title: Check if amount is not zero to save gas
True but this adds an extra check for 99.999% of cases, so we don't want to do this
My personal judgments:
operatorStorage
is already set as immutable. Invalid.Now, here is the methodology I used for calculating a score for each gas report. I first assigned each submission to be either small-optimization (1 point), medium-optimization (5 points) or large-optimization (10 points), depending on how useful the optimization is. The score of a gas report is the sum of these points, divided by the maximum number of points achieved by a gas report. This maximum number was 10 points, achieved by #67.
The number of points achieved by this report is 8 points. Thus the final score of this gas report is (8/10)*100 = 80.
Title: State variables that could be set immutable Severity: GAS
In the following files there are state variables that could be set immutable to save gas.
Title: Unused state variables Severity: GAS
Unused state variables are gas consuming at deployment (since they are located in storage) and are a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality. This is a full list of all the unused storage variables we found in your code base.
Title: Unused declared local variables Severity: GAS
Unused local variables are gas consuming, since the initial value assignment costs gas. And are a bad code practice. Removing those variables will decrease the gas cost and improve code quality. This is a full list of all the unused storage variables we found in your code base.
Title: Unnecessary array boundaries check when loading an array element twice Severity: GAS
Title: Caching array length can save gas Severity: GAS
Caching the array length is more gas efficient. This is because access to a local variable in solidity is more efficient than query storage / calldata / memory. We recommend to change from:
to:
Title: Prefix increments are cheaper than postfix increments Severity: GAS
Prefix increments are cheaper than postfix increments. Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change There is no risk of overflow caused by increamenting the iteration index in for loops (the
++i
infor (uint256 i = 0; i < numIterations; ++i)
). But increments perform overflow checks that are not necessary in this case. These functions use not using prefix increments (++x
) or not using the unchecked keyword:Title: Unnecessary index init Severity: GAS
In for loops you initialize the index to start from 0, but it already initialized to 0 in default and this assignment cost gas. It is more clear and gas efficient to declare without assigning 0 and will have the same meaning:
Title: Internal functions to private Severity: GAS
The following functions could be set private to save gas and improve code quality:
Title: Public functions to external Severity: GAS
The following functions could be set external to save gas and improve code quality. External call cost is less expensive than of public functions.
Title: Unnecessary payable Severity: GAS
The following functions are payable but msg.value isn't used - therefore the function payable state modifier isn't necessary. Payable functions are more gas expensive than others, and it's danger the users if they send ETH by mistake.
Title: Rearrange state variables Severity: GAS
You can change the order of the storage variables to decrease memory uses.
In OwnableProxyDelegation.sol,rearranging the storage fields can optimize to: 2 slots from: 3 slots. The new order of types (you choose the actual variables):
Title: Short the following require messages Severity: GAS
The following require messages are of length more than 32 and we think are short enough to short them into exactly 32 characters such that it will be placed in one slot of memory and the require function will cost less gas. The list:
Title: Unused imports Severity: GAS
In the following files there are contract imports that aren't used Import of unnecessary files costs deployment gas (and is a bad coding practice that is important to ignore)
Title: Unused inheritance Severity: GAS
Title: Use != 0 instead of > 0 Severity: GAS
Using != 0 is slightly cheaper than > 0. (see https://github.com/code-423n4/2021-12-maple-findings/issues/75 for similar issue)
Title: Unnecessary constructor Severity: GAS
The following constructors are empty. (A similar issue https://github.com/code-423n4/2021-11-fei-findings/issues/12)
Title: Unnecessary functions Severity: GAS
Title: Unnecessary cast Severity: Gas
Title: Use calldata instead of memory Severity: GAS
Use calldata instead of memory for function parameters In some cases, having function arguments in calldata instead of memory is more optimal.
Title: Consider inline the following functions to save gas Severity: GAS
Title: Inline one time use functions Severity: GAS
The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.
Title: Check if amount is not zero to save gas Severity: GAS
The following functions could skip other steps if the amount is 0. (A similar issue: https://github.com/code-423n4/2021-10-badgerdao-findings/issues/82)