Bit Fields and the Optimiser
November 2026
How could making Int fields optional double the size of your data model? How efficiently can a generic struct using protocols be optimised by the compiler? In this post I’ll look into the hidden cost of using Optional with scalars, create an arbitrarily sized bit field type to solve the issue, and examine how the Swift compiler optimises that type away to almost nothing.
I was recently working on a problem where code-generated Swift structs could contain dozens of optional scalar properties:
struct Foo {
var field1: Int?
var field2: Int?
var field3: Int?
// ...
var field30: Int?
}
It occurred to me that this might not have a particularly efficient representation in memory, since each of those Optional flags would need an extra byte per field to keep track of nil values. This is easy to verify by printing the size of each:
MemoryLayout<Int>.size // 8 bytes
MemoryLayout<Int?>.size // 9 bytes
Each of those 9-byte fields would need 7 bytes of padding because Int needs to be aligned to the nearest 8 byte boundary. This can also be verified using MemoryLayout.stride, which is the minimum space needed for packed fields of the same type:
MemoryLayout<Int>.stride // 8 bytes
MemoryLayout<Int?>.stride // 16 bytes!
So making each of the fields optional doubles the size that the struct takes up in memory compared to a tightly packed struct of non-optional Int fields.
The cost of Optional
A side note on how Swift enums are represented in memory because I’ve always thought it was quite clever.
Sometimes wrapping a type in Optional takes up zero additional memory. How is this possible? Optional is an enum with two cases (“none” and “some”) and the compiler tries to look for unused values (“extra inhabitants”) in the wrapped type, which it can use to store the tag. This is explained well in the Swift ABI docs.
Object pointers are a good example; these are 8 bytes but have an unused value of zero since null is never a valid pointer. Swift uses the null value to mean “none” for an optional pointer, so an NSObject? variable also takes only 8 bytes.
A Bool takes up one byte but since it really only needs a single bit for the value, the optional tag can also fit in that same byte.
Swift can even see inside structs to look for unused bits, for example:
struct Baz {
let flag: Bool?
}
MemoryLayout<Baz>.size // 1 byte
MemoryLayout<Baz?>.size // also 1 byte!
Here the compiler has packed two optional flags (Baz?.flag?) and the value into a single byte. How smart is that!?
This has a great practical benefit: optional versions of common Swift collections like String? and Array? take up no additional memory, even though these are struct types. Optional enums also usually don’t take extra space because the compiler effectively adds a “none” case to the existing enum.
However, scalars like Int need to use all of their bits so Swift has no choice but to tack on another byte to store the optional “none” value. There was an interesting discussion on using redundant NaN floating-point values, but it looks like Double? still takes 9 bytes.
Back to the original problem…
Storing optional flags separately
Since the optional Int fields in my original struct were using double the memory, why not remove the optional flag from each field and store them separately? It could look something like this instead:
struct Foo {
var field1: Int
var field2: Int
var field3: Int
// ...
var field30: Int
var optionalFlags: UInt32
}
Each of those optional flags only needs a single bit, so they can all be packed into a single UInt32. This reduces the size of the original struct from 480 bytes (30× 16-byte Int?) to only 248 bytes (30× 8-byte Int + 4-byte UInt32 + 4-byte padding) — a saving of almost 50%.
Of course this is far less convenient to use than optionals:
let isMissing = foo.field10 == nil
// vs
let isMissing = foo.optionalFlags & (1 << 9) == 0
Luckily I was working with code-generated Protobuf models so the public API didn’t need to change at all. There was already a foo.hasField10 generated alongside each field.
Larger bit fields
My next problem was: what if a struct had more than 64 optional fields and I couldn’t use a UInt64 for optionalFlags? This might seem unlikely but some of the test cases actually had hundreds of fields, so I needed to solve for the extreme case.
The first idea I had was to use a Data (or [UInt8]) containing the necessary number of bytes for all the bit flags I needed. This worked, but it didn’t seem like the optimal solution. These collection types have huge APIs and support for resizing that I didn’t need, plus they heap-allocate their storage. I really only wanted a simple inline value — a bigger UInt.
Swift has recently added support for UInt128, but it still doesn’t solve the general case.
Composing multiple UInt fields into a struct would work, as it would be inline and only take up the exact amount of space required. Firstly though, how do I want to use this type? Ideally it would act like an array of bits that I could write and read by index, something like:
foo.optionalFlags[9] // test a bit
foo.optionalFlags[3] = true // set a bit
I’ll create a protocol for this so that I can use an int of any size depending on how many optional flags are required. For example, if a struct has 10 optional fields, I’d use a UInt16 to store those 10 bits.
protocol BitIndexable {
static var bitWidth: Int { get }
init()
subscript(bit: Int) -> Bool { get set }
}
The standard Swift ints already provide bitWidth and init(), but I’ll need these when I create a compound bit field struct.
Here’s how I conform the existing fixed-size ints to BitIndexable:
extension BitIndexable where Self: FixedWidthInteger & UnsignedInteger {
subscript(bit: Int) -> Bool {
get {
precondition(bit >= 0 && bit < Self.bitWidth)
return self & (1 << bit) != 0
}
set {
precondition(bit >= 0 && bit < Self.bitWidth)
if newValue {
self |= (1 << bit) // set bit
} else {
self &= ~(1 << bit) // clear bit
}
}
}
}
extension UInt8: BitIndexable {}
extension UInt16: BitIndexable {}
extension UInt32: BitIndexable {}
extension UInt64: BitIndexable {}
So what does this look like in use?
var flags: UInt8 = 0
flags[2] = true
flags[7] = true
print(String(flags, radix: 2)) // 10000100
Nifty!
Now to create a larger BitField structure for when I need more than 64 flags. I want to make it so that I can compose any two bit fields together using as little memory as necessary. For example if I need 90 flags, I’ll join a UInt64 and a UInt32 for a total of 96 bits. This type will be generic on the contained ints like BitField<UInt64, UInt32>.
Using composition like this enables a bit field of any size to be defined. If I need 200 flags, I can use a BitField<BitField<UInt64, UInt64>, BitField<UInt64, UInt8>> (64 + 64 + 64 + 8 = 200). It’s not pretty, but this is similar to the way that deeply nested generic types are used in SwiftUI or Combine.
You may be asking by now: “couldn’t you just use
InlineArray??” Yes that would make the problem a lot easier, but I wrote this beforeInlineArrayexisted, so let’s pretend we need to support an older version of iOS where it’s not available 😄
Here’s how a composed bit field would work, basically splitting the bit index between the left and right parts (a and b):
struct BitField<T: BitIndexable, U: BitIndexable>: BitIndexable {
var a = T()
var b = U()
static var bitWidth: Int { T.bitWidth + U.bitWidth }
subscript(bit: Int) -> Bool {
get {
precondition(bit >= 0 && bit < Self.bitWidth)
return bit < T.bitWidth ? a[bit] : b[bit - T.bitWidth]
}
set {
precondition(bit >= 0 && bit < Self.bitWidth)
if bit < T.bitWidth {
a[bit] = newValue
} else {
b[bit - T.bitWidth] = newValue
}
}
}
}
But surely using this nested generic type with protocols is slow compared to a simple bit check?
Examining how
BitField
compiles
I wouldn’t normally dig into the assembly code but it’s fascinating to see how well the Swift compiler can optimise something like BitField. What I want to see is what a call to flags[index] looks like, compared to accessing a single bit directly, so I’ll build these three functions:
typealias BitField128 = BitField<UInt64, UInt64>
// Read a single bit directly from a UInt64
public func getUIntFlagDirect(field: UInt64, index: Int) -> Bool {
precondition(index >= 0 && index < 64)
return field & (1 << index) != 0
}
// Read a bit flag from a plain UInt64 via BitIndexable
public func getUInt64Flag(field: UInt64, index: Int) -> Bool {
field[index]
}
// Read a bit flag from a composed BitField (built from 2× UInt64)
public func getBitField128Flag(a: UInt64, b: UInt64, index: Int) -> Bool {
BitField128(a: a, b: b)[index]
}
There’s a reason why I didn’t use
BitField128as a parameter: if the type is not used in a public API then the compiler does not need to export it. This makes it much easier to see only the assembly code we’re interested in.
A handy online tool for creating and viewing annotated assembly output is the Godbolt compiler explorer. You can view my BitField code directly on Godbolt or try it yourself:
- Paste all the Swift snippets above into Godbolt and select Swift as the language.
- On the right pane select the latest aarch64 (ARM) compiler, otherwise it defaults to x86-64 (Intel). Either is fine, but I’ll be using snippets of ARM assembly below.
- Type “-O -wmo” in the “Compiler options” box. This will enable compiler optimisations and also whole-module optimisations, which removes non-public types so most of the compiled code disappears.
You should see something like this:
output.getUInt64Flag(field: UInt64, index: Int) -> Bool:
cmp x1, #63
b.hi .LBB1_2
lsr x8, x0, x1
and w0, w8, #0x1
ret
.LBB1_2:
brk #0x1
output.getBitField128Flag(a: UInt64, b: UInt64, index: Int) -> Bool:
cmp x2, #128
b.hs .LBB2_2
subs x8, x2, #64
csel x8, x2, x8, lo
csel x9, x0, x1, lo
lsr x8, x9, x8
and w0, w8, #0x1
ret
.LBB2_2:
brk #0x1
output.getUIntFlagDirect(field: UInt64, index: Int) -> Bool:
b output.getUInt64Flag(field: UInt64, index: Int) -> Bool
You can right-click any opcode in Godbolt for an explanation, but essentially getUInt64Flag is:
- Check
indexlies in 0 to 63, trap if out of range - Right-shift
fieldbyindex - Return the lowest bit
So what does this tell us?
Compiler optimisation results
Firstly this is a really tiny handful of instructions. There are no function calls and no evidence of BitIndexable — it’s all been optimised away!
Secondly, did you notice getUIntFlagDirect? This is a single instruction that says to unconditionally jump to getUInt64Flag, which means the implementations are identical. All that protocol and subscripting code compiles down to the exact same machine code as a manual bit mask.
The getBitField128Flag function for the composed bit field is a little longer because it needs to select between the left and right ints, but this is still highly optimised. There are no function calls or even conditional branches after the precondition. The whole generic BitField type is just gone.
But it gets better. Remember my original models had generated getters for every optional field like hasField10? Look what happens when you generate code which uses a fixed index:
output.hasField10(a: UInt64, b: UInt64) -> Bool:
ubfx x0, x0, #9, #1
ret
That’s right: testing a specific flag comes down to a single CPU instruction 🤯
Even the precondition checks are gone because the compiler knows this is a valid index. So not only would my generated models be smaller than using Optional<Int> fields, but also as fast.
I find it amazing that we can write this safe, expressive Swift code and the compiler says “I get what you’re after” and spits out only a couple of CPU instructions. All that abstraction costs nothing at runtime. It’s magic ✨
Any comments or questions about this post? ✉️ nick @ this domain.
— Nick Randall