Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion helios/src/scheduler/cache_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ fn select_snapshots(
}
return best.map(|(_, id)| vec![id]);
}
// Bound the enumerated set for every multi-row path. With heavily shared prefixes
// (agent-style traffic reusing one long context) the greedy witness below and the
// two-row pair loop both degrade — the pair loop toward O(n^2 * pages) and the
// greedy witness toward O(count * n * pages) with a BTreeSet probe per page — and a
// single quote can burn seconds of scheduler time (see GH #319; an engine livelock
// was reproduced live through both paths). Keep the cheapest MAX_MULTI_ROW_CANDIDATES
// by (cost, id): ids alone are only a tie-break after cost, so the cheapest set is
// kept rather than the first ids, and zero-cost candidates always survive. Inputs
// with at most that many candidates run the exact original algorithms unchanged.
const MAX_MULTI_ROW_CANDIDATES: usize = 128;
let mut candidates = candidates;
if candidates.len() > MAX_MULTI_ROW_CANDIDATES {
candidates.sort_unstable_by_key(|(id, pages)| (pages.len(), *id));
candidates.truncate(MAX_MULTI_ROW_CANDIDATES);
}
if count > 2 {
// Future capture capacity uses a conservative witness without enumerating subsets.
let mut selected = BTreeSet::<SnapshotId>::new();
Expand Down Expand Up @@ -144,7 +159,17 @@ fn select_snapshots(
}
for b in a + 1..candidates.len() {
let union = pages.len() + candidates[b].1.len() - overlap[b] as usize;
if union <= budget && best.is_none_or(|(cost, _, _)| union < cost) {
if union > budget {
continue;
}
// Ties prefer the lexicographically lowest id pair; the candidate list may
// be cost-sorted, so compare ids explicitly instead of relying on scan order.
let better = best.is_none_or(|(cost, ba, bb)| {
union < cost
|| (union == cost
&& (candidates[a].0, candidates[b].0) < (candidates[ba].0, candidates[bb].0))
});
if better {
best = Some((union, a, b));
}
}
Expand Down Expand Up @@ -1466,3 +1491,81 @@ mod tests {
);
}
}

#[cfg(test)]
mod wedge_bench {
use super::*;

// 合成"共享前缀"工作负载: base_pages 被所有候选共享, 每个候选再独占 tail_pages
#[test]
fn select_snapshots_shared_prefix_blowup() {
let pages: usize = 327_680; // 假设 page_tokens=64: 20.98M KV tokens
let base_pages: usize = 45; // ~90k token 共享前缀
let tail_pages: usize = 4;
let n_candidates: usize = 1986; // 全部快照槽都在候选集
let keep = pages / 2;

let backing = vec![true; pages];
let protected = vec![0u32; pages];
let mut ranges = vec![0u32..0; n_candidates];
for i in 0..n_candidates {
let start = (base_pages + i * tail_pages) as u32;
ranges[i] = 0u32..(start + tail_pages as u32); // 覆盖共享前缀+私有尾页
}
let candidates: BTreeSet<SnapshotId> =
(0..n_candidates).map(|i| SnapshotId(i as u32)).collect();

let t = std::time::Instant::now();
let r = select_snapshots(&backing, &protected, &ranges, &candidates, keep, 2);
let elapsed = t.elapsed();
assert_eq!(r.as_ref().map(|v| v.len()), Some(2), "a quotable pair must still be found");
println!("select(shared-prefix, {} candidates) took {:?}", n_candidates, elapsed);
// Guard against the pathological pair-enumeration blowup: unbounded this
// takes ~9.4 s on this input; leave generous margin for slower CI hosts.
assert!(elapsed < std::time::Duration::from_secs(5), "pair enumeration is unbounded again");
}

// The count>2 greedy witness path blew up the same way in the live engine
// (see GH #319 evidence-1118_1807): BTreeSet probes per page over every candidate.
#[test]
fn multi_row_witness_shared_prefix_blowup() {
let pages: usize = 327_680;
let base_pages: usize = 45;
let n_candidates: usize = 1986;
let backing = vec![true; pages];
let protected = vec![0u32; pages];
let ranges: Vec<_> = (0..n_candidates)
.map(|i| 0u32..(base_pages as u32 + i as u32 * 4 + 4))
.collect();
let candidates: BTreeSet<SnapshotId> =
(0..n_candidates).map(|i| SnapshotId(i as u32)).collect();
let t = std::time::Instant::now();
let r = select_snapshots(&backing, &protected, &ranges, &candidates, pages / 2, 7);
let elapsed = t.elapsed();
assert!(r.is_some(), "greedy witness must still select a conservative set");
println!("select(count=7, shared-prefix, {} candidates) took {:?}", n_candidates, elapsed);
assert!(elapsed < std::time::Duration::from_secs(5),
"multi-row witness enumeration is unbounded again");
}

// Review case from GH #326: the only feasible (zero-cost) candidates sit past
// index 127; bounding must keep them reachable for both row counts.
#[test]
fn cheapest_candidates_beyond_the_bound_stay_selectable() {
let base_pages = 2;
let backing = vec![true; base_pages];
let protected = vec![0u32; base_pages];
let mut ranges = vec![0u32..base_pages as u32; 128];
ranges.push(0u32..0); // id 128: zero cost
ranges.push(0u32..0); // id 129: zero cost
let candidates: BTreeSet<SnapshotId> =
(0..130u32).map(SnapshotId).collect();

let one = select_snapshots(&backing, &protected, &ranges, &candidates, base_pages, 1);
assert_eq!(one, Some(vec![SnapshotId(128)]), "single row must pick the zero-cost id 128");

let two = select_snapshots(&backing, &protected, &ranges, &candidates, base_pages, 2);
assert_eq!(two, Some(vec![SnapshotId(128), SnapshotId(129)]),
"two rows must pick the zero-cost pair 128/129");
}
}