beginner

The Struct That Grew By Half

Read the counters before the options. Nothing here is labelled with the answer.

The report

We added one boolean flag to a particle struct and memory use for the particle array jumped by about 30%. One boolean. The array is 20 million elements and we are now over our memory budget. Someone thinks the language runtime is boxing the field.

Before and after the flag was added
// before — 24 bytes per particle
struct Particle {
    double x        // 8 bytes, 8-byte aligned
    double y        // 8 bytes
    uint32 id       // 4 bytes
}                   // + 4 bytes trailing padding = 24

// after — 32 bytes per particle
struct Particle {
    double x        // 8 bytes
    bool   active   // 1 byte  <-- added here
    double y        // 8 bytes
    uint32 id       // 4 bytes
}
CountersSIMULATED
sizeof(Particle)24 → 32 bytesThe struct grew by 8 bytes after adding a 1-byte field.
array footprint (20M)480 MB → 640 MBTotal memory grew in proportion to the per-element size.
particles per 64-byte line2.67 → 2.0Fewer elements now fit within each unit of transfer.
L1-dcache-load-misses (scan)1.6% → 2.1%The sequential scan misses somewhat more often than before.
Field offsets reported by the compiler for the "after" layout
offset  size  field
     0     8  x
     8     1  active
     9     7  <padding>
    16     8  y
    24     4  id
    28     4  <padding>
total 32 bytes, alignment 8
What is the hardware doing?