> cat ./linkedin_post.md
[terminal_mode]EVM Deep Dive: Understanding the Ethereum Virtual Machine Architecture
DATE:2018-06-15|FORMAT:linkedin_post
Nelson Lourenço
Senior Software Engineer | AI & Web Development
2018-06-15•🌐
Ethereum Virtual Machine: A Deep Technical Dive
The Ethereum Virtual Machine (EVM) is the computational engine that powers Ethereum and all EVM-compatible chains. Understanding its internals is essential for writing efficient, secure smart contracts.
EVM Architecture Overview
┌─────────────────────────────────────────────────────────────────────┐
│ EVM ARCHITECTURE LAYERS │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ EVM RUNTIME ENVIRONMENT │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────┐ │ │
│ │ │ EXECUTION CONTEXT │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │
│ │ │ │ Program │ │ Stack │ │ Memory │ │ │ │
│ │ │ │ Counter │ │ (1024) │ │ (Word- │ │ │ │
│ │ │ │ (PC) │ │ items │ │ aligned)│ │ │ │
│ │ │ └─────────┘ └─────────┘ └─────────┘ │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │
│ │ │ │ Storage │ │ Calldata│ │ Return │ │ │ │
│ │ │ │ (Key- │ │ (bytes)│ │ Data │ │ │ │
│ │ │ │ Value) │ │ │ │ │ │ │ │
│ │ │ └─────────┘ └─────────┘ └─────────┘ │ │ │
│ │ │ │ │ │
│ │ └─────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ ├─────────────────────────────────────────────────────────────┤ │
│ │ GAS ACCOUNTING │ │
│ │ • Tracks computational resources consumed │ │
│ │ • Prevents infinite loops and DOS attacks │ │
│ │ • Refunds for storage cleanup │ │
│ │ │ │
│ ├─────────────────────────────────────────────────────────────┤ │
│ │ OPCODE EXECUTION │ │
│ │ • 150+ distinct opcodes │ │
│ │ • Stack-based execution model │ │
│ │ • Deterministic behavior │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
Memory vs Storage vs Calldata
┌─────────────────────────────────────────────────────────────────────┐
│ DATA LOCATION COMPARISON │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ STORAGE (Persistent) │ │
│ │ │ │
│ │ • Key-value mapping (256-bit → 256-bit) │ │
│ │ • Persistent across transactions │ │
│ │ • Most expensive: ~20,000 gas for new writes │ │
│ │ • Remains forever on-chain │ │
│ │ • SLOAD/SSTORE opcodes │ │
│ │ │ │
│ │ struct User { │ │
│ │ uint256 balance; // Storage slot 0 │ │
│ │ uint256 credit; // Storage slot 1 │ │
│ │ address wallet; // Storage slot 2 │ │
│ │ } │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ MEMORY (Ephemeral) │ │
│ │ │ │
│ │ • Linear, byte-addressable │ │
│ │ • Expands in 32-byte words │ │
│ │ • Expensive to expand (quadratic expansion) │ │
│ │ • Wiped between transactions (not between calls) │ │
│ │ • MLOAD/MSTORE opcodes │ │
│ │ │ │
│ │ function processData(bytes memory data) internal { │ │
│ │ // Memory: expensive to expand │ │
│ │ // Avoid reading/writing multiple times │ │
│ │ } │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ CALLDATA (External Input) │ │
│ │ │ │
│ │ • Read-only, immutable │ │
│ │ • Free to read (no gas cost) │ │
│ │ • Cannot be modified │ │
│ │ • Cheaper than memory for external data │ │
│ │ • CALLDATALOAD/CALLDATASIZE/CALLDATACOPY │ │
│ │ │ │
│ │ function setData(string calldata data) external { │ │
│ │ // Calldata: cheapest for read-only access │ │
│ │ // Cannot modify, only read │ │
│ │ } │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ RETURN DATA (Output Buffer) │ │
│ │ │ │
│ │ • Write-only buffer for external calls │ │
│ │ • Stores data returned from external contracts │ │
│ │ • RETURNDATALOAD/RETURNDATACOPY/RETURNDATASIZE │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
Gas Mechanics
┌─────────────────────────────────────────────────────────────────────┐
│ GAS COST STRUCTURE │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ GAS COSTS BY OPERATION: │
│ ┌────────────────────────┬───────────────────────────────────┐ │
│ │ OPCODE │ GAS COST │ │
│ ├────────────────────────┼───────────────────────────────────┤ │
│ │ Zero-time opcodes │ 0 gas │ │
│ │ (STOP, RETURN, REVERT)│ │ │
│ ├────────────────────────┼───────────────────────────────────┤ │
│ │ Base operations │ 2 gas │ │
│ │ (ADD, SUB, AND, OR) │ │ │
│ ├────────────────────────┼───────────────────────────────────┤ │
│ │ Memory expansion │ 3 gas per 32-byte word │ │
│ │ (MLOAD, MSTORE) │ │ │
│ ├────────────────────────┼───────────────────────────────────┤ │
│ │ Storage operations │ SLOAD: 2,100 gas │ │
│ │ │ SSTORE: 20,000/5,000 gas │ │
│ ├────────────────────────┼───────────────────────────────────┤ │
│ │ External calls │ ~7,000 + execution gas │ │
│ │ │ (CALL, DELEGATECALL) │ │
│ ├────────────────────────┼───────────────────────────────────┤ │
│ │ Contract creation │ CREATE: 32,000 gas │ │
│ │ │ CREATE2: 32,000 + salt hash │ │
│ ├────────────────────────┼───────────────────────────────────┤ │
│ │ Log operations │ LOG0-4: 375-3,000 gas │ │
│ └────────────────────────┴───────────────────────────────────┘ │
│ │
│ EIP-1559 GAS PRICING: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Total Gas = Base Fee + Priority Fee (tip) │ │
│ │ │ │
│ │ • Base Fee: Burned, adjusts based on network congestion │ │
│ │ • Priority Fee: Goes to validator, optional │ │
│ │ • Max Fee: User sets max willing to pay │ │
│ │ │ │
│ │ Formula: │ │
│ │ GasUsed × (BaseFee + PriorityFee) = Transaction Fee │ │
│ │ │ │
│ │ Example: │ │
│ │ • Base Fee: 50 Gwei │ │
│ │ • Priority Fee: 2 Gwei │ │
│ │ • Gas Used: 50,000 │ │
│ │ • Total: 50,000 × 52 = 2.6 ETH │ │
│ │ • Burned: 50,000 × 50 = 2.5 ETH │ │
│ │ • Validator Tip: 50,000 × 2 = 0.1 ETH │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ GAS REFUNDS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Storage Refund: │ │
│ │ • Setting storage to zero: +15,000 gas refund │ │
│ │ • Applied at end of transaction │ │
│ │ • Max refund = 50% of gas spent │ │
│ │ │ │
│ │ SELFDESTRUCT Refund: │ │
│ │ • Destroying contract: +24,000 gas refund │ │
│ │ • SELFDESTRUCT now deprecated (no more refund) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
Opcodes Reference
┌─────────────────────────────────────────────────────────────────────┐
│ KEY EVM OPCODES │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ STACK OPERATIONS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ PUSH1-PUSH32 │ Push 1-32 bytes onto stack │ │
│ │ POP │ Remove top stack item │ │
│ │ DUP1-DUP16 │ Duplicate top Nth stack item │ │
│ │ SWAP1-SWAP16 │ Swap top with Nth stack item │ │
│ │ LOG0-LOG4 │ Emit events with 0-4 topics │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ ARITHMETIC OPERATIONS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ ADD/SUB │ Addition/Subtraction (2 gas) │ │
│ │ MUL/DIV │ Multiplication/Division (5 gas) │ │
│ │ MOD │ Modulo operation │ │
│ │ ADDMOD/MULMOD│ Modulo with intermediate addition │ │
│ │ EXP │ Exponentiation (dynamic gas) │ │
│ │ SIGNEXTEND │ Extend signed integer representation │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ COMPARISON OPERATIONS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ LT/GT │ Less-than / Greater-than (3 gas) │ │
│ │ SLT/SGT │ Signed less-than / greater-than │ │
│ │ EQ │ Equality check (3 gas) │ │
│ │ ISZERO │ Logical negation │ │
│ │ AND/OR/XOR │ Bitwise operations (3 gas) │ │
│ │ NOT │ Bitwise NOT │ │
│ │ SHL/SHR/SAR │ Bitwise shifts (3 gas) │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ ENVIRONMENTAL INFORMATION: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ ADDRESS │ Get current contract address │ │
│ │ BALANCE │ Get balance of an address │ │
│ │ ORIGIN │ Get transaction origin address │ │
│ │ CALLER │ Get message sender (tx.origin) │ │
│ │ CALLVALUE │ Get ETH sent with call │ │
│ │ CALLDATALOAD │ Get calldata word at offset │ │
│ │ CALLDATASIZE │ Get calldata size │ │
│ │ CODESIZE │ Get current contract code size │ │
│ │ GASPRICE │ Get gas price (EIP-1559 base fee) │ │
│ │ GASLIMIT │ Get block gas limit │ │
│ │ TIMESTAMP │ Get block timestamp │ │
│ │ NUMBER │ Get block number │ │
│ │ DIFFICULTY │ Get block difficulty │ │
│ │ PREVRANDAO │ Get previous block RANDAO value │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ STORAGE OPERATIONS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ SLOAD │ Load from storage (2,100 gas) │ │
│ │ SSTORE │ Store to storage (20k/5k gas) │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ CONTROL FLOW: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ JUMP │ Unconditional jump to destination │ │
│ │ JUMPI │ Conditional jump (2nd item is condition) │ │
│ │ JUMPDEST │ Mark valid jump destination (1 gas) │ │
│ │ STOP │ Halt execution (0 gas) │ │
│ │ RETURN │ Halt and return data │ │
│ │ REVERT │ Halt with error (no state change) │ │
│ │ INVALID │ Invalid instruction (halts) │ │
│ │ SELFDESTRUCT │ Destroy contract (deprecated) │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ CALL OPERATIONS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ CALL │ Call another contract │ │
│ │ DELEGATECALL │ Call with same context (no msg.value) │ │
│ │ STATICCALL │ Read-only call (no state modification) │ │
│ │ CALLCODE │ Legacy delegatecall (deprecated) │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
Bytecode Optimization
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// UNOPTIMIZED VERSION
contract Unoptimized {
uint256 public value;
function setValue(uint256 _value) external {
value = _value; // SLOAD + SSTORE = 22,100 gas
}
function getValue() external view returns (uint256) {
return value; // SLOAD = 2,100 gas
}
function double() external {
value = value * 2; // SLOAD + MUL + SSTORE = 27,100+ gas
}
}
// OPTIMIZED VERSION
contract Optimized {
uint256 public value;
// Cache storage reads in memory
function setValue(uint256 _value) external {
value = _value; // Still SLOAD + SSTORE
}
// Use calldata for external function parameters
function setMultiple(uint256[] calldata values) external {
for (uint256 i = 0; i < values.length; i++) {
// Cache loop variables
if (values[i] > value) {
value = values[i];
}
}
}
// Use custom errors instead of strings
error Unauthorized();
function checkAccess(address caller) external view {
if (caller != msg.sender) {
revert Unauthorized(); // 4 bytes vs ~50+ bytes
}
}
}
// EXTREMELY OPTIMIZED - Manual inline assembly
contract AssemblyOptimized {
uint256 public value;
// Manual assembly for maximum gas efficiency
function setValueAssembly(uint256 _value) external {
assembly {
// Directly store to slot 0
sstore(0, _value)
}
}
function getValueAssembly() external view returns (uint256) {
assembly {
// Directly load from slot 0
let val := sload(0)
return(0x20, 0x20)
}
}
// Unchecked math where safety is guaranteed
function increment(uint256 i) external returns (uint256) {
unchecked {
return i + 1; // No overflow check needed
}
}
}
EIP Standards Overview
┌─────────────────────────────────────────────────────────────────────┐
│ KEY EIP STANDARDS │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ TOKEN STANDARDS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ ERC-20: Fungible Token Standard │ │
│ │ • balanceOf, transfer, approve, transferFrom │ │
│ │ • Events: Transfer, Approval │ │
│ │ • Most adopted token standard (~500K+ contracts) │ │
│ │ │ │
│ │ ERC-721: Non-Fungible Token (NFT) │ │
│ │ • balanceOf, ownerOf, transferFrom, approve │ │
│ │ • Events: Transfer, Approval, ApprovalForAll │ │
│ │ • Unique tokens with individual ownership │ │
│ │ │ │
│ │ ERC-1155: Multi-Token Standard │ │
│ │ • Single contract for multiple token types │ │
│ │ • Batch transfers and balance queries │ │
│ │ • Gas efficient for gaming/metaverse │ │
│ │ │ │
│ │ ERC-4626: Vault Standard │ │
│ │ • Yield-bearing vault abstraction │ │
│ │ • Standardized interface for lending protocols │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ INTERFACE STANDARDS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ ERC-165: Standard Interface Detection │ │
│ │ • Support function to check interface implementation │ │
│ │ • Used by marketplaces and aggregators │ │
│ │ │ │
│ │ ERC-1820: Pseudo-introspection Registry │ │
│ │ • Contract type registry │ │
│ │ • Used by ERC-777 and ERC-1155 │ │
│ │ │ │
│ │ ERC-2612: Permit for ERC-20 │ │
│ │ • gasless approvals via off-chain signatures │ │
│ │ • Used by Uniswap, Compound │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ SCALING & INFRASTRUCTURE: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ ERC-4337: Account Abstraction (No longer EIP) │ │
│ │ • UserOperations mempool separate from consensus │ │
│ │ • Pay gas with ERC20 tokens, social recovery │ │
│ │ • EntryPoint contract handles execution │ │
│ │ │ │
│ │ ERC-4844: Proto-Danksharding │ │
│ │ • Blob-carrying transactions for Layer 2 data │ │
│ │ • 128 blobs per block (~1MB per block) │ │
│ │ • KZG polynomial commitments │ │
│ │ │ │
│ │ ERC-2535: Diamond Standard │ │
│ │ • Modular proxy pattern for large contracts │ │
│ │ • Multiple facets, each handling specific functions │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
EVM-Compatible Chains
┌─────────────────────────────────────────────────────────────────────┐
│ EVM COMPATIBLE ECOSYSTEM │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ MAIN EVM CHAINS: │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Chain │ Chain ID │ Native Token │ Consensus │ │
│ ├─────────────────┼──────────┼──────────────┼────────────────┤ │
│ │ Ethereum Mainnet│ 1 │ ETH │ PoS (Casper) │ │
│ │ Ethereum Classic │ 61 │ ETC │ PoW │ │
│ │ Binance Smart │ 56 │ BNB │ PoSA │ │
│ │ Polygon │ 137 │ MATIC │ PoS │ │
│ │ Arbitrum │ 42161/70 │ ETH │ Rollup (L2) │ │
│ │ Optimism │ 10 │ ETH │ Rollup (L2) │ │
│ │ Avalanche │ 43114 │ AVAX │ PoS │ │
│ │ Fantom │ 250 │ FTM │ aBFT (Lachesis)│ │
│ │ Celo │ 42220 │ CELO │ PoS │ │
│ │ Moonbeam │ 1284 │ GLMR │ Parachain │ │
│ │ Base │ 8453 │ ETH │ OP Stack (L2) │ │
│ └─────────────────┴──────────┴──────────────┴────────────────┘ │
│ │
│ WHY EVM COMPATIBILITY? │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Benefits: │ │
│ │ • Solidity tooling works out of the box │ │
│ │ • Existing smart contracts can be ported │ │
│ │ • Wallet and frontend infrastructure shared │ │
│ │ • Developers don't need to learn new language │ │
│ │ • Bridge existing DeFi liquidity │ │
│ │ │ │
│ │ Trade-offs: │ │
│ │ • Same execution bottlenecks as Ethereum │ │
│ │ • EVM semantics limit innovation │ │
│ │ • Cannot escape EVM's 256-bit architecture │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
What EVM optimization techniques has your team implemented? Share your experience!
#EVM #Ethereum #Solidity #Blockchain #SmartContracts #GasOptimization
How are you leveraging EVM-compatible chains for your projects?
👍584💬68🔁5