Audit firm Certik has published an article outlining the gas mechanism of the Ethereum Virtual Machine (EVM), key concepts related to gas optimization, and 10 best practices for optimizing gas when developing smart contracts.
1) Minimize storage usage: Non-permanent data can be stored in memory; reduce storage modifications by saving intermediate results in memory and only assigning the result to storage variables after all calculations are completed. 2) Variable packing: The Solidity compiler packs contiguous storage variables during compilation and uses 32-byte slots as the basic unit of variable storage. Variable packing means arranging variables so that multiple variables can fit in one slot. 3) Use saved data types: A variable can be represented by multiple data types, but different data types have different operational costs. Choosing the most appropriate type will help optimize gas usage.
4) Use fixed-size variables instead of dynamic variables: If the data can fit into 32 bytes, it is recommended to use the bytes32 data type instead of bytes or strings. Generally, any fixed-size variable is cheaper than a variable of variable size. If byte length can be limited, use the smallest byte between bytes1 and bytes32. 5) Maps vs arrays: In most cases, maps are more efficient and lower cost than arrays. Arrays are iterable and packable. 6) Use call data instead of memory: If a function argument is read-only, use calldata instead of memory. This avoids unnecessary copying from function call data to memory.
- Overview of Binance Labs Investments: Which Fields are They Currently Focusing On?
- Comparison of Design Differences between Pyth Network and Chainlink Oracle Networks
- Smart money that earns 60 times profit in 2 months
7) Use constants / immutable keywords as much as possible: Constant / immutable variables are not stored in contract storage. These variables are calculated at compile time and stored in the protocol’s bytecode. Therefore, their access cost is much lower than storage, and it is recommended to use the Constant / Immutable keyword as much as possible. 8) Use unchecked when underflow / overflow is not possible: When developers determine that arithmetic operations will not cause underflow or overflow, the unchecked keyword introduced in Solidity v0.8.0 can be used to avoid redundant arithmetic underflow / overflow checks and save gas costs.
9) Modifier optimization: Modifiers can increase bytecode size and gas usage, as modifier code is copied in all instances where it is used. One way to optimize modifier gas costs is to reflect on the internal function _checkOwner (), allowing the internal function to be reused in modifiers. 10) Short-circuiting: In the case of || and && operators, calculations are short-circuited, meaning that if the first condition has already determined the result of the logical expression, the second condition is not calculated. To optimize for lower gas usage, arrange conditions so that the lower-cost calculations come first. This can bypass the execution of more expensive calculations.