How does align work?

Heyo,

I have heard a lot about alignment and cache usage from time to time and know what it does on a very high level. I do, however, find it very hard to know when to actually use it in practice and where it may help.

I would greatly appreciate some practical example(s) that demonstrate where using alignment deliberately can lead to better performance.

I find that there are very few good resources for this for beginners (I personally couldnt find any, at least not specifically for zig).

1 Like

One key example I know of is with multithreaded code. The problem is known as “false sharing” and it means, if you might be dealing with truly independent buckets of data that that are on the same cache line; this can actually worsen multithreaded performance.

This happens because the caches are trying to flush the contents back into main memory too frequently when it might not be necessary because sharing of this data isn’t really needed. It’s an example of the CPU trying to be too smart and it negatively impacting performance.

So, if you align your data so that it falls on separate cache lines you can mitigate this performance problem which can have a measurable impact on your speed because now your data is better spread out (at the cost of a larger footprint in the cache).

There will be better articles about this that go into depth with examples and explain it better than I could so please see that problem.

In Zig, you can mostly choose not to worry about alignment but the total control is there if needed.

Other reasons to align memory:

  1. Better packing of structs - but Zig goes out of its way to do this automatically
  2. Exploiting the cache levels like the example above
  3. Some CPU instructions require it or they won’t work or cause a crash
  4. Some CPUs require the stack to be aligned
  5. Generally in most base cases your code will be naturally aligned automatically. For example if you use an ArrayList, under the hood it calls ArrayListAligned and it will inspect your type and align it properly.

There’s likely more reasons but this is a high-level answer but alignment is a key concept in the large field of computer organization which in some cases is mandatory and in other cases can offer performance benefits.

3 Likes