⛓️Gas Saving Technique 2: Immutable Variable
Introduction
Gas optimization is essential in the development of smart contracts on the Ethereum network. One effective technique to achieve this involves using immutable variables. An immutable variable can save a significant amount of gas as it is assigned once at the time of contract deployment and is read-only thereafter. It offers an advantage over constant variables, which are evaluated every time they are accessed, consuming more gas in the process.
Understanding immutable Variable Optimization
immutable Variable OptimizationWhy Use immutable?
Gas Efficiency: Unlike
constantvariables,immutablevariables are only computed and set during contract deployment. The assigned value is stored immutably, leading to gas savings during transactions as the value isn't recalculated each time it's accessed.Readability Preserved: Using
immutableretains code readability while saving gas, providing clarity and efficiency in your smart contracts.
Practical Examples
Changing Storage Variables to immutable
Consider a contract where certain variables like bribeVault won’t change post-deployment. Using immutable instead of a regular storage variable can lead to gas savings.
Here’s a demonstration:
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ImmutableExample {
// Before optimization
// address public bribeVault;
// After optimization
address public immutable bribeVault;
constructor(address _bribeVault) {
// Immutable variables are assigned once at deployment
bribeVault = _bribeVault;
}
}Making Unchanged Variables immutable or constant
For variables that never change, like _concurShareMultiplier and _perMille, you should declare them as constant or immutable to save gas. Here’s an example:
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ConstantsExample {
// Before optimization
// uint private _concurShareMultiplier = 1e18;
// uint private _perMille = 1000;
// After optimization
uint private constant _concurShareMultiplier = 1e18;
uint private constant _perMille = 1000;
}In situations where the variable value is computed dynamically during contract deployment but remains constant thereafter, immutable is ideal:
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ImmutableDeploymentExample {
// Using immutable for dynamically set constant values
uint public immutable deploymentValue;
constructor(uint _multiplier) {
deploymentValue = _multiplier * 2; // This value is computed once at deployment
}
}Consequences and Considerations
Each usage of a "constant" may cost extra gas on each access. Although this cost is lower than storing the result in storage, it’s still more than using
immutable.Since
constantvariables aren’t real constants in the compiled bytecode, they cannot be referenced in certain constant environments like assembly or other libraries.
Conclusion
Utilizing immutable variables provides a clear path towards optimizing gas usage in your smart contracts without sacrificing code readability. Carefully identify and assign values that remain unchanged post-deployment as immutable or constant to leverage these benefits. Always conduct thorough testing to ensure the assigned values truly remain constant for the life of the contract.
Recommendation
[G-16] Gas: “constants” expressions are expressions, not constants. Use “immutable” instead
Last updated