3

Strangely, the execution cost of a function x() seems to sometimes increase with contract size (ie adding code to unrelated parts of the contract). To be clear, when adding the extra code to the contract, it does not affect the x() function. Is this supposed to happen? Why does it happen?

Unfortunately, I noticed this some time ago and don't have an example. I've seen others ask about it too.

2 Answers 2

1

If your function x() increases in complexity and if the function also makes changes to the state of the contract, such as updating values in state variables, then this increases the cost of gas. One way to help reduce the cost of execution is to try and store values in memory when possible.

1
  • Good tip about the memory. I'm wondering about the case where the function is not affected by the extra code at all. I've updated the question.
    – bevanb
    Commented Oct 30, 2017 at 19:14
0

This kind of strange results are caused by different optimizations performed by the compiler. I'm really not an expert in compiler optimization but the idea is to pack similar functionality into chunks to reduce the size.

In some cases adding unrelated things to the contract breaks optimization of the existing code and the earlier code is not so efficient anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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