I am trying to understand what expansion cost is.

We can see that there's this formula to count the memory cost -

enter image description here

Now, It's also said that this formula doesn't include the expansion cost.

Can anyone in simple terms explain what expansion cost means ?


Can anyone in simple terms explain what expansion cost means?

When your contract writes to memory, you pay for the costs associated with the number of bytes written.

However, if you are writing to an area of memory that hasn't been written to before, there is an associated additional cost with using it for the first time. Think of it as an additional tax levied for using a pristine piece of memory.

From the "Storage, Memory and the Stack" section of the Solidity docs:

Memory is expanded by a word (256-bit), when accessing (either reading or writing) a previously untouched memory word (i.e. any offset within a word). At the time of expansion, the cost in gas must be paid. Memory is more costly the larger it grows (it scales quadratically).

(To add to this: it scales linearly for the first 724 bytes [Appendix H, Yellow Paper] and quadratically after that.)

  • So, when the function starts calling, the first 32 bytes will be stored in the first slot. since the function started calling just now, it's the first time it's going to put some bytes in the first slot. Does this mean that this is the expansion cost ? – Nika Kurashvili Jan 20 at 14:08
  • Richard, I asked a better question. can you help ? ethereum.stackexchange.com/questions/92553/… – Nika Kurashvili Jan 20 at 17:21

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.