When writing big contracts, I am asked to either reduce the size or use the optimization.

What is it ?

What is the numerical value for ?

What happens if I set the optimization to 0, would I still be able to use the contract ?

How do I know which value to use ?

1 Answer 1


Optimization is the property of the solidity compiler (solc) and not the remix.

From solidity docs:

Before you deploy your contract, activate the optimizer when compiling using solc --optimize --bin sourceFile.sol. By default, the optimizer will optimize the contract assuming it is called 200 times across its lifetime (more specifically, it assumes each opcode is executed around 200 times). If you want the initial contract deployment to be cheaper and the later function executions to be more expensive, set it to --optimize-runs=1. If you expect many transactions and do not care for higher deployment cost and output size, set --optimize-runs to a high number. This parameter has effects on the following (this might change in the future):

  • the size of the binary search in the function dispatch routine
  • the way constants like large numbers or strings are stored

from solidity's blog :

  • How is optimization affected by the number of runs (--optimize-runs) and is there a maximum number above which it stops mattering, or is --optimize-runs=20000 less efficient than --optimize-runs=500000?
  • The parameter specifies roughly how often each opcode of the deployed code will be executed across the lifetime of the contract. A “runs” parameter of “1” will produce short but expensive code. The largest value is 2**64-1.

solc compiler will try to maximize gas cost savings by taking into consideration how many times the bytecode is expected to run (lifetime) and the cost of introducing an optimization at compile time.

For example, the solc compiler finds a cheaper execution path by adding a temporary variable costing some gas while deploying, if total amortized gas savings during the lifetime of the contract(i.e runs parameter) is greater than the cost of adding that temporary variable then the compiler will make the optimization.

Generally, after a certain high value of optimizer runs parameter, bytecode gets constant, and solc does not further produce more optimized bytecode.

So for a scenario when a contract is assumed to running for infinite times theoretically, after a certain amount of optimizer runs(a.k.a lifetime for the compiler to produce optimized results), there will be no possible optimization. This does not stop the functioning of the contract.

from a blog post on optimazation

runs is not how many times the optimizer will run but how many times you expect to call functions in that smart contract. If the smart contract is only of one-time use as a smart contract for vesting or locking of tokens, you can set the runs value to 1 so that the compiler will produce the smallest possible bytecode but it may cost slightly more gas to call the function(s). If you are deploying a contract that will be used a lot (like an ERC20 token), you should set the runs to a high number like 1337 so that initial bytecode will be slightly larger but calls made to that contract will be cheaper. Commonly used functions like transfer will be cheaper.

So, more runs will increase the overall compiled bytecode size costing more gas while deploying but cheaper calls. The ideal no. of optimization depends upon what contract is being used for (as covered above), you can use a trial and error approach with testing with different amounts of runs each time until you find the correct balance between initial cost vs future cost.

Other resources on the optimizer might help compare optimized vs non-optimized code :

Behaviour when optimization is disabled

Any reason not to use Browser Solidity's “Enable Optimization”?

'Runs (Optimizer) ' and 'Optimization' while verifying source code on Etherscan

  • "lifetime of contract" ? I need my contract has an eternal life .... Commented Apr 26, 2022 at 15:28

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.