---
title: "Optimal Prepaging and Font Caching"
authors:
  - "David R. Fuchs"
  - "Donald E. Knuth"
year: 1985
venue: "ACM Transactions on Programming Languages and Systems 7(1), 62–79, January 1985 (earlier version: Stanford report STAN-CS-82-901, \"Optimal Font Caching\", March 1982)"
arxiv: null
doi: "10.1145/2363.2367"
source: "https://dl.acm.org/doi/pdf/10.1145/2363.2367"
version_reviewed: "Stanford technical report STAN-CS-82-901 (March 1982, OCR scan from the bitsavers Stanford CS-TR collection), read in full; the 1985 TOPLAS version was checked only through its abstract and opening pages (dl.acm.org returns 403 to the box), which match the report"
topics:
  - "cache-replacement"
  - "prefetching"
  - "beladys-min"
  - "lookahead"
  - "glyph-caching"
  - "typesetting"
  - "priority-queues"
seed_rank: 2752
seed_batch: "archive-2026-11-30"
reviewed: "2026-11-30"
pool: "systems"
relevance_score: 8
cites:
  - title: "Optimal Prepaging and Font Caching"
    url: "https://doi.org/10.1145/2363.2367"
    year: 1985
    arxiv: null
    doi: "10.1145/2363.2367"
  - title: "Optimal Font Caching (Stanford report STAN-CS-82-901)"
    url: "https://mirrors.meulie.net/bitsavers.org/pdf/stanford/Stanford_CS_TR_Collection_2025-12-12/PDF/1982/CS-TR-82-901.pdf"
    year: 1982
    arxiv: null
    doi: null
  - title: "A Study of Replacement Algorithms for a Virtual-Storage Computer"
    url: "https://doi.org/10.1147/sj.52.0078"
    year: 1966
    arxiv: null
    doi: "10.1147/sj.52.0078"
  - title: "Evaluation Techniques for Storage Hierarchies"
    url: "https://doi.org/10.1147/sj.92.0078"
    year: 1970
    arxiv: null
    doi: "10.1147/sj.92.0078"
---

# Optimal Prepaging and Font Caching

## One-sentence takeaway

Fuchs and Knuth extend Belady's MIN to a cache that may also be loaded *ahead* of need. Glyphs are locked while a line is being set, and a limited number of free "get" slots between lines can prefetch. The optimal policy is short: on a fault or prefetch, evict the dead glyph or else the one used furthest in the future, and prefetch the missing glyph needed soonest unless that would displace something needed first. They prove this minimizes faults, run it in O(m + n log s) with a priority queue, and drove Stanford's Alphatype phototypesetter with it.

## Why it matters here

It is a worked recipe for any cache where the future is partly known and there is idle bandwidth to fill it. That covers Anoptic's glyph atlas for scanline text (the layout pass knows which glyphs the next lines need), streaming texture or meshlet residency when the next frames' visible sets are predicted, and the agent-serving prefix/KV cache when a planner's next tool calls and their prompts are already queued. The model's three commands map directly onto these cases. *Lock* is "pinned while the current batch uses it". *Unlock* is "batch done". *Get* is "a free transfer slot this tick". The proof says to spend each slot on the soonest-needed missing item, never on anything else. Those mappings are this card's reading; the paper's own extension is a speculative paragraph about compiler-directed cache preloading for pipelined arithmetic units.

## Key ideas

- **Model.** A job is a sequence of `L(i)` (lock glyph i; a *fault* if it is absent), `U(i)` (unlock) and `G` (optionally bring in any one glyph, at no fault cost, because the device is busy setting the previous line). A line is `L(c1)…L(cn) G^k U(c1)…U(cn)`. At most s glyphs can be wedged (locked) at once. The page-reference model is the special case with no `G` and every `L` immediately followed by `U`.
- **Optimal strategy S.** (1) When bringing a glyph in, use an empty slot, else evict an unwedged glyph that never appears again, else the unwedged glyph whose next `L` is latest. (2) On `G`, bring in the absent glyph whose next `L` is soonest, unless every unwedged resident glyph will be locked before that `L`, or nothing is missing. A trace-completion exchange argument shows S has no more faults than any other trace at every point.
- **Data structures.** Arrays `first[i]` and `next[q]` hold next-use positions, and a pointer `q` runs ahead of the command pointer `p` to find the next glyph to prefetch. Unwedged resident slots sit in a priority queue keyed by next use. Cost is O(m + n log s), close to linear when many `G` slots keep `q` far ahead. More `G` commands make the search space bigger but the algorithm *faster*.
- **Bounded lookahead.** For on-line use, commands sit in a ring of n₀ entries with keys compared circularly. Slots split into Q₀ (empty or dead, unordered) and Q₁ (a heap by next use), so dead glyphs are evicted for free.
- **Evidence (Table A, 60 runs).** The test page was Knuth's TeX debugging text: 5,211 characters, about 578 distinct glyph/size pairs. Runs used caches of 50–150 glyphs, 0, 4.5 or 6 prefetches per line, and four lookahead modes: full, 12 lines, 6 lines, and full with deliberately pessimal replacement. With no prefetch and a 150-glyph cache, full lookahead faults 614 times against a compulsory floor of about 576, versus 1,819 with pessimal replacement. At 4.5 prefetches the same cache drops to 9 faults, and at six prefetches with 125–150 glyphs it reaches 0. Production ended near 150 slots, where "faults hardly ever occur".
- **Prefetch dominates eviction.** With enough `G` slots, even the pessimal replacement order did as well as the optimal one (G6, C150). The prefetch half of the algorithm was still optimal and made the eviction choice immaterial. A 6-line window hurt more, because it starves the prefetcher too. In one case (C100, G6) the 12-line window beat full lookahead (0 faults against 3), through luck in variable-size allocation.
- **Practice beat the model.** Real glyphs vary in size, and optimal dynamic storage allocation is already NP-complete, so the production code ignored size and evicted by priority alone. It still worked well, and the theory served as a guide rather than a spec.

## Caveats

The strategy minimizes *faults*, not the number of transfers. The paper's own two-glyph example shows S can send more glyphs than necessary, which matters if bandwidth rather than stalls is the cost. Optimality needs the future command sequence; with bounded lookahead it is optimal only within the window. The theory assumes uniform-size slots, and the variable-size production version is a heuristic with no bound. I read the 1982 report (OCR scan; some table cells are garbled, so the numbers above are the clearly legible ones), not the 1985 journal text, which may differ in detail. The glyph-atlas, residency and KV-cache bridges are this card's reading.

## Links

- DOI (TOPLAS 1985): https://doi.org/10.1145/2363.2367
- ACM PDF: https://dl.acm.org/doi/pdf/10.1145/2363.2367
- Stanford report STAN-CS-82-901, "Optimal Font Caching" (scan, as read): https://mirrors.meulie.net/bitsavers.org/pdf/stanford/Stanford_CS_TR_Collection_2025-12-12/PDF/1982/CS-TR-82-901.pdf
- Same report, OCR'd PDF: https://mirrors.meulie.net/bitsavers.org/pdf/stanford/Stanford_CS_TR_Collection_2025-12-12/OCR/CS-TR-82-901-ocr.pdf
