`cacheVariable` sometimes is triggered for immutable variables
Opened this issue · 1 comments
CodeSandwich commented
For example:
contract ImmutableCached {
uint256 public immutable one = 1;
function two() public returns (uint256 result) {
result = one + one;
}
}
yields:
### <a name="GAS-1"></a>[GAS-1] State variables should be cached in stack variables rather than re-reading them from storage
The instances below point to the second+ access of a state variable within a function. Caching of a state variable replaces each Gwarmaccess (100 gas) with a much cheaper stack read. Other less obvious fixes/optimizations include having local memory caches of state variable structs, or having local caches of state variable contracts/addresses.
*Saves 100 gas per instance*
*Instances (1)*:
``solidity
File: lol.sol
7: result = one + one;
``
This is a weird issue, because depending on how code is expressed it may not trigger, e.g. changing
result = one + one;
into
return one + one;
fixes the warning.
Picodes commented
Thanks @CodeSandwich!