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

cacheVariable sometimes is triggered for immutable variables #15

Open
CodeSandwich opened this issue Jan 26, 2023 · 1 comment
Open

Comments

@CodeSandwich
Copy link

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
Copy link
Owner

Picodes commented Feb 7, 2023

Thanks @CodeSandwich!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants