Definition · The paging problemA cache holds k pages. A trace is a sequence of page references. A reference to a resident page is a hit; a reference to an absent page is a miss, and the page is loaded, evicting one resident page if the cache is full. A policy is the rule for choosing which.
The lab opens on the trace 1 2 3 4 1 2 5 1 2 3 4 5 with three slots: twelve references over five distinct pages. FIFO evicts the page that has been resident longest and gets 3 hits, a rate of 1/4. LRU evicts the page unused for longest and gets 2, a rate of 1/6. Farthest-in-future gets 5, a rate of 5/12.
LRU doing worse than FIFO here is worth a moment. LRU is the better policy on most workloads and it is not a theorem; on this trace, which was built to embarrass FIFO's cache-size behaviour, LRU happens to lose by one hit. Two measurements on one trace, and neither is a ranking.
Definition · Farthest-in-futureOn a miss with a full cache, evict the resident page whose next reference is latest in the remaining trace; a page never referenced again is evicted first. This requires the whole trace in advance, so it is an offline policy.
Theorem · Farthest-in-future is optimalFor every trace and every cache size, no policy — online or offline — achieves more hits than farthest-in-future under demand paging.
The proof is an exchange argument of exactly the shape performed earlier on this course. Take any optimal schedule of evictions and the first point at which it differs from farthest-in-future; show that its choice can be replaced by farthest-in-future's without losing a hit, by tracking the two caches, which differ in at most one page, until they coincide again. Repeating drives the first difference forward, and the schedule becomes farthest-in-future's.
The lab does not run that argument. It does something stronger for the instance on screen: it searches every eviction decision a cache of this size could make, memoised on the pair of position and cache contents, and reports the maximum hits reachable. On the opening trace that search visits 43 distinct states and returns 5, which is what farthest-in-future got. So the optimality claim is checked here rather than cited, by a search that knows nothing about looking ahead.
Example · Where the online policies cannot reachOn the thirteen-reference trace 1 2 3 1 4 1 2 5 1 2 3 4 5 with three slots, FIFO and LRU both get 4 hits and farthest-in-future gets 6, confirmed by a search over 44 states. On the loop 1 2 3 4 repeated three times with three slots, FIFO and LRU both get 0 — each evicts precisely the page about to be used — while the offline optimum is 6 of 12, because on every miss it discards the page whose turn comes last rather than the one whose turn comes next.
Belady's anomaly, swept rather than described
The panel sweeps FIFO from one slot to six on whatever trace is loaded and prints the hit count at each size. On the opening trace that row reads 0, 0, 3, 2, 7, 7: going from three slots to four costs a hit. On the thirteen-reference trace it reads 0, 1, 4, 3, 8, 8, and the same step loses a hit again. This is Belady's anomaly, and the reader sees the row rather than the citation.
The same sweep under LRU on those two traces reads 0, 0, 2, 4, 7, 7 and 0, 1, 4, 5, 8, 8: never falling. That is not luck. LRU is a stack policy — the pages resident with k slots are always a subset of those resident with k + 1 at the same point — and a stack policy cannot suffer the anomaly. FIFO is not a stack policy, and the two rows above are what that difference costs.
The sweep the panel draws is always FIFO's, whichever policy is selected for the chart, and the column that reads whether a bigger cache ever does worse is about FIFO alone. On two of the four shipped traces it reads no, and a reader who took that as a statement about caching in general would have generalised from a policy to a field.
A caption to distrust
The trace 1 2 1 3 1 4 1 5 1 6 1 7 with two slots is captioned as one where every policy keeps the hot page and the differences between them disappear. The table above it reads FIFO 3, LRU 5, farthest-in-future 5. FIFO does not keep the hot page: it evicts by arrival order, and page 1 is reloaded and then evicted again on every cycle. The difference between the policies on that trace is two hits out of twelve, which is the largest relative gap between FIFO and LRU on any of the four.
Measured on this page: 12 references, 3 slots, FIFO 3, LRU 2, farthest-in-future 5, offline optimum 5 over 43 states, and a FIFO sweep of 0, 0, 3, 2, 7, 7. Proved on this page: farthest-in-future is optimal on every trace and every cache size. Not proved anywhere in this library: any bound on how far an online policy can fall behind it, which is the competitive analysis this course names and does not develop.