Bound recorded SAM poses under the WASM memory cap (#122)
Thin old SAM keyframes in place, reduce allocation capacity and adapt later sampling when a recording exceeds its fixed pose budget. Preserve recent poses and cover the full ten-minute window in native and real WASM stress tests. Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
@@ -155,6 +155,8 @@ impl Recording {
|
||||
objects: Vec::new(),
|
||||
sam_tracks: Vec::new(),
|
||||
sam_index: HashMap::new(),
|
||||
sam_keys: 0,
|
||||
sam_step: STEP,
|
||||
ticks: Vec::new(),
|
||||
times: Vec::new(),
|
||||
quiet: 0,
|
||||
@@ -185,6 +187,8 @@ pub const STEP: u32 = 2;
|
||||
/// The longest recording (10 minutes) and how many are kept.
|
||||
const LONGEST: u32 = 12_000;
|
||||
const KEPT: usize = 3;
|
||||
/// SAM poses share a fixed memory budget; under pressure old poses are thinned and new ones slow down.
|
||||
const MAX_SAM_KEYS: usize = 32_768;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
pub enum What {
|
||||
@@ -381,6 +385,8 @@ pub struct Recording {
|
||||
/// it pointed, whether it was still whole).
|
||||
pub sam_tracks: Vec<SamTrack>,
|
||||
sam_index: HashMap<u32, usize>,
|
||||
sam_keys: usize,
|
||||
sam_step: u32,
|
||||
/// The game's tick number as sampled with the clock: (recording tick, game tick). A replay
|
||||
/// shows the world's blocks as of this time.
|
||||
ticks: Vec<(u32, u64)>,
|
||||
@@ -589,14 +595,38 @@ impl Recording {
|
||||
objects: sim.targets.clone(),
|
||||
sam_tracks: Vec::new(),
|
||||
sam_index: HashMap::new(),
|
||||
sam_keys: 0,
|
||||
sam_step: STEP,
|
||||
ticks: Vec::new(),
|
||||
times: Vec::new(),
|
||||
quiet: 0,
|
||||
}
|
||||
}
|
||||
fn thin_sam_tracks(&mut self) {
|
||||
for track in &mut self.sam_tracks {
|
||||
let len = track.keys.len();
|
||||
let mut kept = 0;
|
||||
for read in (0..len).step_by(2) {
|
||||
track.keys[kept] = track.keys[read];
|
||||
kept += 1;
|
||||
}
|
||||
if len % 2 == 0 && len > 0 {
|
||||
track.keys[kept] = track.keys[len - 1];
|
||||
kept += 1;
|
||||
}
|
||||
track.keys.truncate(kept);
|
||||
track.keys.shrink_to_fit();
|
||||
}
|
||||
self.sam_keys = self.sam_tracks.iter().map(|track| track.keys.len()).sum();
|
||||
self.sam_step = self.sam_step.saturating_mul(2).min(LONGEST);
|
||||
}
|
||||
|
||||
/// Notes the systems' poses at the recording's tick `now`: a pose only when it changed, with
|
||||
/// the pose it held marked before it so a replay doesn't glide through the stillness.
|
||||
fn note_sams(&mut self, sams: &[Sam], now: u32) {
|
||||
if now % self.sam_step != 0 {
|
||||
return;
|
||||
}
|
||||
for sam in sams {
|
||||
let key = SamKey::of(sam, self.origin, now);
|
||||
let i = *self.sam_index.entry(sam.id).or_insert_with(|| {
|
||||
@@ -604,16 +634,21 @@ impl Recording {
|
||||
self.sam_tracks.len() - 1
|
||||
});
|
||||
let track = &mut self.sam_tracks[i];
|
||||
let before = track.keys.len();
|
||||
match track.keys.last().copied() {
|
||||
None => track.keys.push(key),
|
||||
Some(last) if last.same(&key) => {}
|
||||
Some(last) => {
|
||||
if last.at + STEP < now {
|
||||
track.keys.push(SamKey { at: now - STEP, ..last });
|
||||
if last.at + self.sam_step < now {
|
||||
track.keys.push(SamKey { at: now - self.sam_step, ..last });
|
||||
}
|
||||
track.keys.push(key);
|
||||
}
|
||||
}
|
||||
self.sam_keys += track.keys.len() - before;
|
||||
}
|
||||
if self.sam_keys > MAX_SAM_KEYS {
|
||||
self.thin_sam_tracks();
|
||||
}
|
||||
}
|
||||
/// Actual launch, if observed during this recording. Missing is not proof of no launch:
|
||||
@@ -1043,6 +1078,8 @@ mod tests {
|
||||
objects: Vec::new(),
|
||||
sam_tracks: Vec::new(),
|
||||
sam_index: HashMap::new(),
|
||||
sam_keys: 0,
|
||||
sam_step: STEP,
|
||||
ticks: Vec::new(),
|
||||
times: Vec::new(),
|
||||
quiet: 0,
|
||||
@@ -1169,6 +1206,29 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_recording_bounds_sam_keys_without_freezing_their_poses() {
|
||||
let mut r = recording();
|
||||
let mut sams: Vec<_> = (0..64)
|
||||
.map(|id| Sam::new(id, SamKind::Radar, DVec3::ZERO, 0.0))
|
||||
.collect();
|
||||
for now in (0..LONGEST).step_by(STEP as usize) {
|
||||
for sam in &mut sams {
|
||||
sam.dish = now as f64 * 0.01;
|
||||
}
|
||||
r.note_sams(&sams, now);
|
||||
}
|
||||
let keys: usize = r.sam_tracks.iter().map(|track| track.keys.len()).sum();
|
||||
let allocated: usize = r.sam_tracks.iter().map(|track| track.keys.capacity()).sum();
|
||||
assert_eq!(keys, r.sam_keys);
|
||||
assert!(keys <= MAX_SAM_KEYS, "a full recording retained {keys} SamKeys");
|
||||
assert!(allocated <= 2 * MAX_SAM_KEYS, "SamKey allocations reached {} MiB", allocated * size_of::<SamKey>() / (1024 * 1024));
|
||||
assert!(r.sam_step > STEP, "the rotating radars never engaged adaptive sampling");
|
||||
let last = r.sam_tracks[0].pose(LONGEST as f64).unwrap();
|
||||
assert!(last.at >= LONGEST - 64, "poses stopped at tick {}", last.at);
|
||||
assert!(last.dish > 100.0, "the final radar pose was not recorded");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_system_keeps_its_pose_through_stillness_and_moves_when_it_moved() {
|
||||
let mut r = recording();
|
||||
|
||||
Reference in New Issue
Block a user