Chapter 3

Data Is Just Data

A class wraps your data in machinery. A dict hands it to you.
Demo: Process heap layout: object graph vs flat arrays
☠ Object graph: 6 heap allocations
0x7f00
0x7f20
0x7f40
0x7f60
0x7f80
0x7fA0
0x7fC0
0x7fE0
classOrder
classCustomer
classLineItem[]
classAddress
classTaxRegion
classRateTable
L1 cache line (64B)
Allocations: 0
Pointer chases: 0
✦ Flat arrays: 3 heap allocations
0x7f00
0x7f20
0x7f40
0x7f60
0x7f80
0x7fA0
0x7fC0
0x7fE0
arraycustomers
name, email, address, tax_region
arrayorders
total, discount, tax, grand_total
arrayline_items
sku, qty, price
fields stored together
Allocations: 0
Pointer chases: 0
0
Heap allocations
0
Heap allocations
0
Pointer chases
0
Pointer chases

What you just saw

The left panel is a process heap after constructing an Order object graph. The Order points to a Customer, which points to an Address, which points to a TaxRegion, which points to a RateTable. The LineItem array hangs off the Order separately. Six objects, six heap allocations, scattered across the address space by the allocator. To compute the order total, the CPU must chase five pointers. Each pointer chase is a place where the CPU may have to wait for memory, and how long it waits depends on the machine and on what the cache happens to hold.

The right panel is the same data as three flat arrays: one for customers, one for orders and one for line items. Three allocations. Each array is contiguous in memory, and a field is found by its position, so there are no pointer chases. This is the layout the book recommends in place of an object graph. A typed array, a struct or a record fits the picture exactly. A plain Python list or dict still keeps its values as separate objects, so for those the picture is a close sketch.

The class version needs six allocations and five pointer chases to reach its data. The flat version needs three allocations and none. The demo counts those facts, and it makes no claim about how many nanoseconds they cost on your machine.

How this works: This demo visualizes the execution difference between dishonest and honest code. This demo counts heap allocations and pointer chases, which are facts about the architecture, not nanosecond measurements. Object sizes and cache behavior are left out because they differ by language and machine. Instrumented source code, Dockerfile, and raw trace data: github.com/adamzwasserman/honest-code-traces
← Ch.2: Classes Considered Harmful Ch.4: Pure Functions →