Skip to content

Commit 4a46576

Browse files
hearnadamkommander
authored andcommitted
perf(rust): a run collects while it waits for the host
A long-lived run kept up to 64k slots of garbage between cycles, most of what a small one held. The threshold while computing stays 64k slots, so a short program still never collects. A run that stops to wait for the host collects there, a whole cycle, once it has made 8k slots since its last one (Vm::gc_idle): time the program was not going to use. A heap over 256k slots keeps collecting in steps. examples/gc_bench, 20,000 events: palette loop peak live memory 16.1 to 1.9 MB; 50 runs with 5 active 51.7 to 13.2 MB; collection time and event latency unchanged.
1 parent 601f904 commit 4a46576

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎rust/crates/psychoscript/src/collector.rs‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ const GREY: u8 = 1;
4848
const BLACK: u8 = 2;
4949
const FREE: u8 = 3;
5050

51-
/// Slots in use (objects, cells and strings) at which the first cycle starts. A reducer event stays far below it, and
52-
/// of the benchmark programs only the JSON round trip reaches it.
51+
/// Slots in use (objects, cells and strings) at which the first cycle starts while the program computes. A short
52+
/// program, which most are, stays below it and never collects: it ends first, and its whole heap goes at once.
5353
const FIRST: usize = 64 * 1024;
54-
/// The least room a cycle leaves before the next one starts, so a small heap does not collect all the time.
54+
/// The least room a cycle leaves before the next one starts while the program computes, so a small heap does not
55+
/// collect all the time.
5556
const FLOOR: usize = 64 * 1024;
57+
/// A run that stops to wait for the host collects there, whole, once it has made this many slots since its last
58+
/// cycle (and half of what that cycle left): the wait is time the program was not going to use, so a long-lived run,
59+
/// which waits between its events, stays small without a program that only computes paying for it.
60+
const IDLE_ROOM: usize = 8 * 1024;
61+
/// The most slots in use for a whole cycle at a wait: beyond it a cycle takes longer than a wait should, and the
62+
/// run collects in steps as it allocates.
63+
const IDLE_MAX: usize = 256 * 1024;
5664
/// Allocations between steps.
5765
const STEP: usize = 256;
5866
/// Work units per allocation: enough to finish a cycle before the heap grows by about half. A unit is about what
@@ -899,6 +907,38 @@ impl Vm {
899907
true
900908
}
901909

910+
/// The run stopped to wait for the host: collects now, a whole cycle, if it has made enough since its last one
911+
/// (`IDLE_ROOM`) and is small enough for that to be quick (`IDLE_MAX`).
912+
pub(crate) fn gc_idle(&mut self) {
913+
let busy = self.replay_meter.is_some()
914+
|| !self.replay.is_empty()
915+
|| self.convert_req.is_some()
916+
|| self.json_walk != 0;
917+
if busy || stress() {
918+
return;
919+
}
920+
let used = self.gc_used();
921+
if used > IDLE_MAX {
922+
return;
923+
}
924+
if self.gc.phase == Phase::Idle && used < self.gc.live + IDLE_ROOM.max(self.gc.live / 2) {
925+
return;
926+
}
927+
loop {
928+
self.gc.debt = MAX_WORK;
929+
self.gc_step();
930+
if self.gc.phase == Phase::Idle {
931+
break;
932+
}
933+
}
934+
// A step the cycle had asked for is done with it.
935+
self.gc.requested = false;
936+
if self.gc.owns_suspend {
937+
self.gc.owns_suspend = false;
938+
self.suspend_wanted = false;
939+
}
940+
}
941+
902942
/// Whether a step is due, for the host stop.
903943
pub(crate) fn gc_pending(&self) -> bool {
904944
self.gc.requested

‎rust/crates/psychoscript/src/program.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ impl<'a> Program<'a> {
421421
if r.vm.gc_pending() && r.vm.fatal.is_none() {
422422
r.vm.gc_point();
423423
}
424+
// And where a run that waits collects, in time it was not going to use.
425+
if r.vm.fatal.is_none() && r.vm.outcome.is_none() && !r.vm.suspended {
426+
r.vm.gc_idle();
427+
}
424428
if r.vm.heap_meter.is_some() && r.vm.fatal.is_none() && r.vm.outcome.is_none() {
425429
r.vm.heap_point(0);
426430
}

0 commit comments

Comments
 (0)