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 #2

Open
code423n4 opened this issue May 28, 2022 · 1 comment
Open

Gas Optimizations #2

code423n4 opened this issue May 28, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

cache in variables instead of loading

description

The code can be optimized by minimising the number of SLOADs. SLOADs are expensive (100 gas) compared to MLOADs/MSTOREs (3 gas).

findings

array lengths should be cached

/2022-05-backd/protocol/contracts/access/RoleManager.sol
82: for (uint256 i; i < roles.length; i = i.uncheckedInc()) {
/2022-05-backd/protocol/contracts/RewardHandler.sol
42: for (uint256 i; i < pools.length; i = i.uncheckedInc()) {
259: for (uint256 i; i < actions.length; i = i.uncheckedInc()) {
/2022-05-backd/protocol/contracts/tokenomics/FeeBurner.sol
56: for (uint256 i; i < tokens_.length; i = i.uncheckedInc()) {
/2022-05-backd/protocol/contracts/tokenomics/InflationManager.sol
116: for (uint256 i; i < stakerVaults.length; i = i.uncheckedInc()) {
/2022-05-backd/protocol/contracts/tokenomics/VestedEscrow.sol
94: for (uint256 i; i < amounts.length; i = i.uncheckedInc()) {

use calldata instead of memory

description

Use calldata instead of memory for function parameters saves gas if the function argument is only read.

findings

/2022-05-backd/protocol/contracts/tokenomics/FeeBurner.sol
43: function burnToTarget(address[] memory tokens_, address targetLpToken_)

named returns and a return statement isn’t necessary

description

Removing unused named returns variables can reduce gas usage (MSTOREs/MLOADs) and improve code clarity. To save gas and improve code quality: consider using only one of those.

findings

/2022-05-backd/protocol/contracts/tokenomics/FeeBurner.sol
47: returns (uint256 received)

using prefix increments save gas

description

Prefix increments are cheaper than postfix increments,
eg ++i rather than i++

findings

/2022-05-backd/protocol/contracts/tokenomics/KeeperGauge.sol
98: epoch++;

use custom errors

description

use custom errors instead of revert strings

If the contract(s) in scope allow using Solidity >=0.8.4, consider using Custom Errors as they are more gas efficient while allowing developers to describe the error in detail using NatSpec.

findings

eg

/2022-05-backd/protocol/contracts/tokenomics/VestedEscrow.sol
82: require(!initializedSupply, "Supply already initialized once");
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 28, 2022
code423n4 added a commit that referenced this issue May 28, 2022
This was referenced Jun 3, 2022
@chase-manning chase-manning added sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) labels Jun 6, 2022
@GalloDaSballo
Copy link
Collaborator

cache in variables instead of loading

Saves 3 gas per instance
3 * 6 = 18

use calldata instead of memory

As with all other submissions, no math = 0 points

named returns and a return statement isn’t necessary

Should save 3 gas (MSTORE of 0 value)

using prefix increments save gas

Saves 5 gas

## use custom errors
No math = no points

Total Gas Saved
26

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) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants