Struct Layout and Padding
Prerequisites: Copies, Moves, and Object Layout, Cache Lines, Locality, and Pointer Chasing
The course's MarketUpdate has 28 bytes of fields but a sizeof of 32. Build a calculator that applies the compiler's placement rules to struct definitions.
What to implement
process_commands(commands)returns the printed lines, in command order.- Each command is one line. A
STRUCTcommand is followed by its field lines. - On stdin, the first line is the number of lines that follow.
- There are three parts. One solution must pass all ten tests.
| Command | What it does |
|---|---|
STRUCT S k + k field lines | Define struct S (prints nothing) |
LAYOUT S | Print the layout of S |
REORDER S | Print S with its fields reordered (Part 2) |
ARRAY S n start | Count cache lines for an array of S (Part 3) |
Field types
A field line is type name or type name[N]. Sizes are the x86-64 Linux ones, and each scalar's alignment equals its size.
| Type | Size and alignment, in bytes |
|---|---|
bool, char, int8_t, uint8_t | 1 |
int16_t, uint16_t | 2 |
int32_t, uint32_t, float | 4 |
int64_t, uint64_t, double | 8 |
any pointer (char*, Order*, ...) | 8 |
a struct defined by an earlier STRUCT | its sizeof, aligned to its alignof |
- A pointer's target doesn't have to be defined, so
Node* nextis a valid field ofNode. name[N]isNelements back to back: size isN× the element's size, alignment is the element's alignment.- Names print exactly as declared,
[N]included.
Part 1: Place the fields
The three rules (they explain MarketUpdate's 32 bytes):
- Fields go in declaration order, starting at offset 0. Each field starts at the first multiple of its alignment at or after the previous field's end. Skipped bytes are padding.
alignof(S)is the largest alignment of any field.sizeof(S)is the last field's end rounded up to a multiple ofalignof(S). The bytes added at the end are tail padding.
A struct-typed field is one block: size sizeof, alignment alignof. Its inner padding, tail padding included, stays inside the block.
Output of LAYOUT S: a header, then every region in offset order.
layout S sizeof X alignof Y padding Z field offset size name pad offset size
X=sizeof(S),Y=alignof(S),Z=sizeof(S)minus the sum of field sizes.- One
fieldline per field. - One
padline per run of padding bytes (between fields or at the end). No padding means nopadlines.
Limits
- Names use letters, digits and underscores, and don't start with a digit. Struct names are unique and never match a type name. Field names are unique within a struct.
- 1 to 50 fields per struct.
N≤ 1,000,000. Everysizeof≤ 109. At most 1,000 lines. - Every struct a command or field names was defined earlier.
Tests 1–4 cover this part (only STRUCT and LAYOUT).
Part 2: Reorder to remove padding
The same fields in a different order can make a smaller struct. REORDER S lays out the fields of S in this order, then applies the Part 1 rules:
- Decreasing alignment. Fields with equal alignment keep their declaration order.
- A struct-typed field moves as one block and keeps its inner layout.
- An array sorts by its element's alignment, not its size:
char symbol[8]sorts with the 1-byte fields.
Output: the same block as LAYOUT S, except:
- the header starts with
reorderinstead oflayout; - the header ends with
saved D, whereD= declaredsizeof(S)− reorderedsizeof(S)(0 if the new order is no smaller).
REORDER doesn't change S. Later commands, and structs that contain S, still see the declared layout.
Tests 5–7 cover this part.
Part 3: Arrays across cache lines
A cache line is 64 bytes. Line L covers offsets 64 * L through 64 * L + 63.
ARRAY S n start places n elements of S back to back, starting start bytes after a line boundary:
- Element
i(from 0) covers offsetsstart + i * sizeof(S)throughstart + (i + 1) * sizeof(S) - 1. startis 0 to 63 and a multiple ofalignof(S), like the address of a real array.
Output: one line.
array S count n start start bytes B lines L straddling K
B=n * sizeof(S), the array's size.L= the number of cache lines holding at least one byte of the array.K= the number of elements that straddle a line boundary (first and last byte in different lines). An element over 64 bytes always straddles.
n goes up to 109: B needs a 64-bit integer, and looping over every element is too slow.
Tests 8–10 cover this part.
13 STRUCT MarketUpdate 4 uint64_t sequence char symbol[8] int64_t price int32_t qty LAYOUT MarketUpdate STRUCT Tagged 5 bool is_bid uint64_t sequence char symbol[8] int64_t price int32_t qty LAYOUT Tagged
layout MarketUpdate sizeof 32 alignof 8 padding 4 field 0 8 sequence field 8 8 symbol[8] field 16 8 price field 24 4 qty pad 28 4 layout Tagged sizeof 40 alignof 8 padding 11 field 0 1 is_bid pad 1 7 field 8 8 sequence field 16 8 symbol[8] field 24 8 price field 32 4 qty pad 36 4
MarketUpdate: fields end at 8, 16, 24 and 28, with no gaps between them. The largest alignment is 8, so sizeof rounds 28 up to 32 (4 bytes of tail padding).
Tagged adds a 1-byte is_bid in front. sequence needs a multiple of 8, so 7 bytes of padding follow the flag, every later field moves by 8, qty ends at 36, and sizeof rounds up to 40. One extra byte of data costs 8 bytes.