4 ms·
This is also why data alignment/structure padding can be slower than packing all the data together. Cache usage is very important in CPUs now, because in a lot
by userbinator 8y ago
This is also why data alignment/structure padding can be slower than packing all the data together. Cache usage is very important in CPUs now, because in a lot of cases the bottleneck is the memory. Padding is effectively wasted memory.
- dpark 8y agoIs this true on even x86? On most platforms[1] misaligned access will simply crash your program and you definitely will not execute faster (but you’ll probably terminate faster). On x86 you’ll pay a hefty penalty for the misaligned access and I would expect that it likely costs more than the cache optimization saves (but maybe not). Of course you can probably optimize your struct packing anyway. Shuffling members around can get a lot of benefit without a bunch of internal padding, though you might still have some trailing padding. [1] I guess these days the platforms are essentially just x86 and ARM.
- dooglius 8y agoAs long as you don't cross a cacheline boundary, I don't believe there is any misalignment penalty on recent x86/ARM processors.
- dpark 8y agoInteresting. I wasn’t aware that ARM allowed this at all.
- avian 8y agoIt really depends on architecture version and profile. Recent application profiles allow it. On microcontrollers misaligned access is still mostly forbidden as far as I know.
- api 8y agoThis inconsistency means you have to assume unaligned access is forbidden in portable code. Fun times.
- tinus_hn 8y agoMost code is not portable at the assembler level and compiler hides this kind of detail.
- dmitrygr 8y agoAllowed in v7m sometimes with a one cycle penalty for data. Not allowed for code. Not allowed for some instructions ( just like x86 requires alignment for some avx inductions)
- CyberDildonics 8y agoIt depends far more on memory access patterns. If memory is accessed linearly (backwards or forwards now) the prefetcher will get it for you before you get to it. Alignment matter much more when using atomics. X64 can do unaligned atomic access although (I think) there is a penalty for crossing the cache line boundary with atomics. Also there is an issue of 'false sharing' - two threads needing to sync cache lines even though the bytes they are accessing don't overlap. 128 bit (16 byte) atomic operations need to be aligned on 128 bit boundaries or they will crash. (if anyone sees something inaccurate here feel free to correct me)
- stcredzero 8y agoAlignment matter much more when using atomics. Someone should make a lockless library and call it, "Family." Most people will think it is a reference to the Fast and the Furious, but it would actually be a reference to Dune.
- userbinator 8y agoOn x86 the effects of misalignment haven't been "hefty" since at least the 486, when they first got cache and everything within a cacheline would be just as fast --- don't forget that x86 never had aligned instruction bytes either.
- stcredzero 8y agoIsn't data alignment/structure padding the sort of thing we ought to be putting into optimizing compilers? People used to allocate registers by hand. Also, isn't it a pretty strong signal that there's so much talk of cache oblivious algorithms and cache usage? As in an olfactory signal. If we are warping our algorithms and coding so much around it, isn't this a sign that the memory abstraction with the current caching scheme is an overly leaky abstraction? This, and the abuse of memory as a means of communicating between threads running on different CPU cores are two areas that seem very much like a signal that a paradigm shift of some kind is due on the level of the abstract model of the hardware.
- Retra 8y ago>If we are warping our algorithms and coding so much around it, isn't this a sign that the memory abstraction with the current caching scheme is an overly leaky abstraction? No, because we don't code our programs in the 'language of performance'. Performance of algorithms is always exterior to our abstractions. There's no way to talk about performance without talking about all the things that 'leak'. Conversely, if you design your algorithms with no consideration for performance, you will not care what the cache does. If you design your algorithms to maximize something they don't literally encode, then you're escaping the abstraction by definition, and side channels become relevant. In other words, the abstractions don't leak, they are just not up to the task of modeling optimal performance, so you have to use your human brain to do it on the side.
- stcredzero 8y agoPerformance of algorithms is always exterior to our abstractions. From a certain point of view. In other words, the abstractions don't leak, they are just not up to the task of modeling optimal performance If the performance becomes pathological, to the point where the app or system is unusable, then from the point of view of the users, the abstraction is leaking. Let me put it this way. Maybe a flat Random Access Machine isn't the best mental model for certain coding tasks? Maybe there's a way we could have a cleaner abstract model that makes the pathologies visible, yet is a bit more abstract than just thinking about the actual cache?