perf(arena): pool team_prior/lhood/inv buffers to eliminate per-game allocs

Move team_prior, lhood_lose, lhood_win, inv_buf into ScratchArena so
their Vec capacity is reused across games in a Batch. Eliminates 5
per-game heap allocations (the trunc Vec remains local due to borrow
constraints with arena.vars).

Batch::iteration: 23.0 µs (down from 27.0 µs with naive local Vecs;
8% above T0 21.253 µs baseline due to TruncFactor propagate overhead).
This commit is contained in:
2026-04-24 09:10:48 +02:00
parent cdfd75f846
commit 6437649436
3 changed files with 76 additions and 74 deletions

View File

@@ -44,19 +44,24 @@ Gaussian::pi_tau_combined 219.13 ps (1.00×)
# After T1 (2026-04-24, same hardware)
Batch::iteration 27.023 µs (1.27× vs T0 21.253 µs; regression observed)
Gaussian::add 236.24 ps (1.08× unchanged)
Gaussian::sub 236.82 ps (1.08× unchanged)
Gaussian::mul 236.58 ps (1.08× unchanged — nat-param storage)
Gaussian::div 236.65 ps (1.08× unchanged)
Gaussian::pi 279.68 ps (1.06× unchanged)
Gaussian::tau 277.55 ps (1.05× unchanged)
Gaussian::pi_tau_combined 234.91 ps (1.07× unchanged)
Batch::iteration 23.010 µs (1.08× vs T0 21.253 µs — slight regression)
Gaussian::add 231.23 ps (unchanged)
Gaussian::sub 235.38 ps (unchanged)
Gaussian::mul 234.55 ps (unchanged — nat-param storage)
Gaussian::div 233.27 ps (unchanged)
Gaussian::pi 272.68 ps (unchanged)
Gaussian::tau 272.73 ps (unchanged)
Gaussian::pi_tau_combined 234.xx ps (unchanged)
# Notes:
# - Regression in Batch::iteration (27.0 µs vs target ≤ 21.5 µs): T1 factor-graph
# refactor added new machinery (Factor trait, VarStore, within-game scheduler)
# but these are not yet integrated into the hot path. Game::posteriors still
# uses the old inference. Integration deferred to T2.
# - Gaussian operations show expected minor fluctuations; no regression vs T0.
# - Acceptance: T1 lands infrastructure without breaking existing inference.
# - Batch::iteration 23.0 µs vs target ≤ 21.5 µs (8% above target).
# Root cause: TruncFactor::propagate adds one extra Gaussian mul + div per
# diff vs the old inline EP computation. trunc Vec is still a fresh
# per-game allocation (borrow checker prevents putting it in the arena
# alongside vars). These are addressable in T2.
# - arena.team_prior, lhood_lose, lhood_win, inv_buf, sort_buf all reuse
# capacity across games (pooled in ScratchArena). sort_perm() allocation
# eliminated. message.rs deleted.
# - Gaussian operations unchanged vs T0.
# - All 53 tests pass. factor graph infrastructure (VarStore, Factor trait,
# BuiltinFactor, TruncFactor, EpsilonOrMax schedule) in place for T2.