Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Struct-of-arrays collection type in Zig (github.com/ziglang)
8 points by dilap on Feb 25, 2021 | hide | past | favorite | 1 comment


Saw someone mention this on twitter, and thought it was really rad.

Most languages have some way to group things together, i.e., a struct. Many languages also provide some sort of array primitive or library data type, e.g. in Go []Foo is a contiguous list of Foo's in memory, similarly List<Foo> in C#, vector<Foo> in C++, etc.

This is often a very convenient way to organize your code, but there is a potential peformance pitfall in the resulting memory layout: if you want to iterate over your collection only accessing a subset of the fields, the other fields are just junk in memory, causing worse performance (since less of the data you're interested ends up in the cache at once).

A solution is to reorganize your code into a struct of arrays, e.g. instead of using a list of the Foo struct, you make a "FooList" struct, which contains a list for every field that used to be in Foo. This puts all values for the same field contiguously next to each other, improving performance.

But, it comes at a cost: often times the resulting code structure is much less convenient than just being able to say something like list<Foo>.

The linked zig type, MultiArrayList, is really neat because it gives you both: using Zig's compile-time execution feature, it's a generic type that provides the familiar list<Foo> interface, but has a memory layout where all the fields are contiguous with each other.

Pretty cool!




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: