Data alignment for speed: myth or reality? (2012)
lemire.me
lemire.me
But note bene, unaligned vector stores that cross 4K pageline boundaries are so slow that they can destroy performance despite their rarity. Here's what the Intel Architecture Optimization Guide has to say:
11.6.3 Prefer Aligned Stores Over Aligned Loads
There are cases where it is possible to align only a subset
of the processed data buffers. In these cases,
aligning data buffers used for store operations usually
yields better performance than aligning data
buffers used for load operations.
Unaligned stores are likely to cause greater performance
degradation than unaligned loads, since there
is a very high penalty on stores to a split cache-line that
crosses pages. This penalty is estimated at 150
cycles. Loads that cross a page boundary are executed at
retirement. In Example 11-12, unaligned store
address can affect SAXPY performance for 3 unaligned
addresses to about one quarter of the aligned
case.
http://www.intel.com/content/dam/www/public/us/en/documents/...1. http://infocenter.arm.com/help/topic/com.arm.doc.ddi0333h/Cd...
The end of the essay says "Recent ARM processors do support unaligned memory accesses though it is unclear what the performance penalty is." and points to basically the same documentation you did.
'kbart' does have a point: it's always risky talk about hardware behaviour as if C++ code actually specifies what processor is doing. And while the first line shows the flags, one could argue that without the version number for g++ you still don't know what the compiler has done. But in this case, it does seem that gcc-4.7 and gcc-4.8 both produce more less the code that you'd expect when compiling with -O2.
But to show that compiler behaviour is not just an idle worry, icpc 14.0.3 -O3 produces much faster times by realizing it can combine the loops and completely omit the reads. And g++ -O3? Oh, it segfaults, because it tries to optimize to a 128-bit 'movdqa' (Move Double-Quadword Aligned) that requires 16B alignment.
While unhelpful, I think it's technically legal for it to do this, as (incredibly?) reading an unaligned int directly from memory is actually undefined behaviour. But I guess I'd prefer if it just used the equally performant 'movdqu' (Move Double-Quadword Unaligned) instead, so it worked rather than crashing.
Separately, here's a more recent post coming to the same general conclusion as Daniel: http://cdglove98.blogspot.com/2014/05/the-performance-of-una...