4

Example without an intermediate variable:

uint startTime = 9;
uint allowedTime = 7;

require(now > startTime + allowedTime);

Example with an intermediate variable:

uint startTime = 9;
uint allowedTime = 7;

uint deadline = startTime + allowedTime;

require(now > deadline);

Obviously those two blocks above are functionally equivalent, but the latter one introduces an intermediate variable to increase readability. I would expect the intermediate variable to cancelled out when source code gets compiled, so those two example should produce the same bytecode. If this is the case there should be no extra gas cost for usage of an intermediate variable.

Why bother? Those examples above are simple but in real situations sometimes intermediate variables are extremely useful to reduce congnitive load of reader (readible code -> bug-free code -> secure contract). So I wan't to use it if it's free gas-wise.

Now comes the question: Do intermediate variables get cancelled out by Solidity compiler and cost no extra gas? Or Solidity compiler is not that smart yet?

6

Here is more information about storing data in solidity https://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html#storage-memory-and-the-stack

Those variables, which you defined, will be stored in stack(if there is a space and you keep them in one function). in case if compile will optimize it properly it will be for free, if not, each variable will cost you PUST and POP operations (3+2 gas)

UPDATE

Optimized bytecode costs same amount of gas for both versions. Checked on pragma solidity 0.4.24

Ropsten test (Remix)

pragma solidity ^0.4.24;
contract A {
    uint t;

    function run() public returns(uint){
        uint startTime = 9;
        uint allowedTime = 7;

        uint v = startTime + allowedTime; // second test without v
        t = now + v;
        return t;
    }
}

Results:

with v
  deploy 95237 gas
  func 41470 gas

without v
  deploy 95237 gas
  func 41470 gas
  • So what I understand is Solidity compiler can't cancel out intermediate variables during optimization yet? – ferit Oct 7 '18 at 13:38
  • no, you understood wrong. compiler with optimization removes intermediate variables – Aquila Oct 7 '18 at 13:47
  • If removes why it costs gas? – ferit Oct 7 '18 at 14:04
  • 1
    @ferit if compile two blocks of code which you wrote above (with and without intermediate variable) with optimization, deployment and execution should cost same amount of gas in both cases. I put my test results above – Aquila Oct 7 '18 at 15:14
2

It depends on the settings used when compiling the Solidity.

https://solidity.readthedocs.io/en/v0.4.25/using-the-compiler.html

As tested by @Aquila, compiling with the optimizer leads to the same bytecode and gas cost.

If the optimizer is not used, the compiler would produce more bytecode, and the gas cost would be higher.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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