Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #86

Open
code423n4 opened this issue Mar 19, 2022 · 1 comment
Open

Gas Optimizations #86

code423n4 opened this issue Mar 19, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue

Comments

@code423n4
Copy link
Contributor

Catching The Array Length Prior To Loop.

Description
One can save gas by caching the array length (in stack) and using that set variable in the loop. They are as follows:
In AccountAccessController.sol There is:

allowAccounts() and blockAccounts().

Recommendation
Simply do something like so before the for loop: uint length = _accounts.length. Then add length in place of _accounts.length in the for loop.

Pragma Directive Ordering

Description
Pragma directive can not go after import directives. This can be found in the following contracts:

CollateralDepositRecord.sol

DepositHook.sol

WithdrawHook.sol

Recommendation
Move the pragma directive prior to import directives.

Unused Function Parameter

Description
There is an unused function parameter in the following contracts functions:
In DepositHook.sol
The function hook()
unused parameter uint256 _initialAmount
remove the _amount variable passed through collataral.sol
informational/maybe gas since it cuts byte code?

In WithdrawHook.sol
The function hook

Both have an unused parameter of uint256 _initialAmount.

Recommendation
In both hook() functions uint256 _initialAmount can be removed to save some gas.
Along with the removal of the _amount variable being passed to them through Collateral.sol in the functions deposit() and withdraw().

Function Ordering via Method ID

Description
As noted in the "Gas Optimization Notes" there is a list of each contracts most called functions since this is known one could simply save gas by function ordering via Method ID
Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool to help find alternative function names with lower Method IDs while keeping the original name intact.

Recommendation
Find a lower method ID name for the most called functions for example mostCalled() vs. mostCalled_41q() is cheaper by 44 gas.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Mar 19, 2022
code423n4 added a commit that referenced this issue Mar 19, 2022
@ramenforbreakfast
Copy link
Collaborator

Caching - duplicate of #18
Unused function parameters - duplicate of #4
Function ordering - 44 gas seems too small of a benefit to sacrifice readability.

@ramenforbreakfast ramenforbreakfast added the sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue label Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
Projects
None yet
Development

No branches or pull requests

2 participants