r/cpp • Meeting C++ | C++ Evangelist • Sep 24 '26

About alignment, struct layout and the cache...

https://meetingcpp.com/blog/items/About-alignment--struct-layout-and-the-cache---.html
36 Upvotes

27 comments sorted by

View all comments

Show parent comment

2

u/CantThinkOfAnyName Sep 26 '26

Hey, just found this comment and I was intrigued by it since I always thought it's the pointer indirection and the cache miss it incurs that makes list perform bad.

Can you explain more about what you mean that it's the data dependency issue?

What I understood is this:

  1. You can't have random access iterator, so every time you access an element you have to do a search. Even if you had all elements in contiguous memory, you still have to traverse it, otherwise you would just have std::vector
  2. Every element has now higher size since it needs the pointer, thus cache is filled with "useless" data only needed for traversal, also harder to align with cache line sizes perhaps?
  3. You need to actually load the element to get the address of the next one, perhaps causing a wait, but wouldn't speculative execution essentially eliminate this?

But I feel like I'm missing something here, thanks in advance!

3

u/ReDucTor Game Developer | quiz.cpp-perf.com Sep 26 '26

The data dependency is the repeated pointer indirection, its a terminology used a little more for the CPU interaction and one thing depending on another, in the case of the linked list iteration each iteration step depends on loading the previous as a loop carried dependency.

This data dependency is what makes the cache miss be significantly worse and even with an L1 cache hit makes it pretty bad.

Imagine you have A -> B -> C

If you iterate the list and A, B and C are in the L1 cache, and it takes 5 cycles for each L1 hit then the best latency for this is going to be 15 cycle latency (3 nodes * 5 L1 cycles) best case, the CPU cannot do anything out of order for it, the frontend will still issue the instructions for each iteration but those will sit waiting on the data dependency

However if you have [A, B, C]

And do the same iteration and those are all in the L1 cache, if you assume the CPU frontend can dispatch one iteration each cycle and L1 hit of 5 cycles then you could have a best case of 8 cycles latency (3 cycles to start last iteration + 5 cycles L1 hit)

And to take another comparison of [A*, B*, C*]

Iteration again and all in L1 cache, then its a similar thing but two loads instead of one so best case 13 cycles latency (3 cycles to last iteration + 5 cycles load address + 5 cycles load data)

Where this stands out is if you compare list A -> B -> C and vector [A*, B*, C*] where A and B dont exist in any cache and need to hit main memory with 200 cycles, and C in L1 then the linked list is going to be best case 405 cycles (200 main memory load for A + 200 main memory load for B + 5 main memory load for C), where the vector it would be best case 201 cycles (A, B, and C all run at once, assuming one cycle delay between each dispatching A can take 200 cycles load, B can take 1 cycle until its iteration and 200 load so 201 cycles, C can take 2 cycles until its iteration then 5 for L1 hit, as B is the worst latency it is the best case it could be done)

To put it simply iterating N items in a linked list is N*latency, while iterating an array of N items is N+latency

The reason I didn't mention the front end dispatch for the linked list is that the dispatch isnt really relevant as even with the 1 cycle to dispatch isnt stopping that next iteration its the load latency which is longer then 1 cycle.

This is still very much a simplification but hopefully helps you understand.