r/programming • • Aug 02 '26

Every byte matters

https://fzakaria.com/2026/06/01/every-byte-matters
204 Upvotes

43 comments sorted by

View all comments

Show parent comment

4

u/cdb_11 Aug 03 '26

Isnt this always a problem with SoA data types where you use for loops and you index into multiple arrays at the same index in each loop iteration?

See the pseudo code in my other comment. It can be a problem if the index is the same for all arrays, and the base addresses of arrays have the same alignment. So for example if all arrays are allocated with mmap, which is page aligned. Just changing the alignment should work. Allocate one extra page, and pick some random offset for the base address, or something. Likewise, iterating over large structs with power-of-two sizes can cause this as well. In that case, you can add extra 64 bytes to the struct size.