5

When I access an unchanged value from storage multiple times, in the same function or transaction, does it get cached, or do I pay the SLOAD cost for each time I access it?

I can easily cache it in memory if not, but I'm not sure if I need to or not.

1 Answer 1

7

By using a very simple test contract:

contract Test {
    uint one = 1;
    function test() returns (uint){
        return one + one;
    }
} 

then using the debugger built into Remix, we can see that in fact there are two SLOAD calls.

Thus, no, these types of storage variables are not cached in any way, and it may be worth doing so manually.

2
  • 1
    This is true only if Optimizer is not used. When used, it appears to lower the execution cost.
    – JS_Riddler
    Feb 20, 2018 at 6:46
  • Do the transitions from a variable as it is modified from one state to another (i.e. the variable in storage increasing by one) get cached in any way? Or is that catalogued on via transaction inputs? Oct 5, 2020 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.