The Physics of Execution: EVM & Opcodes
Why the Ethereum Virtual Machine is 100x slower than Native Code. The physics of Stack Machines, Gas Metering, and I/O Bottlenecks.
🎯 What You'll Learn
- Deconstruct the EVM as a Stack-Based Machine
- Analyze Opcode Gas Cost (Why `SSTORE` is 20,000 gas)
- Trace a Transaction Execution Step-by-Step
- Calculate the throughput limit of Sequential Execution
- Audit the overhead of JIT Compilation vs Interpreter
📚 Prerequisites
Before this lesson, you should understand:
Introduction
The EVM is Turing Complete, but it is Resource Constrained. It is designed for Determinism, not Speed. It sacrifices raw performance to ensure that a 50,000 server in Tokyo.
This lesson explores the physics of the World Computer and why it is, by design, slow.
Pro Version: See the full research: Blockchain Node Execution
The Physics: Stack Machines (EVM)
Your CPU (x86/ARM) is a Register Machine. It operates on fast, internal registers (rax, rbx).
The EVM is a Stack Machine. It has no registers.
It operates on a single Stack of 256-bit words.
The Physics: To add two numbers:
PUSH1 0x05(Stack: [5])PUSH1 0x03(Stack: [3, 5])ADD(Popper 3 and 5, Pushes 8. Stack: [8])
Stack machines are easier to implement (simple VM), but slower because every operation requires memory access (Push/Pop) rather than register access.
Pro Version: See the full research: Blockchain Node Execution
Deep Dive: Gas Physics (Opcode Cost)
Why does ADD cost 3 gas, but SSTORE costs 20,000 gas?
Physics relative to Real World Resources.
ADD: CPU Cycle (~0.3ns). Cost is negligible.SSTORE: Disk Write (~100 microseconds). Cost is massive.
Gas is not arbitrary money. Gas is a proxy for Time.
If SSTORE were cheap, an attacker could fill every node’s hard drive in 1 block, stopping the network. Gas limits enforce resource quotas.
Pro Version: See the full research: Blockchain Node Execution
Architecture: Sequential Execution
The EVM processes transactions Sequentially.
Tx1 -> Tx2 -> Tx3.
Even if Tx1 affects Account A and Tx2 affects Account B (unrelated), they cannot run in parallel.
Physics:
This is because State Root N depends on State Root N-1.
If you parallelize, you risk Non-Determinism (Race Conditions).
Modern chains (Solana, Aptos) use Parallel Execution (Block-STM) by pre-declaring memory access dependencies.
Pro Version: See the full research: Blockchain Node Execution
Code: Simulating the EVM Stack
class TinyEVM:
def __init__(self, code):
self.code = code
self.pc = 0
self.stack = []
def run(self):
while self.pc < len(self.code):
opcode = self.code[self.pc]
if opcode == "PUSH":
value = self.code[self.pc + 1]
self.stack.append(value)
self.pc += 2
elif opcode == "ADD":
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(a + b)
self.pc += 1
elif opcode == "STOP":
break
return self.stack[-1]
# Execution: 5 + 3
# Code: PUSH 5, PUSH 3, ADD, STOP
vm = TinyEVM(["PUSH", 5, "PUSH", 3, "ADD", "STOP"])
result = vm.run() # Returns 8
Pro Version: See the full research: Blockchain Node Execution
Practice Exercises
Exercise 1: The Infinite Loop (Beginner)
Scenario: Smart Contract has while(true) {}.
Result: It consumes all provided Gas. The transaction fails with “Out of Gas”. The miner keeps the fee. The node stops executing when Gas Remaining == 0.
Exercise 2: Storage Cost (Intermediate)
Scenario: You change a variable from 0 to 1.
Task: Calculate cost. SSTORE (Dirty Slot) = 20,000 gas.
Scenario 2: You change it back to 0.
Result: You get a Gas Refund (up to a limit) for freeing up storage space.
Exercise 3: Parallelism (Advanced)
Task: Why can’t Ethereum add threads? (Answer: Read/Write conflicts on the Global State Trie. If Tx1 reads Balance A, and Tx2 writes Balance A, the order matters absolutely).
Pro Version: See the full research: Blockchain Node Execution
Knowledge Check
- What is a Stack Machine?
- Why is
SSTOREthe most expensive opcode? - Why is the EVM single-threaded?
- What happens when a transaction runs out of gas?
- How does Parallel Execution (like Solana) work?
Answers
- LIFO. A VM that operates on a Last-In-First-Out data structure instead of registers.
- Disk I/O. Writing to physical storage is orders of magnitude slower than CPU math.
- Determinism. To ensure every node reaches the exact same state root after every block.
- Revert. State changes are undone, but the fee is paid to the miner for the work done so far.
- Access Lists. Transactions declare which accounts they touch beforehand, allowing non-overlapping txs to run on different cores.
Pro Version: See the full research: Blockchain Node Execution
Summary
- EVM: Stack-based, Slow, Deterministic.
- Gas: Metrics for Time & Storage.
- Execution: Sequential bottleneck.
Pro Version: See the full research: Blockchain Node Execution
Questions about this lesson? Working on related infrastructure?
Let's discuss