Sub topic 5
Arrays versus linked structures
This chapter assumes you know how a cache line moves data (64 bytes is common) and what spatial locality means. It also assumes you can read C and know how arrays, linked lists and pointers work.
Two layouts, one answer
Both functions below return the same sum. They differ in how they reach each value.
#include <stddef.h>
struct Node {
int value;
struct Node *next;
};
long sum_list(const struct Node *head) {
long s = 0;
for (const struct Node *n = head; n != NULL; n = n->next)
s += n->value;
return s;
}
long sum_array(const int *values, size_t n) {
long s = 0;
for (size_t i = 0; i < n; i++)
s += values[i];
return s;
}Big-O treats them as equal: both are O(n). On real hardware the array version is often several times faster once the data is larger than the cache.
Why the array wins
An int is 4 bytes, so one 64-byte cache line holds 16 consecutive array elements. One miss loads 16 values, and the next 15 accesses hit.
A list node on a 64-bit system is 16 bytes after padding, plus allocator overhead. Nodes allocated at different times may sit far apart in memory. Each node can land in a different cache line, so you may pay one miss per element and use only 4 of the 64 bytes you loaded.
The list has a second problem. The CPU cannot know the address of the next node until the current node has been loaded. This is a dependent load chain. Each miss must finish before the next one starts, so the miss latencies add up. In the array loop, addresses are computed from i alone. The CPU can issue many loads at once, and the hardware prefetcher can detect the sequential pattern and fetch ahead.
Where the difference shows
| Property | Array | Linked list |
|---|---|---|
| Elements per cache line | Many | Often one |
| Next address known early | Yes | No, must load the node |
| Prefetcher helps | Yes | Rarely |
| Insert in the middle | Shifts elements, O(n) | O(1) once you hold the node |
| Extra memory per element | None | Pointer plus allocator overhead |
Common errors
- Choosing a list because "insertion is O(1)". Finding the insertion point is O(n) and slow, so a vector with a shifting insert often wins for small and medium sizes.
- Benchmarking with a tiny list. If everything fits in L1, the difference is hidden. Test with data larger than your last-level cache.
- Assuming a freshly built list is scattered. Nodes built in one loop may be adjacent at first, and then become scattered after many inserts and deletes. Measure the layout you actually have.
Decision rule
Default to a contiguous array (or std::vector, a Rust Vec) for sequential access. Use pointer-based structures when you need stable addresses, cheap splicing of nodes you already hold, or very large elements. Even then, consider a pool allocator or indices into an array instead of raw pointers.
Exercise
A list node holds a 4-byte int and an 8-byte pointer, padded to 16 bytes. With 64-byte cache lines, how many nodes fit in one line if they are contiguous, and why can a scattered list still need one miss per node?
Show answer
Four nodes fit in one line (64 / 16). If nodes are scattered, each node sits in a different cache line, so each access misses and the other bytes in the line go unused. The next address is also unknown until the current node loads, so the misses cannot overlap.