4 ms·
Coding for Performance: Data alignment and structures (2013)
- srcmap 12y agoIn the rearrange example, can/does the compiler do the rearrange, padding automatically?
- CJefferson 12y agoIn both C and C++ fields in structs must be outputted in the order they are listed. There is in C++ a freedom to rearrange fields whenever the user switches between public/private/protected, but I believe no compiler takes advantage of it, because (I believe) it is believed too suprising for these kinds of changes to change a class' size.
- userbinator 12y agoNote that in modern x86 processors the overhead of unaligned accesses is nearly 0 - caching has more significant effect, so it's more important to make sure frequently accessed data can fit within a cache line even if it means some of it won't be aligned. Crossing cache-line boundaries is when things slow down. http://lemire.me/blog/archives/2012/05/31/data-alignment-for-speed-myth-or-reality/ http://lemire.me/blog/archives/2012/05/31/data-alignment-for... Edit: here's an algorithm that exploits this "nearly free" unaligned access for fast substring search: http://www.codeproject.com/Articles/250566/Fastest-strstr-like-function-in-C http://www.codeproject.com/Articles/250566/Fastest-strstr-li...