Contents
Update: slides from Go Night Reading #588
Dig101: dig more, simplify more, know more
Strings, slices, and maps all lean on structs under the hood. Here: struct memory alignment — handy when reading stdlib code and shaving wasted padding.
A few terms first:
- Word — the natural data unit for the machine (
machine word); the chunk the CPU often moves in one step. - Word size — bits per word (often 16 / 32 / 64 today; on many systems word size in bytes is
N/8for N-bit CPUs).
Registers, CPU↔memory transfers, and many addresses are word-oriented. See Word (computing).
0x01 Why align?
CPUs do not read memory byte-by-byte; they access at 2/4/8-byte (etc.) granularities.
If address and size are n-byte aligned, one transfer suffices; otherwise the hardware does extra work.
0x02 Data structure alignment
Size guarantees
From Size and alignment guarantees:
| type | size (bytes) |
|---|---|
| byte, uint8, int8 | 1 |
| uint16, int16 | 2 |
| uint32, int32, float32 | 4 |
| uint64, int64, float64, complex64 | 8 |
| complex128 | 16 |
A struct or array type has size zero if it contains no fields (or elements) larger than zero. Two distinct zero-size variables may share the same address.
struct{} and [0]T have size 0; distinct zero-size vars may share an address.
Alignment guarantees
- For any variable
x:unsafe.Alignof(x) ≥ 1.- For struct
x:Alignof(x)is the max of field alignments, at least 1.- For array
x: same as element type alignment.
| type | alignment |
|---|---|
| bool, byte, uint8, int8 | 1 |
| uint16, int16 | 2 |
| uint32, int32 | 4 |
| float32, complex64 | 4 |
| arrays | element type |
| structs | fields |
| other types | one machine word |
On 32-bit, a machine word is 4 bytes; on 64-bit, 8.
1 | type T1 struct { |
On amd64, max field alignment is 8. T1 pads after a and after c → 24 bytes. Reordering to T2 packs a+c then one pad before b → 16 bytes.
Reordering fields can cut padding.
0x03 Zero-size fields
struct{} has size 0. As a non-final field it adds no padding; as the final field the compiler still pads the struct.
Why? A pointer to that final zero field would point past the struct’s storage; if that pointer outlives the struct, you leak unrelated memory.
1 | type T1 struct { |
0x04 Address alignment
Addresses may need to be multiples of
Alignof(T). For variablex:uintptr(unsafe.Pointer(&x)) % unsafe.Alignof(x) == 0.
sync.WaitGroup exploits this:
1 | type WaitGroup struct { |
state1 is three uint32s. On 64-bit, uint32 is 4-aligned, so state1 is at 4N.
- If
4Nis also 8-aligned (Neven), the first 8 bytes are a valid*uint64. - If not (
Nodd), bytes[1:9]are 8-aligned.
The spare 4 bytes hold sema.
Why force 64-bit alignment on 32-bit? So uint64 can be updated atomically.
0x05 Safe 64-bit access
sync/atomic BUG note: on x86-32, ARM (non-Linux), and 32-bit MIPS, you must 8-align uint64 used with atomic ops (Pentium MMX+ on x86-32, etc.).
Why
uint64 is 8 bytes. On 32-bit, alignment is 4 → the value can span two words; a non-atomic pair of loads/stores is racy. issue #6404 shows int64 after bool only 32-bit aligned.
How
The first word in a variable or in an allocated struct, array, or slice can be relied upon to be 64-bit aligned.
“Allocated” = declared, make, new.
On GOARCH=386, safe examples:
1 | var c0 int64 |
Slice data lives on the heap and is aligned; embedded arrays of structs are not — this panics:
1 | c51 := struct { |
Heap pointer to int64:
1 | c6 := struct { |
Caveat: a 12-byte struct whose first field is int64 may still fail alignment — issue #37262.
Or use a mutex
1 | c := struct{ |
Why not uint64 in WaitGroup directly? Embedding WaitGroup anywhere but the first field would break alignment guarantees.
Takeaways
- Alignment speeds memory access.
- Address of
x:uintptr(unsafe.Pointer(&x)) % Alignof(x) == 0. - Reorder fields to shrink structs; avoid trailing
struct{}. - On 32-bit, 8-align
uint64for atomics — or use a mutex.
Tools: dominikh/go-tools (structlayout, structlayout-optimize, structlayout-pretty).
Code: NewbMiao/Dig101-Go
See also: Golang memory alignment
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
