'Clean' Code, Horrible Performance — Casey Muratori
“Casey demonstrates with real benchmarks how textbook OOP principles like excessive polymorphism, class hierarchies, and virtual functions cause a 15x slowdown on modern hardware.”
Executive Summary: Hardware Realities vs Dogmatic Clean Code
In “'Clean' Code, Horrible Performance” (2023), veteran game engine architect Casey Muratori presents an empirical deconstruction of dogmatic Object-Oriented Programming (OOP) rules popularized by Robert C. Martin (“Uncle Bob”) in the book Clean Code. Muratori shows that classic clean code rules—such as strictly avoiding switch statements, replacing branching with class polymorphism and virtual methods, enforcing microscopic single-purpose functions, and hiding data layouts behind getters and setters—cause catastrophic hardware performance degradation. By running exact assembly-level cycle benchmarks computing shape areas on modern x86/ARM CPUs, Muratori demonstrates that dogmatic clean code runs 15x to 25x slower than simple, straightforward procedural code written with Data-Oriented Design (DOD). The lecture serves as a watershed wake-up call for software engineers who prioritize abstract design aesthetics over CPU caching, instruction pipelining, and memory bandwidth reality.
Key Mental Models Derived from this Lecture:
- Hardware Reality over Dogmatic Rules: Modern CPUs are not abstract Turing machines; they depend critically on L1/L2 cache locality, branch prediction, and SIMD vector execution.
- The Vtable Indirection Tax: Virtual method calls force pointer indirection through a vtable, preventing compiler inlining, thrashing branch target buffers, and generating cache misses.
- Data-Oriented Design (DOD): Organize data structures around how hardware memory controllers fetch cache lines (64 bytes), preferring Structure of Arrays (SoA) over deeply nested OOP hierarchies.
- The “Switch Statements Are Evil” Fallacy: Modern compilers optimize dense integer switch statements into single indirect jump tables or vectorized lookups, dramatically outperforming polymorphic dynamic dispatch.
- Microscopic Functions Destroy Inlining: Splitting code into 3-line functions creates unnecessary stack frame overhead and obscures loop optimization opportunities from LLVM/GCC.
- Performance is a Feature, Not an Afterthought: Software that is 20x slower squanders decades of hardware advances, drives up cloud infrastructure costs, and harms end-user battery life.
Key Timestamps: Clean Code Horrible Performance Benchmarks
The Core Question: What is the Hardware Cost of Clean Code?
The Benchmark Setup: Computing Bounding Areas of 10,000,000 Shapes
Polymorphism & Virtual Function Tables: Tracing Assembly Overhead
Data-Oriented Refactoring: Eliminating Vtables and Flattening Structs
SIMD Vectorization & AVX2: Achieving a 25x Performance Speedup
The Engineering Moral: Writing Readable Code Without Sabotaging Hardware
Mathematical Formulations: Instruction Counts and CPU Cache Latency
CPU Memory Hierarchy Latency Disparity
Accessing main memory DRAM is nearly 100 times slower than reading from the L1 cache. Deeply nested OOP class hierarchies with heap-allocated pointers force random DRAM lookups, stalling CPU execution pipeline cycles.
# Comparing OOP Pointer Chasing vs Contiguous Cache Lines (64 bytes)
# Clean Code OOP (Pointers to separate heap allocations):
# [Ptr] -> Heap Shape Object -> [vptr] -> Vtable -> Function Pointer -> DRAM Miss!
# Data-Oriented (Contiguous cache-line packed struct):
struct Shape {
float width, height;
uint32_t type; // Fits in 12 bytes; 5 shapes per single 64-byte L1 cache line!
};Dynamic Dispatch vs Jump Table Instruction Complexity
A virtual method call requires reading the object's vptr, dereferencing the function table offset, and performing an indirect branch. A contiguous switch statement requires a single indexed table jump costing ~2 cycles.
// Disassembly of Clean Code Virtual Call: mov rax, QWORD PTR [rdi] // Load vtable pointer call QWORD PTR [rax+16] // Indirect branch (cannot inline, branch hazard) // Disassembly of Data-Oriented Table Switch: mov eax, DWORD PTR [rdi+8] // Load shape type enum jmp [rax*8 + jump_table] // Direct predicted jump (inlinable)
SIMD Vectorized Throughput Scaling Law
Contiguous memory layouts allow x86 AVX2 instructions to calculate areas for 8 floating-point shapes in parallel every single clock cycle. Polymorphic OOP classes scatter data across memory, making SIMD vectorization impossible.
// AVX2 SIMD computation for 8 rectangles simultaneously: __m256 widths = _mm256_loadu_ps(&rect_widths[i]); __m256 heights = _mm256_loadu_ps(&rect_heights[i]); __m256 areas = _mm256_mul_ps(widths, heights); // 8 shape areas computed in 1 cycle!
Community Consensus: Hacker News Debates on Casey Muratori and Uncle Bob
Casey Muratori vs Uncle Bob: Is “Clean Code” Actively Harmful?
“Casey showed what game programmers knew for 20 years: Clean Code was written by people who never looked at a profiler or compiler disassembly.” — @render_pass
Does 20x Performance Matter for Cloud Backends and Web Apps?
“When everyone says 'hardware is cheap, programmer time is expensive', software becomes 100x slower until your simple Electron chat app eats 4GB of RAM.” — @serverless_skeptic
Is Data-Oriented Design Mutually Exclusive with Clean Architecture?
“Procedural code with plain structs and switch statements isn’t 'dirty code'. It’s honest code that respects the hardware it runs on.” — @mike_acton_fan
Technical FAQ: Data-Oriented Design, Vtables, CPU Pipelines and C++
In Simple Made Easy, what is the fundamental difference between Simple and Easy?
In the Simple Made Easy lecture by Rich Hickey, Simple is an objective design property (simplex: having a single role, unbraided), whereas Easy is subjective and relative to developer familiarity (facilis: near at hand).
What does the Simple Made Easy lecture mean by the verb “to complect”?
In Simple Made Easy, to complect means to braid, intertwine, or fold multiple distinct concerns together (from Latin complecti). Once concepts are complected, you cannot reason about or test one without dragging the other along.
Why does Simple Made Easy advocate for immutable values over mutable state?
Simple Made Easy proves that mutable state braids value and time together. Immutable values represent permanent historical facts that can be freely shared across concurrent threads without locks, cached indefinitely, and trivially reasoned about.
How does the Epochal Time Model in Simple Made Easy solve concurrency?
In Simple Made Easy, identity is an atomic reference pointing to an immutable value snapshot at time t. Updates apply a pure function to produce a new immutable value at t+1 and swap the reference atomically (e.g. CAS), allowing lock-free reads.
Why does Simple Made Easy recommend passing plain data maps instead of OOP classes?
In Simple Made Easy, plain associative maps represent information directly without braiding data representation with domain logic or class inheritance hierarchies, enabling universal composition.
What architectural constructs does Simple Made Easy recommend for decoupled systems?
Simple Made Easy recommends five foundational constructs: persistent immutable data structures, declarative data (like JSON or EDN), asynchronous queues to decouple space and time, pure functions, and runtime schemas.
