Gas · Intermediate
Smart contract gas optimization: 15 techniques that actually move the number
Concrete Solidity gas optimisation techniques ranked by impact: storage layout, calldata, custom errors, loops, events, and the measurement habit that makes them stick. Includes Monad-specific notes.
By Adwait Keshari, founder of Monsmith8 min read
Gas optimisation has a bad reputation because much of the advice is micro-level and stale. The techniques below are ordered by how much they typically matter. The first five are worth more than the rest combined.
Storage (the big one)
- Write storage as rarely as possible. An SSTORE from zero to non-zero is the most expensive ordinary operation in the EVM. Cache in memory, write once.
- Pack variables. Two
uint128in one slot cost one SSTORE. But on Monad, note that packing concentrates writes on one slot, which can serialise otherwise independent transactions. - Use
mappingover arrays when you do not need iteration. Array length updates are extra writes. - Delete storage you no longer need; refunds are smaller than they were but not zero.
- Read storage once into a local variable inside loops rather than on every iteration.
Calldata and data types
- Use
calldatafor external function array and string parameters;memorycopies. - Use custom errors instead of revert strings. Smaller bytecode and cheaper reverts.
- Do not shrink integer types below 256 bits for local variables; the EVM works in 256-bit words and shrinking adds masking. Shrink only for storage packing.
Control flow
- Cache array length outside loops. Use
unchecked { ++i; }where overflow is impossible. - Short-circuit conditions with the cheapest check first.
- Avoid unbounded loops entirely; they are a gas problem and a denial-of-service problem.
Events and external calls
- Emit events instead of storing data that only off-chain readers need. An event is far cheaper than a storage write.
- Batch external calls where the callee supports it.
- Use
immutableandconstantfor values set once; they live in bytecode, not storage.
Deployment
- Turn the optimizer on and tune runs to how often the contract will be called: low runs for cheap deployment, high runs for cheap calls.
Monad-specific notes
- Fees are billed on the gas limit. Tight estimation is a gas optimisation on Monad.
- The 128 KB contract limit means you can often skip the library-splitting that costs extra DELEGATECALLs on Ethereum.
- Storage packing trades gas for parallelism. Profile before you pack hot-path variables together.
Measure, do not guess
Chain Fit at monsmith.com/compare runs your contract in a real EVM and reports gas per function, then prices it on each chain with live gas prices. Optimise against those numbers, and re-run after every change.
Frequently asked questions
- What uses the most gas in a smart contract?
- Storage writes (SSTORE), followed by external calls and contract deployment. Reducing storage writes is the highest-impact optimisation.
- Do gas optimisations matter on Monad?
- Less in dollar terms because fees are low, but Monad bills the gas limit, so accurate estimates matter more, and storage layout affects parallel throughput.
Try it in Monsmith
Free, in the browser, no account. Compile, audit, profile, and deploy to Monad.
Open the studio