Struct Layout: Combined vs Split Field Storage

Question 15 / 51 Correct so far: 0 (0 answered)

Snippet A

Mixed Struct

struct Entity {
    float x, y, z;
    float health;
    char  name[48];
};

float sumX = 0.0f, sumHealth = 0.0f;
for (const auto& e : ENTITIES) {
    sumX      += e.x;
    sumHealth += e.health;
}
Snippet B

Split Struct

struct EntityHot {
    float x, y, z;
    float health;
};

struct EntityCold {
    char name[48];
};

float sumX = 0.0f, sumHealth = 0.0f;
for (const auto& e : HOT) {
    sumX      += e.x;
    sumHealth += e.health;
}
Shared test data (shared-setup)
constexpr std::size_t kNumEntities = 524288;

Which snippet is faster?

Select the correct answer(s)