The Lower Bound k for Deterministic Paging
LRU and FIFO Are k-Competitive gave an upper bound of for a large class of algorithms. This lesson proves that no deterministic online algorithm does better, with care about the details that the quick version in Part I skipped: why the bound is non-strict, why it needs only pages, why giving the algorithm a glimpse of the future (lookahead) does not help, and what the bound says about real caches. Throughout, ratios are for deterministic algorithms, so all adversaries coincide (Oblivious and Adaptive Adversaries).
The theorem
Theorem (Sleator and Tarjan, 1985). Let ALG be any deterministic online paging algorithm with a cache of pages, and let the universe have at least pages. For every there is a request sequence of length with
Hence ALG is not -competitive for any , even in the non-strict sense.
Proof
Use only pages and assume both caches start full with the same pages (an empty start only adds compulsory misses). The cruel adversary simulates ALG and always requests the unique page of that is not in ALG's cache. Every request is a miss for ALG, so .
Now bound OPT by the cost of Belady's rule (LFD) on the same . When LFD misses at time , it evicts the cached page whose next request is furthest away. The universe has pages, so at time the cache holds all of them except the evicted one, say . Each of the cached pages other than and the requested page is needed again before , since was the furthest. The next miss of LFD is the next request to , so between two consecutive misses there are at least hits, and LFD misses at most once every requests: plus the start-up misses.
Since as , an inequality for all forces : the bound holds for the non-strict ratio.
Three remarks on the proof.
- The adversary needs the algorithm, not just its class. It computes ALG's cache at every step; that is why the bound is for deterministic algorithms. Against a randomized algorithm, the adversary does not know which page is missing, and the bound drops to (The Harmonic Lower Bound).
- pages suffice. A larger universe does not help the adversary, and with only pages every algorithm is trivially optimal.
- The upper and lower bounds match exactly. LRU's and the adversary's against leave no gap in the leading constant.
The adversary at work
The program runs the cruel adversary against five deterministic algorithms, including CLOCK (second chance, the approximation of LRU used by operating systems) and a rule that evicts a page chosen by a fixed hash of the time step, which looks random but is deterministic. None of them gets below .
def make_algorithms(k):
def lru(state, cache, t):
return min(cache, key=lambda p: state["last"][p])
def fifo(state, cache, t):
return min(cache, key=lambda p: state["loaded"][p])
def mru_like(state, cache, t): # most recently loaded: terrible
return max(cache, key=lambda p: state["loaded"][p])
def hashed(state, cache, t): # deterministic, but looks arbitrary
return sorted(cache)[(t * 2654435761) % 2 ** 32 % len(cache)]
def clock(state, cache, t):
hand, ring, ref = state["hand"], state["ring"], state["ref"]
while ref[ring[hand]]:
ref[ring[hand]] = False
hand = (hand + 1) % k
state["hand"] = hand
return ring[hand]
return {"LRU": lru, "FIFO": fifo, "CLOCK": clock, "hashed": hashed, "MR-loaded": mru_like}
def cruel_adversary(evict, k, n):
cache = list(range(k))
state = {"last": {p: p for p in cache}, "loaded": {p: p for p in cache},
"hand": 0, "ring": list(range(k)), "ref": {p: True for p in range(k + 1)}}
reqs = []
for t in range(k, k + n):
page = next(p for p in range(k + 1) if p not in cache) # the missing page
reqs.append(page)
victim = evict(state, cache, t)
slot = cache.index(victim)
cache[slot] = page
state["ring"][state["ring"].index(victim)] = page
state["ref"][page] = True
state["last"][page] = state["loaded"][page] = t
return reqs
def opt_misses(reqs, k, start):
n = len(reqs)
nxt, seen = [0] * n, {}
for t in range(n - 1, -1, -1):
nxt[t] = seen.get(reqs[t], n + reqs[t])
seen[reqs[t]] = t
first = {p: seen.get(p, n + p) for p in start} # next use of the start pages
cache, misses = dict(first), 0
for t, p in enumerate(reqs):
if p not in cache:
misses += 1
del cache[max(cache, key=cache.get)]
cache[p] = nxt[t]
return misses
k = 5
print(f"k = {k}: every request misses for ALG; ratio = n / OPT")
print("algorithm n=100 n=1000 n=10000")
for name, rule in make_algorithms(k).items():
row = []
for n in [100, 1000, 10000]:
reqs = cruel_adversary(rule, k, n)
row.append(n / opt_misses(reqs, k, range(k)))
print(f"{name:10} " + " ".join(f"{x:7.3f}" for x in row))k = 5: every request misses for ALG; ratio = n / OPT
algorithm n=100 n=1000 n=10000
LRU 5.000 5.000 5.000
FIFO 5.000 5.000 5.000
CLOCK 5.000 5.000 5.000
hashed 8.333 9.009 9.074
MR-loaded 100.000 1000.000 10000.000
LRU, FIFO and CLOCK sit at exactly , as the matching upper bound promises for marking and conservative algorithms. The other two rules are worse: the hashed rule settles near 9, and against the most-recently-loaded rule the adversary alternates between two pages that OPT simply keeps, so its ratio grows without bound. The sequence is different for each algorithm, which is the point: the adversary is tailored to its victim, and the theorem says only that no victim escapes below .
In C++
The cruel adversary against CLOCK, written as a standalone simulation: CLOCK keeps a reference bit per frame and a hand that sweeps the frames, clearing bits until it finds a frame whose bit is 0. Belady's rule on the generated sequence gives OPT.
#include <cstdio>
#include <set>
#include <unordered_map>
#include <vector>
int belady(const std::vector<int>& r, int k, const std::vector<int>& start) {
int n = r.size(), misses = 0;
std::vector<int> nxt(n);
std::unordered_map<int, int> seen;
for (int t = n - 1; t >= 0; --t) { nxt[t] = seen.count(r[t]) ? seen[r[t]] : n + r[t]; seen[r[t]] = t; }
std::set<std::pair<int, int>> s;
std::unordered_map<int, int> key;
for (int p : start) { int nu = seen.count(p) ? seen[p] : n + p; key[p] = nu; s.insert({nu, p}); }
for (int t = 0; t < n; ++t) {
if (key.count(r[t])) s.erase({key[r[t]], r[t]});
else { ++misses; auto f = std::prev(s.end()); key.erase(f->second); s.erase(f); }
key[r[t]] = nxt[t];
s.insert({nxt[t], r[t]});
}
return misses;
}
int main() {
for (int k : {2, 4, 8, 16}) {
std::vector<int> frame(k), start(k);
std::vector<bool> ref(k, true);
for (int i = 0; i < k; ++i) frame[i] = start[i] = i;
int hand = 0, n = 20000;
std::vector<int> reqs;
for (int t = 0; t < n; ++t) {
std::vector<bool> cached(k + 1, false);
for (int p : frame) cached[p] = true;
int page = 0;
while (cached[page]) ++page; // the page CLOCK lacks
reqs.push_back(page);
while (ref[hand]) { ref[hand] = false; hand = (hand + 1) % k; }
frame[hand] = page; // evict and load
ref[hand] = true;
hand = (hand + 1) % k;
}
int opt = belady(reqs, k, start);
std::printf("k = %2d: CLOCK %d misses, OPT %d, ratio %.3f\n", k, n, opt, double(n) / opt);
}
}k = 2: CLOCK 20000 misses, OPT 10000, ratio 2.000
k = 4: CLOCK 20000 misses, OPT 5000, ratio 4.000
k = 8: CLOCK 20000 misses, OPT 2500, ratio 8.000
k = 16: CLOCK 20000 misses, OPT 1250, ratio 16.000
Lookahead does not help
Suppose the algorithm may see the next requests before serving the current one. The adversary defeats this by repeating every request times in a row: the repetitions are hits for everybody, and the lookahead window shows only copies of the current page, so the algorithm learns nothing it can use. The ratio still tends to (Borodin and El-Yaniv discuss this; Albers, 1997, studies a "strong lookahead" model in which the window must contain distinct pages, and there lookahead does help).
def lookahead_misses(reqs, k, ell):
"""Evict a cached page not seen in the next ell requests (LRU among them), else LRU."""
cache, last, misses = [], {}, 0
for t, p in enumerate(reqs):
if p not in cache:
misses += 1
if len(cache) == k:
window = set(reqs[t + 1:t + 1 + ell])
unseen = [q for q in cache if q not in window] or cache
cache.remove(min(unseen, key=lambda q: last[q]))
cache.append(p)
last[p] = t
return misses
def adversary_with_repeats(k, ell, blocks):
"""Requests the page missing from the algorithm's cache, each one ell + 1 times in a row."""
reqs = []
for _ in range(blocks):
cache = simulate_cache(reqs, k, ell)
page = next(p for p in range(k + 1) if p not in cache)
reqs += [page] * (ell + 1)
return reqs
def simulate_cache(reqs, k, ell):
cache, last = [], {}
for t, p in enumerate(reqs):
if p not in cache:
if len(cache) == k:
window = set(reqs[t + 1:t + 1 + ell])
unseen = [q for q in cache if q not in window] or cache
cache.remove(min(unseen, key=lambda q: last[q]))
cache.append(p)
last[p] = t
return cache
def opt_misses(reqs, k):
cache, misses = set(), 0
for t, p in enumerate(reqs):
if p not in cache:
misses += 1
if len(cache) == k:
rest = reqs[t + 1:]
cache.remove(max(cache, key=lambda q: rest.index(q) if q in rest else len(rest)))
cache.add(p)
return misses
k = 4
for ell in [0, 3, 10]:
reqs = adversary_with_repeats(k, ell, 160)
alg, opt = lookahead_misses(reqs, k, ell), opt_misses(reqs, k)
print(f"lookahead {ell:2}: {len(reqs):4} requests, ALG {alg}, OPT {opt}, ratio {alg / opt:.2f}")lookahead 0: 160 requests, ALG 160, OPT 43, ratio 3.72
lookahead 3: 640 requests, ALG 160, OPT 43, ratio 3.72
lookahead 10: 1760 requests, ALG 160, OPT 43, ratio 3.72
With 160 distinct blocks the ratio is for every lookahead, on its way to as the number of blocks grows. A subtlety: the adversary must know the algorithm's eviction before choosing the next distinct page, and the lookahead algorithm's eviction depends on that future page. With repeats of length , the window at a miss shows only copies of the current page, so the eviction is fixed before the adversary moves on, and the construction is consistent.
What the bound says about real caches
The factor is a statement about adversarial sequences. Real request streams have locality of reference, and the competitive ratio hides that LRU usually beats FIFO in practice even though both have ratio exactly . Several refined models capture locality:
- Access graphs (Borodin, Irani, Raghavan and Schieber, 1995): requests must follow the edges of a known graph, as a program's page references follow its control flow. The best ratio then depends on the graph, and on a path or a tree LRU is much better than .
- Diffuse adversaries (Koutsoupias and Papadimitriou, 2000): the adversary chooses a distribution from a restricted class rather than a single sequence.
- Working-set models (Albers, Favrholdt and Giel, 2005): sequences are characterized by how many distinct pages appear in windows of each length, and LRU is optimal among deterministic algorithms in this model.
Test yourself
How many distinct pages does the cruel adversary need against a cache of size ?
- Unboundedly many
Why is the deterministic lower bound a bound on the non-strict ratio?
- Because the sequences are short
- Because grows without bound with , so no additive constant can absorb the gap
- Because OPT is zero
- Because LRU is strictly -competitive
In the proof, why does Belady's rule miss at most once every requests on the adversary's sequence?
- Because the requests are random
- Because the evicted page is requested only after all other cached pages, and with pages only that page can cause the next miss
- Because Belady never misses
- Because the adversary repeats pages
How does the adversary neutralize a lookahead of requests?
- It cannot; lookahead always helps
- It repeats every request times, so the window shows only the current page
- It requests new pages
- It uses pages