perf(game): eliminate per-event allocations via ScratchArena
Game::likelihoods previously allocated four Vecs (teams, diffs, ties, margins) on every call. Batch now owns one ScratchArena reused across all Game::new calls in the iteration loop; likelihoods() clears and extends the arena buffers instead of allocating fresh. For log_evidence (called infrequently), a local ScratchArena is created per invocation so the method signature stays &self. Also: add #[derive(Debug)] to TeamMessage and DiffMessage (required by ScratchArena's own Debug derive). Part of T0 engine redesign.
This commit is contained in:
44
src/arena.rs
Normal file
44
src/arena.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use crate::message::{DiffMessage, TeamMessage};
|
||||
|
||||
/// Reusable scratch buffers for `Game::likelihoods`.
|
||||
///
|
||||
/// The four Vecs previously allocated fresh on every `Game::new` call —
|
||||
/// `teams`, `diffs`, `ties`, `margins` — are now borrowed from this arena,
|
||||
/// reset between uses. A `Batch` owns one arena; all events in the slice
|
||||
/// share it across the convergence iterations.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ScratchArena {
|
||||
pub(crate) teams: Vec<TeamMessage>,
|
||||
pub(crate) diffs: Vec<DiffMessage>,
|
||||
pub(crate) ties: Vec<bool>,
|
||||
pub(crate) margins: Vec<f64>,
|
||||
}
|
||||
|
||||
impl ScratchArena {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn reset(&mut self) {
|
||||
self.teams.clear();
|
||||
self.diffs.clear();
|
||||
self.ties.clear();
|
||||
self.margins.clear();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn reset_keeps_capacity() {
|
||||
let mut arena = ScratchArena::new();
|
||||
arena.teams.push(TeamMessage::default());
|
||||
let cap = arena.teams.capacity();
|
||||
arena.reset();
|
||||
assert_eq!(arena.teams.len(), 0);
|
||||
assert_eq!(arena.teams.capacity(), cap);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user