Memory Hierarchy

Why does traversing the linked list above tend to be much slower than reading an array with the same number of elements in order?

struct node { int val; struct node *next; };
for (p = head; p != NULL; p = p->next)
    sum += p->val;
  1. AThe next element's address is unknown until the current one is read, so misses occur one after another
  2. BLinked-list elements are, by rule, never placed in the cache
  3. CThe CPU's branch prediction always fails in code that uses pointers
Show answer and explanation

Answer: A. The next element's address is unknown until the current one is read, so misses occur one after another

In a linked list the next element's address is known only after reading p->next. If elements are scattered in memory, each access misses, and since the next address is unknown, misses cannot overlap, so the full memory latency piles up element by element. With an array the next address can be computed, so prefetching and overlapping misses work. Linked-list elements are cached like any data, and the loop branch is predictable rather than always mispredicted.

  • A. Correct. The next address is unknown, so miss latencies accumulate one by one.
  • B. Linked-list elements are cached like any other data.
  • C. The loop branch is regular, and prediction does not always fail.
Practice this topic Back to topic

More Memory Hierarchy questions

  1. Each miss fetches a 64-byte line and memory latency is 100 ns. With only one miss outstanding at a time, read bandwidth is about 0.64 GB/s. If 10 misses can be outstanding at once, what is the bandwidth approximately (assuming no other limits)?
  2. When writing out a large amount of data that will not be read back for a while, what is the main benefit of non-temporal stores that bypass the cache?
  3. If the cache hit rate is 95%, what is the miss rate?
  4. Which technique, when fetching a missed line from memory, sends the word the CPU needs first and the rest afterward?
  5. Why does fusing the two loops above into one (loop fusion) tend to be faster when N is large?