Implementing a Struct of Arrays
Published on: 2025-07-17 14:52:15
Implementing a Struct of Arrays
Recently, I watched Andrew Kelley’s talk on Practical Data Oriented Design. It goes into some of the architectural changes he’s been making to the Zig compiler, with pretty significant performance benefit. Would definitely recommend checking out the talk, even if you’re like me and have never written any Zig.
About halfway through the talk, he shows a way to improve his memory usage by avoiding wasting memory. By turning this structure:
const Monster = struct { anim : *Animation, kind : Kind, const Kind = enum { snake, bat, wolf, dingo, human }; }; var monsters : ArrayList(Monster) = .{};
into this one:
var monsters : MultiArrayList(Monster) = .{};
ArrayList(Monster) is what we could call std::vector , and MultiArrayList(Monster) now stores the anim s and kind s in two separate arrays, instead of one. That is, a struct of arrays instead of an array of structs. But it’s a tiny code change.
One of the interesting things about Zig to me is t
... Read full article.