Course contentsShow
← Rust
Lesson 36 of 1,9101. Rust Fundamentals and ToolingFree lesson

Memory Layout and Data Alignment

Optimize data structures by controlling field ordering and alignment for cache efficiency.

In this lesson, you will learn how to organize your data structures to minimize wasted memory and improve CPU performance through strategic field ordering.

The Cost of Padding

When Rust lays out a struct in memory, it doesn't just pack fields together like sardines. To keep the CPU happy, it aligns data types to their natural boundaries—a u64 prefers to start at an address divisible by 8, while a u8 is happy anywhere.

If you place a u8 before a u64, the compiler adds "padding" bytes after the u8 to ensure the u64 starts on an 8-byte boundary. This wasted space adds up, increasing your struct's total memory footprint and potentially causing cache misses.

Optimizing Layouts

You can often reduce the size of your structs simply by reordering your fields from largest to smallest.

Compare these two structs:

struct BadLayout {
    a: u8,   // 1 byte
    b: u64,  // 8 bytes (requires 7 bytes of padding after 'a')
    c: u8,   // 1 byte (requires 7 bytes of padding after 'c')
} // Total: 24 bytes

struct GoodLayout {
    b: u64,  // 8 bytes
    a: u8,   // 1 byte
    c: u8,   // 1 byte
} // Total: 10 bytes (rounded up to 16 by alignment rules)

In BadLayout, the gaps created by alignment requirements result in 24 bytes of memory usage. By placing the u64 first in GoodLayout, the smaller fields can fill the space that would otherwise be wasted as padding. The struct size drops significantly, allowing more instances of your data to fit into the CPU’s L1 cache simultaneously.

Why This Matters

Modern CPUs are incredibly fast, but fetching data from main memory is relatively slow. By minimizing padding, you ensure that your data is packed tightly. This means your CPU spends less time waiting for memory fetches and more time processing your code. When designing high-performance structs, always try to group your fields by size, placing larger types at the top.

Key Takeaway: By ordering your struct fields from largest to smallest, you reduce unnecessary padding, save memory, and maximize CPU cache efficiency.