If i change a existing uint variable in a struct compared to normal ones does it consumes the same amount of gas(cost)? Im trying to optimize my contracts cost

  • What are you changing it to? What are the sizes and types of the other/surrounding members of the struct? We'll probably need to see some code to comment further. – Richard Horrocks Oct 10 '18 at 19:00

If you modify an uint256 in a struct it should cost about the same than modifying a regular uint256 variable (a little extra is has to be used to adjust the offset inside the struct).

The compiler tries to packed together smaller types (like bool, uint8, uint16, uint32, etc.) to use less gas when storing an struct. But it has to generate extra code and that cause the gas to increase both when deploying and when executing a function.

Try different layouts of your struct to have together the smaller types and/or change the types to smaller ones. Measure the gas use carefully, a good change might cause too much gas use in practice, or your contract grow too large.

| improve this answer | |

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.