Data Alignment and Performance on x86
I went looking recently for whether accessing unaligned data on x86 was still slow. I found a couple of other blog posts: one that claimed it was no slower, and another that claimed it was slower, but only when using certain stride lengths of data.
I’d also done some previous experimentation that showed it only made a performance difference for large working sets, which pointed me at the conclusion that alignment only mattered when reading from memory, and not from the cache.
So, I wrote a simple program that could misalign some data and read back chunks of it. To separate alignment effects from cache effects, I wrote the program such that the random reads could be within a controlled interval, for example the width of a cache line. The source is here.
I’m running this code on an Intel Core i7-7700K, overclocked to 4.6GHz and with Turbo Boost turned off. I left hyperthreading on, and core-isolated the CPU the code ran on. This CPU has the following cache sizes:
- L1: 64KiB (line size: 64 bytes)
- L2: 256KiB
- L3: 8MiB
I hope to be able to isolate where (if at all) alignment matters. Given these cache sizes, and the “randomness” variable being the number of 8-byte integers read, I expect to see slowdowns at around 8192, 32768, and 8388608. (The number of 8-byte integers that can be stored in 64KiB, 256KiB and 8MiB respectively). So, here is a pretty graph:
As you can see from the graph, we start getting into a bigger discrepancy once our working set size exceeds the size of our L3 cache (the blue line is for correctly aligned data). So, it does matter when reading from memory, but not necessarily when reading from the cache. However, it doesn’t seem to matter how misaligned the data is.
Appendix: Raw data
Note that this particular markdown renderer does not support tables. You might be better off looking at the source code for this webpage.
Alignment = 0: | randomness | time | | ———- | ——- | | 1 | 8181157 | | 2 | 8354898 | | 4 | 8174322 | | 8 | 8211249 | | 16 | 8343181 | | 32 | 8488852 | | 64 | 8750264 | | 128 | 10064173| | 256 | 12656142| | 512 | 12786024| | 1024 | 12257925| | 2048 | 12052490| | 4096 | 12062776| | 8192 | 12472646| | 16384 | 12865153| | 32768 | 15663143| | 65536 | 22561254| | 131072 | 25891264| | 262144 | 28353846| | 524288 | 30722955| | 1048576 | 36841277| | 2097152 | 54355806| | 4194304 | 66082659| | 8388608 | 72649163| | 16777216 | 75744947| | 33554432 | 76947762| | 67108864 | 77817510|
Alignment = 1: | randomness | time | | ———- | ——- | | 1 | 8447980 | | 2 | 8491618 | | 4 | 8390634 | | 8 | 8440255 | | 16 | 8406359 | | 32 | 8541830 | | 64 | 8890191 | | 128 | 10205219| | 256 | 12874106| | 512 | 12759274| | 1024 | 12268464| | 2048 | 12039457| | 4096 | 12195217| | 8192 | 12545538| | 16384 | 13075868| | 32768 | 16358233| | 65536 | 23508325| | 131072 | 26678173| | 262144 | 29223873| | 524288 | 30873066| | 1048576 | 38475869| | 2097152 | 58863340| | 4194304 | 72666111| | 8388608 | 79778571| | 16777216 | 83870194| | 33554432 | 85435103| | 67108864 | 85995102|
Alignment = 2: | randomness | time | | ———- | ——- | | 1 | 8270807 | | 2 | 8474441 | | 4 | 8448831 | | 8 | 8363270 | | 16 | 8483864 | | 32 | 8549659 | | 64 | 8988867 | | 128 | 10359135| | 256 | 12844133| | 512 | 12798989| | 1024 | 12215322| | 2048 | 11983799| | 4096 | 12182183| | 8192 | 12677278| | 16384 | 13036499| | 32768 | 16307194| | 65536 | 23498263| | 131072 | 27525089| | 262144 | 29602856| | 524288 | 31276775| | 1048576 | 38428969| | 2097152 | 59516688| | 4194304 | 73094291| | 8388608 | 79994591| | 16777216 | 83437182| | 33554432 | 85232251| | 67108864 | 85745168|
Alignment = 3: | randomness | time | | ———- | ——- | | 1 | 8289606 | | 2 | 8553159 | | 4 | 8326458 | | 8 | 8374253 | | 16 | 8362783 | | 32 | 8510911 | | 64 | 8835021 | | 128 | 10128544| | 256 | 12788627| | 512 | 12691523| | 1024 | 12238412| | 2048 | 11955787| | 4096 | 12170610| | 8192 | 12597820| | 16384 | 13103941| | 32768 | 15946022| | 65536 | 22942537| | 131072 | 26925065| | 262144 | 28712440| | 524288 | 30668532| | 1048576 | 37790083| | 2097152 | 58449610| | 4194304 | 72099845| | 8388608 | 79293454| | 16777216 | 82973218| | 33554432 | 84628554| | 67108864 | 85486755|