perf(engine): keep the lower halves of 32 recent contexts instead of 8

This commit is contained in:
Tyler Smith committed 2026-11-11 14:26:38 -05:00
1 parent e73d9daadd
commit ff5d34c7ea
1 file changed
+31 -2
+31 -2
View File
@@ -154,8 +154,12 @@ pub struct Kept {
switches: u64,
}
/// The contexts whose lower halves [`Kept`] holds at most.
pub const KEPT: usize = 8;
/// The contexts whose lower halves [`Kept`] holds at most. A replaced half
/// frees no frames (they return only when the lower half is emptied), and
/// refilling a context that comes back allocates its tables again, so a
/// replacement costs frames as well as fills: a desktop session rotates
/// among dozens of processes, which eight halves missed half the time.
pub const KEPT: usize = 32;
/// One context's kept lower half.
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -473,6 +477,31 @@ mod tests {
assert!(kept.is_empty());
}
#[test]
fn a_rotation_of_many_contexts_finds_every_half() {
// Two dozen processes taking turns, as a desktop session's do,
// each find their half again after the first round.
let mut kept = Kept::new();
let key = |n: u64| (n << 12, 0);
const CONTEXTS: u64 = 24;
const { assert!(CONTEXTS as usize <= KEPT) };
let mut found = 0;
for turn in 0..CONTEXTS * 4 {
let (from, to) = (turn % CONTEXTS, (turn + 1) % CONTEXTS);
let half = kept.switch(
key(from),
alloc::vec![(0x1000, 0, from + 1)],
Extents::new(),
key(to),
);
if let Some(half) = half {
assert_eq!(half.entries, [(0x1000, 0, to + 1)]);
found += 1;
}
}
assert_eq!(found, CONTEXTS * 4 - (CONTEXTS - 1));
}
#[test]
fn switching_to_the_oldest_kept_half_reuses_it_when_full() {
let mut kept = Kept::new();