Sub topic 5
The Doubly Linked List: Cheap Reordering, Slow Lookup
This chapter assumes you know Python classes, Big-O notation, and that variables hold references to objects. The previous chapter showed that a hash map finds a key fast but keeps no usable order. Here you will see what the doubly linked list contributes: it can reorder entries cheaply, but it can't find them quickly.
Why the cache needs a list
An LRU cache must always know which entry was used least recently. That order changes on every get, because the entry you just read becomes the most recent. The structure that stores the order therefore has to support frequent moves.
How a doubly linked list works
A doubly linked list is a chain of nodes. Each node stores its data and two references: prev points to the node before it, and next points to the node after it. Nodes are separate objects, so none of them needs to sit next to another in memory.
In an LRU cache, one end of the list holds the most recent entry and the other end holds the least recent one.
Cheap reordering
Suppose you already hold a reference to node B. To remove it, you connect its two neighbors to each other:
- Set
B.prev.nexttoB.next. - Set
B.next.prevtoB.prev.
That is two assignments, whatever the list length. The prev pointer is what makes this possible. In a singly linked list you would have to walk from the start to find the node before B, which costs O(n). Adding a node at the front is also a fixed number of assignments, so moving a node to the front is O(1).
Slow lookup
The list has no index and no hashing. To find the node with a given key, you start at one end and follow next until you hit it. That is O(n) in the worst case.
The program below shows both costs. It unlinks a middle node by reference, then counts the steps needed to find the last key in a longer chain.
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.prev = None
self.next = None
def link(a, b):
a.next = b
b.prev = a
def unlink(node):
node.prev.next = node.next
node.next.prev = node.prev
node.prev = None
node.next = None
def find(start, key):
steps = 0
node = start
while node is not None:
steps += 1
if node.key == key:
return node, steps
node = node.next
return None, steps
a, b, c = Node(1, "a"), Node(2, "b"), Node(3, "c")
link(a, b)
link(b, c)
unlink(b)
print(a.next.key, c.prev.key) # 3 1
nodes = [Node(i, i) for i in range(1000)]
for left, right in zip(nodes, nodes[1:]):
link(left, right)
_, steps = find(nodes[0], 999)
print(steps) # 1000The unlink touches two neighbors. The search touches every node.
Summary of costs
| Operation | Cost on a doubly linked list |
|---|---|
| Remove a node you hold a reference to | O(1) |
| Insert at the front | O(1) |
| Remove the last node (with a tail reference) | O(1) |
| Find a node by key | O(n) |
The list is good at everything except finding things, and the hash map is the reverse. The next chapter joins them.
Exercise
A cache holds 10,000 entries in a doubly linked list only, with no hash map. A get for a key that sits near the back finds the node and moves it to the front. Which step is slow, and which step is fast?
Show answer
Finding the node is slow, because the list must be walked from one end, which is O(n), about 10,000 steps here. Moving the node to the front is fast, because it takes a fixed number of pointer updates, O(1), once you hold the node.