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

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

Gas Optimizations #10

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

Comments

@code423n4
Copy link
Contributor

1. using calldata instead of memory for read-only arguments in external functions saves gas

2. abi.encode() is less efficient than abi.encodepacked()

3. not using the named return variables when a function returns, wastes deployment gas

4. using bools for storage incurs overhead

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/58f635312aa21f947cae5f8578638a85aa2519f5/contracts/security/ReentrancyGuard.sol#L23-L27

Use uint256(1) and uint256(2) for true/false to avoid a Gwarmaccess (100 gas) for the extra SLOAD, and to avoid Gsset (20000 gas) when changing from ‘false’ to ‘true’, after having been ‘true’ in the past

5. state variables only set in the constructor should be declared immutable

avoids a gsset (20000 gas)

6. state variables should be cached in stack variables rather than re-reading them from storage

owner

7. can make the variable outside the loop to save gas

8. <array>.length should not be looked up in every loop of a for-loop

9. ++i costs less gas than i++, especially when it’s used in for-loops (--i/i-- too)

Saves 6 gas per loop

10. it costs more gas to initialize non-constant/non-immutable variables to zero than to let the default of zero be applied

11. ++i/i++ should be unchecked{++i}/unchecked{i++} when it is not possible for them to overflow, as is the case when used in for-loop and while-loops

12. Use a solidity version of at least 0.8.10 to have external calls skip contract existence checks if the external call has a return value

only in files

13. Use a solidity version of at least 0.8.13 to get the ability to use using for with a list of free functions

14. using private rather than public for constants, saves gas

If needed, the values can be read from the verified contract source code, or if there are multiple values there can be a single getter function that returns a tuple of the values of all currently-public constants. Saves 3406-3606 gas in deployment gas due to the compiler not having to create non-payable getter functions for deployment calldata, not having to store the bytes of the value outside of where it’s used, and not adding another entry to the method ID table

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