« All posts

Cache-Conscious Data Layout in Rust: Field Zoning and False Sharing

Exploring field zoning, cache alignment, and false-sharing prevention for multi-core Rust data structures, illustrated through an SPSC ring buffer design.

This piece opens a series on high-throughput, low-latency systems programming in Rust, using a single-producer/single-consumer (SPSC) ring buffer to examine memory layout decisions for multi-core data structures. It argues that beyond simply fitting data in cache, engineers must ask which core touches each field and how often — the root cause of false sharing, a performance pathology that no profiler flags directly.

The article walks through 'field zoning': grouping fields by write ownership and access frequency into producer-hot, consumer-hot, and cold regions. It shows why single-writer, non-atomic cache fields like cached_head and cached_tail belong in hot zones despite being snapshots, since they cut down expensive cross-core reads. Cold fields such as config and metrics are deliberately packed together without padding, since aligning everything wastes cache capacity that hot data needs.

It also explains why #[repr(C)] is essential: Rust's default repr(Rust) layout doesn't guarantee field ordering, so the compiler could silently reorder and collapse carefully separated zones. The piece transitions into alignment mechanics — including a CacheAligned<T> helper and the reasoning behind a 128-byte rather than 64-byte rule — as the concrete mechanism that turns zoning intent into real hardware-level separation.

» SourceHashnode #15