0

Why does changing a storage slot's value from zero to non-zero cost more than changing from non-zero to non-zero, zero to zero, or non-zero to zero?

0

By default, all storage locations have zero value on the EVM. So when you change a slot from zero to non-zero, the overall size of the state increases. This means, all the nodes have to allocate extra storage to accommodate for the new slot you created. However, when you change a non-zero to another non-zero, it doesn't increase the overall size of the EVM state. That storage slow was already occupying some space and it's just rewritten. Hence the difference in gas used.

Additionally, some gas is refunded when you delete some variable, when the SSTORE or SELFDESTRUCT opcode is executed, as it frees up storage. Read more

2
  • Excellent. Thanks. Jan 17 at 16:01
  • Ah, didn't know I could do that :) Jan 17 at 17:29

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.