EVM
The EVM (Ethereum Virtual Machine) is a stack-based, 256-bit-word VM. Solidity compiles to EVM bytecode. Every Ethereum node runs every transaction deterministically — that’s how consensus works.
The three storage areas + gas mental model
EXAMPLE
// Storage (persistent, very expensive — ~20,000 gas per slot write)
contract Bank {
mapping(address => uint256) private balances; // lives in storage
}
// Memory (per-call, cheap — bytes32 / arrays you allocate)
function sum(uint256[] memory xs) external pure returns (uint256 t) {
for (uint256 i; i < xs.length; ++i) t += xs[i];
}
// Calldata (input bytes, read-only, cheapest)
function echo(bytes calldata data) external pure returns (bytes memory) {
return data;
}
// Cheap-to-expensive (rough)
// calldata read < memory ops < storage read < storage write
// non-zero storage slot: ~20,000 gas (first write)
// subsequent updates: ~5,000
// zero-to-zero: free (refund)
// Tips that save real money
// - Pack storage slots: two uint128 fit in one slot
// - Use 'unchecked { ++i; }' in for loops where overflow is impossible
// - Emit events instead of storing log-like data
// - Pass arrays as 'calldata' when they're read-only inputs
Why it matters
Every public on-chain function is an auction: users bid gas to get included. Every storage slot you avoid is real money saved by every caller, every call, forever.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
// EVM = Ethereum Virtual Machine. Stack-based, 256-bit words. // Solidity compiles to EVM bytecode. // Storage is expensive; memory is cheap and per-call.Try it Yourself »
Discussion
Loading…