Gas Optimizations in Solidity
Gas Costly Patterns
- Comparison with Unilateral Outcome in a Loop
- Constant Outcome of a Loop
- Dead Code
- Expensive Operations in a Loop
- Loop Fusion
- Opaque Predicate
- Repeated Computations in a Loop
- Storage Variable as Loop Length
- Unnecessary Libraries
Gas Saving Patterns
- Proper Data Types
- Explicit Function Visibility
- Short Circuiting
- Constants and Immutables
- Struct Bit Packing
Gas Golfing
- Short Revert Strings
- Unchecked Arithmetic
- Optimal Comparison Operator
- Payable Functions
- Function Ordering
- Using Mulmod Over Mul & Div
- Optimal Increment and Decrement Operators
Sources/References
- https://medium.com/coinmonks/optimizing-your-solidity-contracts-gas-usage-9d65334db6c7
- https://ethereum.stackexchange.com/questions/28813/how-to-write-an-optimized-gas-cost-smart-contract
- https://arxiv.org/pdf/1703.03994.pdf
- https://ethereum.stackexchange.com/questions/3067/why-does-uint8-cost-more-gas-than-uint256
- https://www.youtube.com/watch?v=qwBkeJ84d2g&feature=youtu.be&t=68
- https://ethereum.stackexchange.com/questions/11556/use-string-type-or-bytes32
- https://gist.github.com/hrkrshnn/ee8fabd532058307229d65dcd5836ddc
- https://github.com/ZeroEkkusu/re-golf-course