Documentation

CslibFrontier.DataStructure.Paging.LRU

Least-recently-used (LRU) paging and k-competitiveness #

This file implements the least-recently-used (LRU) online paging rule and proves its k-competitiveness via a Cslib.DataStructure.Amortized potential certificate, formalizing Theorem 6 of Sleator and Tarjan, Amortized Efficiency of List Update and Paging Rules, Section 4 ("Paging"), p. 207 (proof pp. 207–208) in the equal-capacity (n_LRU = n_MIN = k) demand-paging model of CslibFrontier.DataStructure.Paging.Basic.

Source statement and mapping #

In [ST85a], Section 4 ("Paging"), pp. 207–208, Theorem 6 (p. 207) bounds the page faults of LRU with fast-memory capacity n_LRU against any offline paging algorithm with fast-memory capacity n_MIN ≤ n_LRU by (n_LRU / (n_LRU - n_MIN + 1)) · F_MIN + n_MIN. Specializing to equal online and offline fast-memory capacity (n_LRU = n_MIN = k, 0 < k):

Resource augmentation with unequal fast-memory capacities (n_MIN < n_LRU) is left as a separate extension.

Main definitions #

Main results #

References #

Computable recency-list LRU operations #

def Cslib.DataStructure.Paging.lruNext {α : Type} [DecidableEq α] (k : ℕ) (q : List α) (p : α) :
List α

Advance the recency-ordered LRU list (most recently used first) of capacity k on page p.

def Cslib.DataStructure.Paging.lruStep {α : Type} [DecidableEq α] (k : ℕ) (q : List α) (p : α) :

Single-step LRU update returning the new recency list and whether a page fault occurred.

def Cslib.DataStructure.Paging.lruRun {α : Type} [DecidableEq α] (k : ℕ) (q : List α) :
List α → List α

Run lruNext k across a request list from initial recency list q.

def Cslib.DataStructure.Paging.lruFaults {α : Type} [DecidableEq α] (k : ℕ) (q : List α) :
List α → ℕ

Count total page faults incurred by computable recency-list LRU from q on reqs.

@[simp]
theorem Cslib.DataStructure.Paging.lruRun_nil {α : Type} [DecidableEq α] (k : ℕ) (q : List α) :
lruRun k q [] = q
@[simp]
theorem Cslib.DataStructure.Paging.lruRun_cons {α : Type} [DecidableEq α] (k : ℕ) (q : List α) (p : α) (ps : List α) :
lruRun k q (p :: ps) = lruRun k (lruNext k q p) ps
@[simp]
@[simp]
theorem Cslib.DataStructure.Paging.lruFaults_cons {α : Type} [DecidableEq α] (k : ℕ) (q : List α) (p : α) (ps : List α) :
lruFaults k q (p :: ps) = (if p ∈ q then 0 else 1) + lruFaults k (lruNext k q p) ps
theorem Cslib.DataStructure.Paging.nodup_lruNext {α : Type} [DecidableEq α] {k : ℕ} {q : List α} (hq : q.Nodup) (p : α) :
(lruNext k q p).Nodup

lruNext preserves List.Nodup.

theorem Cslib.DataStructure.Paging.length_lruNext_le {α : Type} [DecidableEq α] {k : ℕ} {q : List α} (hq : q.length ≤ k) (p : α) :
(lruNext k q p).length ≤ k

lruNext k preserves length ≤ k.

theorem Cslib.DataStructure.Paging.toFinset_lruNext_of_mem {α : Type} [DecidableEq α] {k : ℕ} {q : List α} {p : α} (hp : p ∈ q) :

On a hit p ∈ q, lruNext preserves the underlying set of cached pages.

theorem Cslib.DataStructure.Paging.mem_lruNext {α : Type} [DecidableEq α] {k : ℕ} (hk : 0 < k) (q : List α) (p : α) :
p ∈ lruNext k q p

For 0 < k, p always resides in lruNext k q p.

theorem Cslib.DataStructure.Paging.toFinset_lruNext_subset {α : Type} [DecidableEq α] {k : ℕ} (q : List α) (p : α) :

Every page in lruNext k q p comes from insert p q.toFinset.

theorem Cslib.DataStructure.Paging.validStep_lruNext {α : Type} [DecidableEq α] {k : ℕ} (hk : 0 < k) {q : List α} (hlen : q.length ≤ k) (p : α) :

For 0 < k, lruNext induces a valid demand-paging transition on q.toFinset.

Packaging LRU as an Online.DetStrategy #

@[reducible, inline]
noncomputable abbrev Cslib.DataStructure.Paging.lruStrategy {α : Type} [DecidableEq α] (k : ℕ) (hk : 0 < k) :

Deterministic online strategy realizing LRU for demand paging with positive capacity k.

Marked noncomputable solely because init s₀ := s₀.toList selects an initial recency ordering on pre-loaded cache elements from s₀ : Finset α (canonically [] when s₀ = ∅, as witnessed by lruStrategy_cost_empty, with lruPotential k s₀ s₀.toList = 0 for every s₀). For an explicit initial recency list q : List α, lruRun, lruFaults, and lruFaults_amortized_bound are directly computable.

theorem Cslib.DataStructure.Paging.lruStrategy_runMem_eq {α : Type} [DecidableEq α] (k : ℕ) (hk : 0 < k) (q reqs : List α) :
(lruStrategy k hk).runMem q reqs = lruRun k q reqs

lruStrategy.runMem agrees with computable lruRun.

