DEV Community

Cover image for Ethereum-Solidity Quiz Q7: What is the "solc optimizer" in Solidity?
MihaiHng
MihaiHng

Posted on

Ethereum-Solidity Quiz Q7: What is the "solc optimizer" in Solidity?

The solc optimizer is built in the Solidity compiler and is used to reduce gas costs by optimizing the compiled bytecode. It analyzes code and makes optimizations like removing dead code, simplifying expressions, and reorganizing operations to use less gas. Used across multiple tools, like Foundry, Hardhat, Truffle, Remix.

The optimizer has a parameter called optimizer runs that is configurable. This number represents roughly how many times you expect the contract to be called. Higher values[ex. 2000] optimize for lower runtime gas costs (good for contracts called frequently), while lower values[ex. 1-50] optimize for lower deployment costs.

For example, in Foundry's foundry.toml:

[profile.default]
optimizer = true
optimizer_runs = 2000
Enter fullscreen mode Exit fullscreen mode

Or in Hardhat's hardhat.config.js:

solidity: {
  version: "0.8.20",
  settings: {
    optimizer: {
      enabled: true,
      runs: 1
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)