Which item will be evicted next using farthest-in-future schedule?
d
e
c
a
cache
b
d
b
y
a
c
d
b
c
a
e
d
e
c
a
f
?
?
?
?
c
d
a
e
a
c
reduced eviction schedules
A reduced schedule is a schedule that brings an item \(d\) into the cache in step \(j\) only if there is a request for \(d\) in step \(j\) and \(d\) is not already in the cache.
an unreduced schedule:
cache
a
a
b
c
a
a
b
c
c
a
d
c
d
a
d
c
a
a
c
b
b
a
c
b
c
a
c
b
d
d
c
b
d
d
c
d
a reduced schedule:
cache
a
a
b
c
a
a
b
c
c
a
b
c
d
a
d
c
a
a
d
c
b
a
d
b
c
a
c
b
d
d
c
b
d
d
c
b
reduced eviction schedules
Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.
Pf by induction on number of steps \(j\):
Suppose \(S\) brings \(d\) into the cache in step \(j\) without a request
Let \(c\) be the item \(S\) evicts when it brings \(d\) into the cache
Case 1: \(S\) brings \(d\) into the cache in step \(j\) without a request
Case 1a: \(d\) evicted before next request for \(d\)
...
reduced eviction schedules
unreduced schedule \(S\):
cache
c
c
c
x
d
y
d
z
d
e
e
e
schedule \(S'\):
cache
c
c
c
x
c
y
c
z
c
e
e
e
\(d\) enters cache without a request
\(d\) evicted before next request for \(d\)
might as well leave \(c\) in cache until \(d\) is evicted
reduced eviction schedules
Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.
Pf by induction on number of steps \(j\):
Suppose \(S\) brings \(d\) into the cache in step \(j\) without a request
Let \(c\) be the item \(S\) evicts when it brings \(d\) into the cache
Case 1: \(S\) brings \(d\) into the cache in step \(j\) without a request
Case 1a: \(d\) evicted before next request for \(d\)
Case 1b: next request for \(d\) occurs before \(d\) is evicted
...
reduced eviction schedules
unreduced schedule \(S\):
cache
c
c
c
x
d
y
d
z
d
d
d
d
schedule \(S'\):
cache
c
c
c
x
c
y
c
z
c
d
d
d
\(d\) enters cache without a request
\(d\) still in cache before next request for \(d\)
might as well leave \(c\) in cache until \(d\) is requested
reduced eviction schedules
Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.
Pf by induction on number of steps \(j\):
Suppose \(S\) brings \(d\) into the cache in step \(j\) without a request
Let \(c\) be the item \(S\) evicts when it brings \(d\) into the cache
Case 1: \(S\) brings \(d\) into the cache in step \(j\) without a request
Case 2: \(S\) brings \(d\) into the cache in step \(j\) even though \(d\) is in cache
Case 2a: \(d\) evicted before it is needed
...
reduced eviction schedules
unreduced schedule \(S\):
cache
d
a
c
d
a
c
d
a
c
d
d
a
d
d
d
a
d
c
c
a
d
b
c
a
b
d
c
a
d
schedule \(S'\):
cache
d
a
c
d
a
c
d
a
c
d
d
a
c
d
d
a
c
c
c
a
c
b
c
a
b
d
c
a
d
\(d\) enters cache even though \(d\) is already in cache (not necessary)
might as well leave \(c\) in cache until \(d\) is evicted
reduced eviction schedules
Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.
Pf by induction on number of steps \(j\):
Suppose \(S\) brings \(d\) into the cache in step \(j\) without a request
Let \(c\) be the item \(S\) evicts when it brings \(d\) into the cache
Case 1: \(S\) brings \(d\) into the cache in step \(j\) without a request
Case 2: \(S\) brings \(d\) into the cache in step \(j\) even though \(d\) is in cache
Case 2a: \(d\) evicted before it is needed
Case 2b: \(d\) is needed before it is evicted
reduced eviction schedules
unreduced schedule \(S\):
cache
d
a
c
d
a
c
d
a
c
d
d
a
d
d
d
a
d
c
c
a
d
a
c
a
d
d
c
a
d
schedule \(S'\):
cache
d
a
c
d
a
c
d
a
c
d
d
a
c
d
d
a
c
c
c
a
c
a
c
a
c
d
c
a
d
\(d\) enters cache even though \(d\) is already in cache (not necessary)
might as well leave \(c\) in cache until \(d\) is needed
reduced eviction schedules
Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.
Pf by induction on number of steps \(j\):
Suppose \(S\) brings \(d\) into the cache in step \(j\) without a request
Let \(c\) be the item \(S\) evicts when it brings \(d\) into the cache
Case 1: \(S\) brings \(d\) into the cache in step \(j\) without a request
Case 2: \(S\) brings \(d\) into the cache in step \(j\) even though \(d\) is in cache
If multiple unreduced items in step \(j\), apply each one in turn, dealing with Case 1 before Case 2
Resolving Case 1 might trigger Case 2 ∎
farthest-in-future analysis
Theorem: FF is optimal eviction algorithm
Pf: Follows directly from the following invariant
farthest-in-future analysis
Invariant: There exists an optimal reduced schedule \(S\) that has the same eviction schedule as \(S_{FF}\) through the first \(j\) steps
Pf by induction on number of steps \(j\):
Base case: \(j=0\)
Let \(S\) be reduced schedule that satisfies invariant through \(j\) steps
We produce \(S'\) that satisfies invariant after \(j+1\) steps
Let \(d\) denote the item requested in step \(j+1\)
Since \(S\) and \(S_{FF}\) have agreed up until now, they have the same cache contents before step \(j+1\)
Case 1: \(d\) is already in the cache: \(S' = S\) satisfies invariant
Case 2: \(d\) is not in the cache and \(S\) and \(S_{FF}\) evict the same item: \(S' = S\) satisfies invariant
...
farthest-in-future analysis
(continued)
Case 3: \(d\) is not in the cache; \(S_{FF}\) evicts \(e\); \(S\) evicts \(f \neq e\).
Begin construction of \(S'\) from \(S\) by evicting \(e\) instead of \(f\)
Now \(S'\) agrees with \(S_{FF}\) for first \(j+1\) steps; we show that having item \(f\) in cache is no worse than having item \(e\) in cache.
Let \(S'\) behave the same as \(S\) until \(S'\) is forced to take a different action (because either \(S\) evicts \(e\); or because either \(e\) or \(f\) is requested)
Schedule \(S\) at steps \(j, j+1\)
cache
same
e
f
d
same
e
d
Schedule \(S'\) at step \(j, j+1\)
cache
same
e
f
d
same
d
f
farthest-in-future analysis
Let \(j'\) be the first step after \(j+1\) that \(S'\) must take a different action from \(S\) (involves either \(e\) or \(f\) or neither); let \(g\) denote the item requested in step \(j'\).
Schedule \(S\) at step \(j, j+1, j'\)
cache
same
e
f
d
same
e
d
e
same
e
d
Schedule \(S'\) at step \(j,j+1,j'\)
cache
same
e
f
d
same
d
f
e
same
d
f
Case 3a: \(g=e\). Can't happen with FF since there must be a request for \(f\) before \(e\) (\(S'\) agrees with \(S_{FF}\) through first \(j+1\) steps)
Case 3b: ...
farthest-in-future analysis
Schedule \(S\) at step \(j, j+1, j'\)
cache
same
e
f
d
same
e
d
f
same
e
d
Schedule \(S'\) at step \(j,j+1,j'\)
cache
same
e
f
d
same
d
f
f
same
d
f
Case 3b: \(g=f\). Element \(f\) can't be in cache of \(S\); let \(e'\) be the item that \(S\) evicts
if \(e' = e\), \(S'\) accesses \(f\) from cache; now \(S\) and \(S'\) have same cache
if \(e' \neq e\), we make \(S'\) evict \(e'\) and bring \(e\) into the cache; now \(S\) and \(S'\) have the same cache (\(S'\) is no longer reduced, but can be transformed into a reduced schedule that agrees with FF through first \(j+1\) steps)
We let \(S'\) behave exactly like \(S\) for remaining requests
farthest-in-future analysis
Schedule \(S\) at step \(j, j+1, j'\)
cache
same
e
f
d
same
e
d
g
same
g
d
Schedule \(S'\) at step \(j,j+1,j'\)
cache
same
e
f
d
same
d
f
g
same
d
f
Case 3c: \(g \neq e,f\). \(S\) evicts \(e\) (otherwise \(S'\) could have taken the same action)
make \(S'\) evict \(f\)
now \(S\) and \(S'\) have the same cache
let \(S'\) behave exactly like \(S\) for the remaining requests ∎
caching perspective
Online vs. offline algorithms
Offline: full sequence of requests is known a priori
Online (reality): requests are not known in advance
Caching is among most fundamental online problems in CS
FIFO: Evict item brought in least recently
LRU: Evict item whose most recent access was earliest (FF with direction of time reversed!)
caching perspective
Theorem: FF is optimal offline eviction algorithm
Provides basis for understanding and analyzing online algorithms
LIFO can be arbitrarily bad
LRU is \(k\)-competitive: for any sequence of requests \(\sigma\)
\(\text{cost}_\text{LRU}(\sigma) \leq k \cdot \text{cost}_\text{FF}(\sigma) + b\)
Randomized marking is \(O(\log k)\)-competitive
See section 13.8
Fact: no online paging algorithm is better than \(k\)-competitive