Resource Augmentation: k Pages Against h
The lower bound compares an online cache with an offline optimum that has the same cache size. That comparison is harsh: knowing the future is worth a lot. A gentler and often more informative question is: how much more memory does an online algorithm need to compete with OPT? Sleator and Tarjan already asked it in their 1985 paper. With slots online against OPT with slots, the answer is the ratio : double the cache and LRU is within a factor 2 of the optimal smaller cache. This idea, comparing against a weaker optimum, is called resource augmentation, and it became a general method in scheduling and beyond (Kalyanasundaram and Pruhs, 2000).
The upper bound
Write for the optimal offline cost with slots and for the online algorithm with slots.
Theorem (Sleator and Tarjan, 1985). Every marking or conservative algorithm (LRU, FIFO, CLOCK, FWF) satisfies, for every and every , with the same initial cache contents,
Proof
Use the -phases of from LRU and FIFO Are k-Competitive: each phase has distinct pages, except perhaps the last. As before, misses per phase, so for phases.
For phase , let be its first request and let the window run from the second request of phase to the first request of phase . The window requests at least distinct pages other than . After serving , holds and at most other pages, so at least of those pages are missing and each causes a miss when first requested in the window. Windows are disjoint, so and
For this is the familiar .
The matching lower bound
Theorem (Sleator and Tarjan, 1985). For every deterministic online algorithm with slots and every , there are arbitrarily long sequences with .
The adversary is the cruel one from the lower bound for deterministic paging: with pages it always requests the page missing from the online cache, so the online algorithm misses on every request. The work is in showing that misses on at most a fraction of such a sequence (up to an additive term); we do not reproduce that part of the proof and check it numerically instead. The program generates the cruel sequence for LRU (which is simply the cycle repeated) and for FIFO and a hashed rule, then computes with Belady's rule for every .
def opt_misses(reqs, h):
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
cache, misses = {}, 0
for t, p in enumerate(reqs):
if p not in cache:
misses += 1
if len(cache) == h:
del cache[max(cache, key=cache.get)]
cache[p] = nxt[t]
return misses
def cruel(k, n, evict):
cache, loaded, reqs = [], {}, []
for t in range(n):
page = next(p for p in range(k + 1) if p not in cache)
reqs.append(page)
if len(cache) == k:
cache.remove(evict(cache, loaded, t))
cache.append(page)
loaded[page] = t
return reqs
rules = {
"LRU/FIFO": lambda c, loaded, t: min(c, key=lambda p: loaded[p]), # every request misses, so LRU = FIFO
"hashed": lambda c, loaded, t: sorted(c)[(t * 40503) % 97 % len(c)],
}
k, n = 8, 4000
print(f"k = {k}, n = {n} requests, the online algorithm misses on all of them")
print(" h OPT_h n/OPT_h (LRU/FIFO) n/OPT_h (hashed) k/(k-h+1)")
for h in [1, 2, 4, 6, 7, 8]:
ratios = [n / opt_misses(cruel(k, n, rule), h) for rule in rules.values()]
print(f"{h:2} {opt_misses(cruel(k, n, rules['LRU/FIFO']), h):6} {ratios[0]:18.3f} {ratios[1]:16.3f} {k / (k - h + 1):9.3f}")k = 8, n = 4000 requests, the online algorithm misses on all of them
h OPT_h n/OPT_h (LRU/FIFO) n/OPT_h (hashed) k/(k-h+1)
1 4000 1.000 1.000 1.000
2 3501 1.143 1.729 1.143
4 2503 1.598 3.571 1.600
6 1505 2.658 6.182 2.667
7 1006 3.976 9.501 4.000
8 507 7.890 18.605 8.000
For every the ratio against LRU and FIFO matches up to start-up effects; the hashed rule does worse, as non-marking rules may.
Reading the bound
The function falls quickly as the online cache grows:
from math import ceil
print(" h k = h: ratio k = 2h: ratio k for ratio <= 1.1 k for ratio <= 1.01")
for h in [4, 16, 64, 256, 1024]:
r1 = h / 1
r2 = 2 * h / (h + 1)
k11 = ceil(1.1 * (h - 1) / 0.1) # k/(k-h+1) <= 1 + eps <=> k >= (1 + eps)(h - 1)/eps
k101 = ceil(1.01 * (h - 1) / 0.01)
print(f"{h:4} {r1:12.0f} {r2:13.3f} {k11:18} {k101:19}") h k = h: ratio k = 2h: ratio k for ratio <= 1.1 k for ratio <= 1.01
4 4 1.600 33 303
16 16 1.882 165 1515
64 64 1.969 694 6363
256 256 1.992 2805 25755
1024 1024 1.998 11254 103323
With the guarantee is the useless factor ; with it is below 2 for every . Getting within 10 percent of OPT requires about slots, and within 1 percent about : the bound is still pessimistic, since real traces with locality behave much better, but it explains why a modest amount of extra memory removes the worst-case gap between online and offline caching.
In C++
LRU with slots against Belady with slots on a seeded trace with locality, for . The measured ratio is far below the worst-case bound , and for small caches LRU with twice the memory even beats the smaller optimal cache.
#include <cstdio>
#include <list>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>
int lru(const std::vector<int>& r, int k) {
std::list<int> order;
std::unordered_map<int, std::list<int>::iterator> pos;
int misses = 0;
for (int p : r) {
auto it = pos.find(p);
if (it != pos.end()) order.erase(it->second);
else { ++misses; if ((int)pos.size() == k) { pos.erase(order.back()); order.pop_back(); } }
order.push_front(p);
pos[p] = order.begin();
}
return misses;
}
int belady(const std::vector<int>& r, int h) {
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 t = 0; t < n; ++t) {
if (key.count(r[t])) s.erase({key[r[t]], r[t]});
else { ++misses; if ((int)key.size() == h) { 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() {
std::mt19937 rng(31);
std::vector<int> r;
int page = 0;
for (int t = 0; t < 50000; ++t) { // a random walk with occasional jumps
unsigned x = rng() % 100;
if (x < 3) page = rng() % 200;
else page = (page + (x % 5) - 2 + 200) % 200;
r.push_back(page);
}
std::printf(" h OPT_h LRU_2h LRU_2h/OPT_h bound 2h/(h+1)\n");
for (int h : {2, 4, 8, 16, 32}) {
int o = belady(r, h), l = lru(r, 2 * h);
std::printf("%4d %6d %7d %12.3f %14.3f\n", h, o, l, double(l) / o, 2.0 * h / (h + 1));
}
} h OPT_h LRU_2h LRU_2h/OPT_h bound 2h/(h+1)
2 27180 22096 0.813 1.333
4 18897 16124 0.853 1.600
8 14384 14001 0.973 1.778
16 11568 12601 1.089 1.882
32 8802 10069 1.144 1.939
In practice: sizing a cache
System designers rarely ask "how close is LRU to OPT for a fixed cache?". They ask "how large must the cache be for a target miss rate?", and they answer it with a miss-ratio curve: the miss rate of LRU as a function of the cache size, which for LRU (a stack algorithm) can be computed for all sizes in one pass over a trace (Mattson, Gecsei, Slutz and Traiger, 1970). Resource augmentation is the worst-case version of the same question: it compares the online curve at size with the optimal curve at size . The C++ experiment above shows the typical picture: on a trace with locality the online curve at stays close to the offline curve at (within 15 percent in that experiment, and below it for small caches), far better than the worst-case factor, and the worst case only appears on loops slightly larger than the cache.
Beyond paging
Resource augmentation compares an online algorithm with a handicapped optimum: a smaller cache, slower machines, or fewer servers. It explains why simple heuristics work well in practice despite bad competitive ratios, and it is the standard analysis tool for online scheduling with speed augmentation (Kalyanasundaram and Pruhs, 2000; Phillips, Stein, Torng and Wein, 2002). For randomized paging the effect is similar: Young (1991) showed that the marking algorithm with slots is roughly -competitive against for , and that no randomized algorithm beats about .
Test yourself
LRU has slots, OPT has . What competitive ratio does the theorem give?
In the proof, how many misses does make at least in each window?
- 1
Why is it fair to say that doubling the cache makes LRU nearly optimal in the worst case?
- With , LRU is -competitive against the optimum with slots
- With , LRU equals OPT
- Doubling the cache halves the misses
- LRU becomes randomized