openskills.info
Course Preview

EVM and Gas Optimization

The Ethereum Virtual Machine (EVM) is the virtual computer that every Ethereum node runs so that smart contracts execute the same way everywhere. Every instruction and every byte a contract stores has a price in units called gas, paid in ether, so gas optimization is the practice of designing, writing, and compiling contracts so that each call costs less without changing what the contract does.

itDistributed systems, messaging, and integration

Recommended first:smart-contract-development

Don't Panic: EVM and Gas Optimization

Somewhere, thousands of computers are running the same program at the same time and insisting on getting exactly the same answer. That program is the Ethereum Virtual Machine, and every smart contract call is a small, very expensive agreement between all of them.

The expense is measured in gas. Every instruction has a price on a public list. Adding two numbers costs 3 gas. Writing a brand new value into storage, the contract's permanent memory, costs 20,000 gas, plus 2,100 more if the transaction has not touched that slot yet. That second number is the one to remember. Most of the bill is decided by how many storage slots a call touches, not by how clever its arithmetic is.

The price list has a few quirks worth knowing before you open the course:

  • Cold and warm. The first time a transaction reads a slot it pays 2,100 gas. Every later read in the same transaction pays 100.
  • Packing. Small values declared next to each other can share one 32-byte slot, which turns three expensive writes into one. It can also make updating a single field cost the same as before, so pack fields that change together.
  • Refunds are capped. Clearing storage earns gas back, but never more than a fifth of what the transaction used.
  • Memory grows quadratically. Cheap for small buffers, ruinous for large ones.

The compiler does a lot of the work for you. Its optimizer runs setting trades cheaper deployment against cheaper calls, and its EVM version target decides which instructions your bytecode may use. Pick a target newer than the chain you deploy to, and deployment fails.

The honest part of the job is measurement. Record gas with a snapshot, change one thing, record it again. Keep the settings identical between runs, because a slot warmed up during test setup quietly hides the cold cost a real user pays.

One more thing: the price list moves. Upgrades have repriced storage, capped refunds, added cheaper instructions, and made data-heavy transactions dearer, and the next one is scheduled to change storage pricing again. So treat every gas number as belonging to a particular upgrade, and never write a gas amount into a contract as if it were a law of nature.

You now know enough to follow the rest of the course. Start with the storage section; that is where the money is.

Where this skill leads

Relevant careers

See how this topic contributes to broader role-level skill maps.

Sources