Contract size limit (EIP-170)
Ethereum caps deployed bytecode at 24,576 bytes, so a contract that compiles but exceeds it cannot be deployed — measure early and shrink deliberately.
Procedure
- Measure:
forge build --sizeslists runtime size per contract; anything over 24,576 fails. - Identify the offenders and their margin:
forge build --sizes | awk '$2 > 20000'
- Enable the optimiser with runs tuned for size:
optimizer_runs = 200(lower = smaller) infoundry.toml, thenvia_ir = truefor deeper optimisation. - Convert
require(cond, "long string")to custom errors — each string costs ~32 bytes:
error Unauthorized();
...
if (msg.sender != owner) revert Unauthorized();
- Extract logic into a library with
external(notinternal) functions so its code lives separately, or split into facets/modules. - Remove unused imports, generated getters for
publicstate (useprivate+ an explicit getter only where needed), and duplicate constant strings. - Turn on the compiler's
revertStrings = "strip"only for interfaces where revert data is not needed. - Consider a linked library (
externallibrary → delegatecall) or a minimal-proxy factory for many identical instances. - Rebuild and diff:
forge build --sizes | grep MyContractbefore and after each change. - Verify the final deployed bytecode size on-chain after deploy:
cast code $ADDR --rpc-url $RPC | wc -c.
Pitfalls
- Optimiser
runsat 1,000,000 minimises gas at the cost of size — the default 200 is usually the right trade for a big contract. via_ir = truecan increase size for small contracts; measure rather than assume.- Using
--via-irlocally but not in the deploy config, so CI passes and the deploy script fails. - Splitting into a library but marking functions
internal— the code is then inlined back into every caller and nothing shrinks. - Forgetting initcode: constructor + runtime is capped at 49,152 bytes (EIP-3860), separate from the runtime limit.
Verification
forge build --sizes | awk '$2 > 24576 {print "OVER", $0}'
Pass: empty output (no contract over 24,576). Confirm the deployed instance: cast code $ADDR --rpc-url $RPC | wc -c is 2 * size hex characters.
Report each contract's size, the techniques applied, and the final number.