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

Open
code423n4 opened this issue Aug 30, 2022 · 0 comments
Open

Gas Optimizations #108

code423n4 opened this issue Aug 30, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

code423n4 commented Aug 30, 2022

Gas Optimizations

Title N° of Appearances
[G-1] Constants that are private instead of public save gas. 8
[G-2] Variables set only while constructing should be immutable 3
[G-3] Multiple mappings could be combined 8
[G-4] Users can manipulate string chain size 1
[G-5] Comparing a uint with != is cheaper than using > 1

Total: 21 appearances over 5 issues.

Gas Optimizations

[G-1] Constants that are private instead of public save gas.

Because constant variables should be declared an initialized as a one-liner, their value could be easily retrieved by reading directly the source code. Removing the public modifier and using private instead, does not create a function getter for the public constant saving around 3500 gas on deployment.

Found 8 times
RANGE.sol   L65:       uint256 public constant FACTOR_SCALE = 1e4;
Operator.sol   L89:       uint32 public constant FACTOR_SCALE = 1e4;
Governance.sol   L121:       uint256 public constant SUBMISSION_REQUIREMENT = 100;
Governance.sol   L124:       uint256 public constant ACTIVATION_DEADLINE = 2 weeks;
Governance.sol   L127:       uint256 public constant GRACE_PERIOD = 1 weeks;
Governance.sol   L130:       uint256 public constant ENDORSEMENT_THRESHOLD = 20;
Governance.sol   L133:       uint256 public constant EXECUTION_THRESHOLD = 33;
Governance.sol   L137:       uint256 public constant EXECUTION_TIMELOCK = 3 days;


[G-2] Variables set only while constructing should be immutable


This operation reduces the gas cost each time a variable is consulted and also avoids a Gsset (20,000 gas) while constructing.

Found 3 times

Heart.sol   L48:       IOperator internal _operator;
BondCallback.sol   L43:       aggregator = aggregator_;
BondCallback.sol   L44:       ohm = ohm_;


[G-3] Multiple mappings could be combined


If there are used mappings with repeated types of keys, it is a sign that they could be refactored into a single mapping that points to a struct when appropriate. Depending on the data types and their sizes, this could avoid triggering a Gsset operation (consumes 20,000 gas while changing from zero to a non zero value). Because memory slots are calculated via keccak256, reducing the amount of slots also reduce for the compiler the need to compute the keys' hash.

Found 8 times

Governance.sol   L96:       mapping(uint256 => ProposalMetadata) public getProposalMetadata;
Governance.sol   L99:       mapping(uint256 => uint256) public totalEndorsementsForProposal;
Governance.sol   L102:       mapping(uint256 => mapping(address => uint256)) public userEndorsementsForProposal;
Governance.sol   L105:       mapping(uint256 => bool) public proposalHasBeenActivated;
Governance.sol   L108:       mapping(uint256 => uint256) public yesVotesForProposal;
Governance.sol   L111:       mapping(uint256 => uint256) public noVotesForProposal;
Governance.sol   L114:       mapping(uint256 => mapping(address => uint256)) public userVotesForProposal;
Governance.sol   L117:       mapping(uint256 => mapping(address => bool)) public tokenClaimsForProposal;


[G-4] Users can manipulate string chain size


Users can freely use string chains with uncapped size. When the 32 bytes slot size is exceeded, a new slow will be used to render and store that string. In order to provide a better user experience, it is advisable to constraint the user-input string size.

Found 1 time

Governance.sol   L162:       string memory proposalURI_


[G-5] Comparing a uint with != is cheaper than using >


For unsigned integers, it is cheaper to check that they are non zero values with the unequal operator rather than checking their greatness. While compiling with the optimizer enabled this change saves around 6 gas.

Found 1 time

Governance.sol   L247:       if (userVotesForProposal[activeProposal.proposalId][msg.sender] > 0) {

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Aug 30, 2022
code423n4 added a commit that referenced this issue Aug 30, 2022
code423n4 added a commit that referenced this issue Aug 30, 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)
Projects
None yet
Development

No branches or pull requests

1 participant