theorem Cslib.DataStructure.Paging.lruStrategy_costFrom_eq {α : Type} [DecidableEq α] (k : ℕ) (hk : 0 < k) (q reqs : List α) :
(lruStrategy k hk).costFrom q reqs = lruFaults k q reqs

lruStrategy.costFrom agrees with computable lruFaults.

theorem Cslib.DataStructure.Paging.lruStrategy_cost_empty {α : Type} [DecidableEq α] (k : ℕ) (hk : 0 < k) (reqs : List α) :
(lruStrategy k hk).cost ∅ reqs = lruFaults k [] reqs

From an empty cache ∅, lruStrategy.cost equals computable lruFaults k [] reqs.

Potential function and structural step inequalities #

Recency weight of the most recently used element in q absent from s (0 if all elements of q belong to s), assigning head weight w and decrementing by 1 at each step toward the LRU tail.

def Cslib.DataStructure.Paging.lruPotential {α : Type} [DecidableEq α] (k : ℕ) (s : Finset α) (q : List α) :

Potential function between offline cache s and LRU recency list q at capacity k.

@[simp]
theorem Cslib.DataStructure.Paging.maxWeightFrom_cons {α : Type} [DecidableEq α] (w : ℕ) (s : Finset α) (x : α) (xs : List α) :
maxWeightFrom w s (x :: xs) = if x ∈ s then maxWeightFrom (w - 1) s xs else w
theorem Cslib.DataStructure.Paging.maxWeightFrom_le {α : Type} [DecidableEq α] (w : ℕ) (s : Finset α) (q : List α) :

maxWeightFrom w s q never exceeds the head weight w.

theorem Cslib.DataStructure.Paging.maxWeightFrom_eq_zero_of_subset {α : Type} [DecidableEq α] {s : Finset α} {q : List α} (h : ∀ x ∈ q, x ∈ s) (w : ℕ) :

When every page of q is in s, maxWeightFrom vanishes.

Taking a prefix cannot increase maxWeightFrom.

theorem Cslib.DataStructure.Paging.maxWeightFrom_erase_of_mem {α : Type} [DecidableEq α] {s : Finset α} {p : α} (hp : p ∈ s) (w : ℕ) (q : List α) :
maxWeightFrom (w - 1) s (q.erase p) ≤ maxWeightFrom w s q

Removing a resident page p ∈ s from q and decrementing the starting weight does not increase maxWeightFrom.

theorem Cslib.DataStructure.Paging.maxWeightFrom_pred_zero_or_lt {α : Type} [DecidableEq α] (w : ℕ) (s : Finset α) (q : List α) :
maxWeightFrom (w - 1) s q = 0 ∨ maxWeightFrom (w - 1) s q + 1 ≤ maxWeightFrom w s q

Decrementing the starting weight either yields 0 or decreases maxWeightFrom by at least 1.

theorem Cslib.DataStructure.Paging.lruPotential_le {α : Type} [DecidableEq α] {k : ℕ} {s : Finset α} (hs : s.card ≤ k) (q : List α) :

lruPotential k s q never exceeds k when s.card ≤ k.

lruPotential k s q vanishes whenever q represents the exact same page set as s.

theorem Cslib.DataStructure.Paging.lruPotential_step {α : Type} [DecidableEq α] {k : ℕ} (hk : 0 < k) {s s' : Finset α} {q : List α} {p : α} (hq : q.Nodup ∧ q.length ≤ k) (hv : ValidStep k s p s') :
stepCost q.toFinset p (lruNext k q p).toFinset + lruPotential k s' (lruNext k q p) ≤ k * stepCost s p s' + lruPotential k s q

Single-step competitive potential inequality for LRU (0 < k).

Cslib.DataStructure.Amortized certificate and competitive theorems #

theorem Cslib.DataStructure.Paging.lruAmortized {α : Type} [DecidableEq α] (k : ℕ) (hk : 0 < k) :
Amortized (lruStrategy k hk).impl id (lruPotential k) fun (s : (problem α k).offlineSpec.State) (o : (problem α k).offlineSpec.Op) => k * stepCost s o.1 o.2

Native Cslib.DataStructure.Amortized potential certificate for lruStrategy k hk against k times the offline demand-paging step cost.

theorem Cslib.DataStructure.Paging.lruFaults_amortized_bound {α : Type} [DecidableEq α] {k : ℕ} (hk : 0 < k) {s_off : Finset α} {q reqs : List α} {offStates : List (Finset α)} (hq : q.Nodup ∧ q.length ≤ k) (hv : (problem α k).ValidSchedule s_off reqs offStates) :
lruFaults k q reqs + lruPotential k ((problem α k).finalState s_off offStates) (lruRun k q reqs) ≤ k * (problem α k).scheduleCost s_off reqs offStates + lruPotential k s_off q

Computable-list telescoping bound for LRU: for any valid recency list q and any valid offline schedule (s_off, reqs, offStates), lruFaults k q reqs plus final potential is at most k times offline faults plus initial potential.

LRU k-competitiveness from a common initial cache ([ST85a], Theorem 6 at n_LRU = n_MIN = k with identical initial caches): when online and offline algorithms start from the same valid cache s₀ (including ∅), lruStrategy k hk incurs at most k times the faults of any valid offline schedule, with zero additive constant.

LRU k-competitiveness across arbitrary initial caches ([ST85a], Theorem 6 at n_LRU = n_MIN = k): for any valid initial caches s₀ and s_off of size at most k, lruStrategy k hk incurs at most k * optCost + k faults.