Formal Equations, Memory Identities & Mathematical Invariants

Review essential mathematical equations, alignment formulas, fat pointer byte identities, and formal theorems governing Rust systems engineering.

06. Systems Architecture Decision Matrices & Interview Bank6 min read

Rust's memory safety guarantees are founded on rigorous mathematical logic.

This reference lesson brings together the core formal equations, alignment formulas, memory identities, and theorems established throughout the curriculum.

Universal Fat PointerMemory Layout
16 Bytes

All standard fat pointers (&[T], &str, dyn Trait) occupy exactly 16 bytes on 64-bit hardware.

1. Hardware Alignment & Padding Equations#

For any composite struct SS containing field types F1,F2,…,FnF_1, F_2, \dots, F_n with alignments A(Fi)A(F_i):

Aggregate Struct Alignment#

The struct's alignment requirement equals the maximum alignment of its constituent components:

Align(S)=max⁡(A(F1), A(F2), …, A(Fn))\text{Align}(S) = \max\bigl(A(F_1), \, A(F_2), \, \dots, \, A(F_n)\bigr)

Field Offset Invariant#

Every field offset OiO_i must be an integer multiple of that field's alignment requirement:

Oi≡0(modA(Fi))O_i \equiv 0 \pmod{A(F_i)}

Total Memory Footprint#

Total memory footprint equals the sum of field sizes plus alignment padding bytes (PiP_i) and tail padding (PtailP_{\text{tail}}):

Size(S)=∑i=1nSize(Fi)+∑i=1nPi+Ptail\text{Size}(S) = \sum_{i=1}^{n} \text{Size}(F_i) + \sum_{i=1}^{n} P_i + P_{\text{tail}}
Size(S)≡0(modAlign(S))\text{Size}(S) \equiv 0 \pmod{\text{Align}(S)}

2. The XOR Aliasing Invariant#

At any given point pp in program execution, for any resource xx, active immutable references R(x)R(x) and active mutable references W(x)W(x) must satisfy mutual exclusivity:

∀x,(∣R(x)∣≥0∧∣W(x)∣=0)∨(∣R(x)∣=0∧∣W(x)∣≤1)\forall x, \quad \bigl(|R(x)| \ge 0 \land |W(x)| = 0\bigr) \quad \lor \quad \bigl(|R(x)| = 0 \land |W(x)| \le 1\bigr)
Valid(p)  ⟺  ∣R(x)∣×∣W(x)∣=0\text{Valid}(p) \iff |R(x)| \times |W(x)| = 0
Architecture Diagram
Rendering diagram...

3. 64-Bit Memory Size Identities#

Understanding the exact byte footprint of primitives and pointers on 64-bit hardware is essential for cache optimization:

Type ExpressionSize in BytesInternal Representation & Components
&T / &mut T8 BytesSingle 64-bit hardware memory pointer.
*const T / *mut T8 BytesBare 64-bit raw pointer address.
Box<T>8 BytesSingle 64-bit pointer pointing to heap allocation.
Option<&T>8 BytesNiche optimization: 0x0 represents None (Zero tag overhead).
&[T] (Slice Reference)16 BytesFat Pointer: 8-byte base address + 8-byte usize element count.
&str (String Slice)16 BytesFat Pointer: 8-byte UTF-8 data address + 8-byte usize byte length.
dyn Trait (Trait Object)16 BytesFat Pointer: 8-byte payload pointer + 8-byte static vtable pointer.
String24 Bytes8-byte pointer + 8-byte capacity + 8-byte length.
Vec<T>24 Bytes8-byte buffer pointer + 8-byte capacity + 8-byte length.
() (Unit / ZST)0 BytesZero-Sized Type: stripped from physical memory layouts.

4. The Thread Safety Theorem#

The foundational theorem connecting Send and Sync:

T:Sync  ⟺  &T:SendT : \text{Sync} \iff \&T : \text{Send}

A type TT is thread-safe to share across concurrent threads if and only if an immutable borrow of TT (&T\&T) is safe to transfer between threads.

Closure Subtyping Subset Hierarchy#

{c∣c∈Fn}⊆{c∣c∈FnMut}⊆{c∣c∈FnOnce}\{c \mid c \in Fn\} \subseteq \{c \mid c \in FnMut\} \subseteq \{c \mid c \in FnOnce\}

5. Concept Verification#

Test your mastery of formal equations and memory identities:

Concept check

What is the exact memory size of an Option<&T> on a 64-bit architecture?

Concept check

If a struct has three fields with alignment requirements 1 byte, 4 bytes, and 8 bytes, what is the aggregate alignment requirement of the struct?