To compare spacing between array elements, inspect stride as well as size. Size describes the value’s footprint; stride includes the alignment gap before the next value. Reordering properties can change padding and therefore both measurements.
Mechanism and design choice
Bad stores Bool, Double, Int32; Good stores Double, Int32, Bool. Double has alignment 8 on this target, so placing it after a small field introduces padding. Verify sizes for the compiler, target, and type definition; do not treat source field order as a permanent ABI contract. Primary source
Reproduce the experiment
The experiment prints both structs’ size, stride, and alignment, then measures Bool?, Int?, a single non-class-constrained protocol existential, and an Array value. It requires a 64-bit target and stops if the recorded expectations differ, making environment changes visible.
This standalone example was run on 2026-10-23 with Apple Swift 6.4 targeting arm64 macOS, in Swift 6 language mode. It needs no network or additional packages.
Download the complete Swift file
Run the command in the download directory. Preconditions stop execution if a check fails. Do not use -Ounchecked: it removes these checks.
swift -swift-version 6 swift-memory-layout-size-stride-alignment.swift
struct Bad {
var flag: Bool
var value: Double
var count: Int32
}
struct Good {
var value: Double
var count: Int32
var flag: Bool
}
protocol Shape {}
precondition(MemoryLayout<Int>.size == 8, "This experiment requires a 64-bit target")
let bad = (MemoryLayout<Bad>.size, MemoryLayout<Bad>.stride, MemoryLayout<Bad>.alignment)
let good = (MemoryLayout<Good>.size, MemoryLayout<Good>.stride, MemoryLayout<Good>.alignment)
print("bad=size:\(bad.0),stride:\(bad.1),alignment:\(bad.2)")
print("good=size:\(good.0),stride:\(good.1),alignment:\(good.2)")
print("optional=bool-size:\(MemoryLayout<Bool?>.size),int-size:\(MemoryLayout<Int?>.size),int-stride:\(MemoryLayout<Int?>.stride)")
print("existential=\(MemoryLayout<any Shape>.size) array-value=\(MemoryLayout<[Int]>.size)")
precondition(bad == (20, 24, 8) && good == (13, 16, 8), "Layout differs from the recorded toolchain")
precondition(MemoryLayout<Bool?>.size == 1 && MemoryLayout<Int?>.size == 9)
precondition(MemoryLayout<Int?>.stride == 16 && MemoryLayout<any Shape>.size == 40)
precondition(MemoryLayout<[Int]>.size == 8)
Recorded output and interpretation
bad=size:20,stride:24,alignment:8
good=size:13,stride:16,alignment:8
optional=bool-size:1,int-size:9,int-stride:16
existential=40 array-value=8
Here Bad measured size 20 and stride 24; Good measured size 13 and stride 16. A million contiguous elements differ by 8,000,000 bytes in stride alone. Bool? had size 1, Int? size 9 and stride 16, any Shape size 40, and the Array value size 8.
Scope and limitations
An 8-byte Array value does not contain all its elements. Heap backing storage and allocator overhead are outside this measurement. The existential’s 40 bytes are not universal for every any type. These are observations on the recorded target, not guaranteed numbers on other platforms.
What to check next
Measure instance counts and heap allocations first. Consider property reordering only where padding materially affects a large collection, checking API compatibility too. If value and reference storage are unclear, begin with the class-versus-struct experiment.
Sources and verification
- Swift ABI: Type LayoutSwift.org · Official documentation · Checked October 23, 2026Supports: Source for the implementation contract and explanation. The output was checked separately using the experiment included here.

