2

We have a struct:

struct structEx {
  uint num;
}

And an array:

structEx[] arr;

Have a look at this code snippet, which just instantiates the struct and push:

function f() public {
   structEx s = structEx(0);
   arr.push(s);
}

The above method creates new storage from struct reference and when we push it to arr, copy it to array's storage.

Another way to do this:

function f() public {
   structEx memory s = structEx(0);
   arr.push(s);
}

The above method stores struct instance in memory and copies to storage when pushed to array.

So, as per my understanding second should cost lesser gas than first. Is this correct? feel free to add your thoughts and experience!

2

The first f function is not actually a legit instruction in Solidity, you'll receive an error if you try to compile it.

TypeError: Type struct structEx memory is not implicitly convertible to expected type struct structEx storage pointer.
       structEx s = structEx(0);

You can read this for more details.

So said you can actually have another variant of that function:

function f1() public {
   structEx memory s = structEx(0);
   arr.push(s);
}
function f2() public {
   arr.push(structEx(0));
}

In this case f2 will cost less gas than f1.

 // f1() 
 status     0x1 Transaction mined and execution succeed
 from   0xca35b7d915458ef540ade6068dfe2f44e8fa733c
 to     AContract.f1() 0x692a70d2e424a56d2c6c27aa97d1a86395877b3a
 gas    3000000 gas

 transaction cost   47308 gas 
 execution cost     26036 gas 
 hash   0x08de50728f2994986d07040787683747805686643a00722d94fa1fc569593031
 input  0xc27fc305
 decoded input  {}
 decoded output     {}
 logs   []
 value  0 wei

 // f2()
 status     0x1 Transaction mined and execution succeed
 from   0xca35b7d915458ef540ade6068dfe2f44e8fa733c
 to     AContract.f2() 0x692a70d2e424a56d2c6c27aa97d1a86395877b3a
 gas    3000000 gas

 transaction cost   32208 gas 
 execution cost     10936 gas 
 hash   0x1fc5d2ba0731944c052f8ba040dce8ef6e73118a0b39d916b8bee3dc01ffe4f7
 input  0x9942ec6f
 decoded input  {}
 decoded output     {}
 logs   []
 value  0 wei

Code executed in remix. The actual gas might be slightly different.

  • Thank you so much for your detailed response. It is much clearer to me now. However, could you explain why there is a difference in cost in f1() and f2(). Isn't memory supposed to be cheaper? – blockwala Apr 12 '18 at 7:55
  • 1
    Its. But f1 is saving the value of the struct in a local variable and then it push this value in the storage. f2 will write nothing in memory (explicitly) and just push the struct in the storage. Probably is an optimization of the EVM that will prevent to waste memory space needed for f1 (even if actually you need to create the object in both case and so the memory will be used somehow). But its just an empirical deduction since I don't know how it works the EVM and the gas calculation at low level. – mirg Apr 12 '18 at 8:13

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.