sim: account where the mod's memory goes (Sim::memory, per-cache bytes)

The fullscreen-map OOM (Ground::answer rehash under a large raid) needs a
measured breakdown, not a guess: Sim::memory estimates each consumer's
allocated capacity (hashbrown tables with their control bytes, vec capacities,
BTreeMap nodes) — Ground.known, WorldCache.secs and rays, the sim vectors and
radar tracks, the recording and kept replays, Intel/recon, routes, sites,
strikes, the raid plan. MemoryReport::rehash_headroom names the transient a
rehash adds (old + new table of the biggest map). Ground::known_len exposes the
column count the budget tests guard.

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-21 17:15:31 +03:00
parent dca35dc9a2
commit 4dbe103f5b
10 changed files with 301 additions and 0 deletions
+16
View File
@@ -151,6 +151,11 @@ pub(super) struct KnownRelief {
}
impl KnownRelief {
/// What the relief holds, about (see `sim::memory`).
pub(super) fn bytes(&self) -> usize {
use super::memory::vec_bytes;
vec_bytes(&self.heights)
}
pub(super) fn new(grid: raid::Grid, heights: Vec<f32>) -> Self {
Self { grid, heights }
}
@@ -401,6 +406,11 @@ pub(super) struct KnowledgeSnapshot {
}
impl KnowledgeSnapshot {
/// What the snapshot holds, about (see `sim::memory`); shared `Arc`s are counted per holder.
pub(super) fn bytes(&self) -> usize {
use super::memory::vec_bytes;
vec_bytes(&self.contacts) + vec_bytes(&self.marks)
}
pub(super) fn new(tick: u64, contacts: Vec<Contact>, marks: Vec<recon::Mark>) -> Self {
Self { tick, contacts, marks }
}
@@ -506,6 +516,12 @@ pub(super) struct Intel {
}
impl Intel {
/// What the enemy's memory holds, about (see `sim::memory`).
pub(super) fn bytes(&self) -> usize {
use super::memory::{btree_bytes, vecdeque_bytes};
btree_bytes(self.contacts.len(), std::mem::size_of::<u32>() + std::mem::size_of::<Contact>())
+ vecdeque_bytes(&self.outcomes)
}
/// The picture as a save writes it, and back: the contacts (one per unit) and the sector scores.
pub(super) fn contacts(&self) -> &BTreeMap<u32, Contact> {
&self.contacts
+7
View File
@@ -67,6 +67,13 @@ pub fn level(age: f64, life: f64) -> (f32, f32) {
}
impl Fires {
/// What the fires hold, about (see `sim::memory`).
pub(super) fn bytes(&self) -> usize {
use super::memory::vec_bytes;
vec_bytes(&self.list)
+ self.list.iter().map(|f| vec_bytes(&f.blocks)).sum::<usize>()
+ self.cleanup.iter().map(|s| s.capacity()).sum::<usize>()
}
/// Fires burning now.
pub fn len(&self) -> usize {
self.list.len()
+178
View File
@@ -0,0 +1,178 @@
//! Where the mod's memory goes: an estimate of what each consumer holds, for the budget
//! tests and the diagnostics. The whole mod lives in one 64 MiB WASM linear memory
//! (`horizon-mods` runtime `MEMORY`): the simulation, the ground and block caches, the
//! recordings and the enemy's memory all share it, and a hashbrown rehash briefly holds
//! the old and the new table at once. The estimates count allocated capacity, not len,
//! and the tables' control bytes; they are a lower bound on the allocator's footprint
//! (fragmentation and the transient query/answer vectors come on top).
use super::*;
/// A hashbrown table's allocation for `len` entries of `entry` bytes (key + value):
/// the capacity grows 3, 7, 14, 28, … at a 7/8 load, and the allocation is
/// `(capacity + group) * (entry + 1)` (one control byte per slot).
pub(super) fn map_bytes(len: usize, entry: usize) -> usize {
if len == 0 {
return 0;
}
let mut cap = 3usize;
while cap * 7 / 8 < len {
cap = if cap == 3 { 7 } else { cap * 2 };
}
(cap + 16) * (entry + 1)
}
/// A `BTreeMap`'s nodes, about: the entry plus the node's share of pointers and slack.
pub(super) fn btree_bytes(len: usize, entry: usize) -> usize {
len * (entry + 32)
}
pub(super) fn vec_bytes<T>(v: &Vec<T>) -> usize {
v.capacity() * std::mem::size_of::<T>()
}
pub(super) fn vecdeque_bytes<T>(v: &std::collections::VecDeque<T>) -> usize {
v.capacity() * std::mem::size_of::<T>()
}
/// What each consumer holds, in bytes (estimated allocated capacity).
#[derive(Clone, Copy, Default, Debug)]
pub struct MemoryReport {
/// `Ground::known`: the answered land heights by column.
pub ground_known: usize,
/// The ground's other bookkeeping: loading, questions waiting, chunks wanted.
pub ground_other: usize,
/// `WorldCache::secs`: the block sections, table and the mixed ones' bits.
pub world_sections: usize,
/// The block cache's other bookkeeping: exact rays, queues, questions, blasts expected.
pub world_other: usize,
/// The simulation's vectors: drones (and their routes), missiles, systems, objects,
/// effects, blasts, the launch queue and the rest of the live state.
pub units: usize,
/// The radars' tracks and horizons.
pub tracks: usize,
/// The recording in progress.
pub recording: usize,
/// The replays kept.
pub replays: usize,
/// The enemy's memory of the layout (contacts and outcomes).
pub intel: usize,
/// The recon memory (sorties, marks, checked zones).
pub recon: usize,
/// The vehicles' ground routes and their searches.
pub routes: usize,
/// The raid's launch sites.
pub sites: usize,
/// The counter-strikes on the sites.
pub strikes: usize,
/// The raid being planned (its grid, prices, routes, knowledge) and the retarget jobs.
pub plan: usize,
/// The rest: fires, flares, roles, tallies, the smart refresh.
pub other: usize,
}
impl MemoryReport {
pub fn total(&self) -> usize {
self.ground_known
+ self.ground_other
+ self.world_sections
+ self.world_other
+ self.units
+ self.tracks
+ self.recording
+ self.replays
+ self.intel
+ self.recon
+ self.routes
+ self.sites
+ self.strikes
+ self.plan
+ self.other
}
/// The largest table a rehash could double while the old one is still held: the
/// transient peak is the total plus twice this (old + new − steady).
pub fn rehash_headroom(&self) -> usize {
2 * self.ground_known.max(self.world_sections)
}
/// One line per consumer, biggest first: the budget report's shape.
pub fn text(&self) -> String {
let mut rows = [
("ground.known", self.ground_known),
("ground.other", self.ground_other),
("world.secs", self.world_sections),
("world.other", self.world_other),
("sim.units", self.units),
("sim.tracks", self.tracks),
("recording", self.recording),
("replays", self.replays),
("intel", self.intel),
("recon", self.recon),
("routes", self.routes),
("sites", self.sites),
("strikes", self.strikes),
("plan", self.plan),
("other", self.other),
];
rows.sort_by(|a, b| b.1.cmp(&a.1));
let mut out = format!("total {:.2} MiB (+{:.2} MiB rehash transient)", self.total() as f64 / (1024.0 * 1024.0), self.rehash_headroom() as f64 / (1024.0 * 1024.0));
for (name, bytes) in rows {
if bytes > 0 {
out.push_str(&format!("\n {name}: {:.2} MiB", bytes as f64 / (1024.0 * 1024.0)));
}
}
out
}
}
impl Sim {
/// What the simulation and the ground cache hold, about: the budget tests measure it
/// under load, and the diagnostics can show where the memory went.
pub fn memory(&self, ground: &Ground) -> MemoryReport {
let mut r = MemoryReport::default();
r.ground_known = map_bytes(ground.known.len(), std::mem::size_of::<(i32, i32)>() + std::mem::size_of::<(i32, u64)>());
r.ground_other = map_bytes(ground.loading.len(), std::mem::size_of::<(i32, i32)>() + std::mem::size_of::<u64>())
+ vec_bytes(&ground.wanted)
+ map_bytes(ground.asked.len(), std::mem::size_of::<(i32, i32)>())
+ vec_bytes(&ground.chunks);
let (sections, other) = ground.world.memory();
r.world_sections = sections;
r.world_other = other;
r.units = vec_bytes(&self.drones)
+ self.drones.iter().map(|d| vec_bytes(&d.route)).sum::<usize>()
+ vec_bytes(&self.missiles)
+ vec_bytes(&self.sams)
+ vec_bytes(&self.targets)
+ vec_bytes(&self.target_health)
+ vec_bytes(&self.fx)
+ vec_bytes(&self.blasts)
+ vec_bytes(&self.queue)
+ vec_bytes(&self.settling)
+ vec_bytes(&self.later)
+ vec_bytes(&self.flares)
+ vec_bytes(&self.lit);
r.tracks = self.sams.iter().map(|s| vec_bytes(&s.tracks)).sum::<usize>()
+ map_bytes(self.horizons.len(), std::mem::size_of::<u32>() + std::mem::size_of::<sight::Horizon>())
+ self.horizons.values().map(|h| vec_bytes(&h.rays) + h.rays.iter().map(|ray| vec_bytes(ray)).sum::<usize>()).sum::<usize>();
r.recording = self.rec.as_ref().map_or(0, |rec| rec.bytes());
r.replays = self.replays.iter().map(|rec| rec.bytes()).sum();
r.intel = self.intel.bytes();
r.recon = vec_bytes(&self.recon.sorties)
+ btree_bytes(self.recon.marks.len(), std::mem::size_of::<u32>() + std::mem::size_of::<recon::Mark>())
+ btree_bytes(self.recon.truth.len(), 2 * std::mem::size_of::<u32>())
+ btree_bytes(self.recon.checked.len(), std::mem::size_of::<(i64, i64)>() + std::mem::size_of::<u64>());
r.routes = self.routes.bytes();
r.sites = self.sites.bytes();
r.strikes = self.strikes.bytes();
r.plan = self.plan.as_ref().map_or(0, |p| p.bytes())
+ vecdeque_bytes(&self.retargets)
+ self.retargets.iter().map(|j| j.known.bytes() + j.relief.as_ref().map_or(0, |k| k.bytes())).sum::<usize>()
+ self.known_relief.as_ref().map_or(0, |k| k.bytes());
r.other = map_bytes(self.roles.len(), std::mem::size_of::<SamKind>() + std::mem::size_of::<u8>())
+ self.fires.bytes()
+ std::mem::size_of::<record::Tally>()
+ std::mem::size_of::<Self>();
r
}
}
+6
View File
@@ -41,6 +41,8 @@ pub mod outcome;
mod units;
pub mod why;
pub mod world;
pub mod memory;
pub use memory::MemoryReport;
pub mod profile;
#[cfg(test)]
mod balance;
@@ -454,6 +456,10 @@ impl Ground {
pub fn chunks_wanted(&self) -> usize {
self.chunks.len()
}
/// How many columns' heights are cached (the budget tests and the diagnostics read it).
pub fn known_len(&self) -> usize {
self.known.len()
}
/// The chunks to ask the game to load this tick.
pub fn take_chunks(&mut self) -> Vec<(i32, i32)> {
std::mem::take(&mut self.chunks)
+17
View File
@@ -127,6 +127,23 @@ pub(super) struct Plan {
mapped: bool,
}
impl Plan {
/// What the plan holds, about: the grid's heights, prices, routes and items (see `sim::memory`).
pub(super) fn bytes(&self) -> usize {
use super::memory::{map_bytes, vec_bytes};
map_bytes(self.prices.len(), std::mem::size_of::<DroneKind>() + std::mem::size_of::<Vec<f32>>())
+ self.prices.values().map(|p| vec_bytes(p)).sum::<usize>()
+ map_bytes(self.routes.len(), std::mem::size_of::<(DroneKind, i32, i32, i32)>() + std::mem::size_of::<Vec<DVec3>>())
+ self.routes.values().map(|r| vec_bytes(r)).sum::<usize>()
+ vec_bytes(&self.items)
+ vec_bytes(&self.done)
+ vec_bytes(&self.wants)
+ vec_bytes(&self.heights)
+ vec_bytes(&self.unanswered)
+ self.knowledge.bytes()
}
}
/// The raid in progress: the counts at its start, for the report.
pub(super) struct WaveStats {
kills: u32,
+29
View File
@@ -919,6 +919,35 @@ impl Recording {
}
}
/// What the recording holds, about: tracks and their points, the event and scene
/// journals, the systems' poses and the by-time index (see `sim::memory`).
pub fn bytes(&self) -> usize {
use super::memory::{map_bytes, vec_bytes};
let mut n = vec_bytes(&self.tracks)
+ self.tracks.iter().map(|t| vec_bytes(&t.points)).sum::<usize>()
+ map_bytes(self.index.len(), std::mem::size_of::<u32>() + std::mem::size_of::<usize>())
+ vec_bytes(&self.events)
+ vec_bytes(&self.sam_tracks)
+ self.sam_tracks.iter().map(|t| vec_bytes(&t.keys)).sum::<usize>()
+ map_bytes(self.sam_index.len(), std::mem::size_of::<u32>() + std::mem::size_of::<usize>())
+ vec_bytes(&self.ticks)
+ vec_bytes(&self.times)
+ vec_bytes(&self.sams)
+ vec_bytes(&self.objects)
+ self.index_bytes()
+ std::mem::size_of::<Tally>();
if let Some(scene) = &self.scene {
n += vec_bytes(&scene.events)
+ map_bytes(scene.tracks.len(), std::mem::size_of::<(u32, u32)>() + std::mem::size_of::<(bool, bool, DVec3)>())
+ map_bytes(scene.fires.len(), std::mem::size_of::<u32>() + std::mem::size_of::<(DVec3, u32, f32, bool, bool, Option<bool>)>())
+ vec_bytes(&scene.fire_events)
+ map_bytes(scene.launches.len(), std::mem::size_of::<u32>() + std::mem::size_of::<usize>())
+ map_bytes(scene.ignitions.len(), std::mem::size_of::<u32>() + std::mem::size_of::<usize>())
+ map_bytes(scene.burnouts.len(), std::mem::size_of::<u32>() + std::mem::size_of::<usize>());
}
n
}
/// The bytes the by-time index adds to a recording (its own journal aside).
pub fn index_bytes(&self) -> usize {
let order = self.time_order.as_ref().map_or(0, |o| o.capacity() * std::mem::size_of::<u32>());
+17
View File
@@ -563,6 +563,23 @@ pub(super) struct Routes {
}
impl Routes {
/// What the routes and their searches hold, about (see `sim::memory`).
pub(super) fn bytes(&self) -> usize {
use super::memory::{map_bytes, vec_bytes};
let cell = std::mem::size_of::<(i32, i32)>();
let mut n = map_bytes(self.map.len(), std::mem::size_of::<u32>() + std::mem::size_of::<Route>())
+ map_bytes(self.unreachable.len(), std::mem::size_of::<(u32, u32)>() + std::mem::size_of::<u64>());
for r in self.map.values() {
n += map_bytes(r.cells.len(), cell + std::mem::size_of::<Info>())
+ map_bytes(r.bad.len(), cell)
+ map_bytes(r.g.len(), cell + std::mem::size_of::<f32>())
+ map_bytes(r.came.len(), 2 * cell)
+ r.open.capacity() * std::mem::size_of::<Open>()
+ vec_bytes(&r.parked)
+ vec_bytes(&r.pts);
}
n
}
pub(super) fn out_of_reach(&self, truck: u32, job: u32, now: u64) -> bool {
self.unreachable.get(&(truck, job)).is_some_and(|&t| t > now)
}
+5
View File
@@ -184,6 +184,11 @@ pub(super) enum Slot {
}
impl Sites {
/// What the sites hold, about (see `sim::memory`).
pub(super) fn bytes(&self) -> usize {
use super::memory::vec_bytes;
vec_bytes(&self.list) + vec_bytes(&self.jobs) + vec_bytes(&self.holding) + vec_bytes(&self.flying)
}
pub fn get(&self, id: u32) -> Option<&LaunchSite> {
self.list.iter().find(|s| s.id == id)
}
+7
View File
@@ -349,6 +349,13 @@ pub struct Strikes {
}
impl Strikes {
/// What the counter-strikes hold, about (see `sim::memory`).
pub(super) fn bytes(&self) -> usize {
use super::memory::vec_bytes;
vec_bytes(&self.known) + vec_bytes(&self.flights) + vec_bytes(&self.pending) + vec_bytes(&self.flown)
+ vec_bytes(&self.wrecks) + vec_bytes(&self.cancelled)
+ self.news.iter().map(|s| s.capacity()).sum::<usize>()
}
pub fn new() -> Self {
Strikes { stock: TUNE.stock, ..Default::default() }
}
+19
View File
@@ -232,6 +232,25 @@ impl WorldCache {
.map(|e| 48 + if matches!(e.fill, Fill::Mixed(_)) { 512 } else { 0 })
.sum()
}
/// What the cache holds, about: `(sections, other)` — the sections' table and mixed
/// bits, then the rays, queues, questions and blasts expected (see `sim::memory`).
pub fn memory(&self) -> (usize, usize) {
use super::memory::{map_bytes, vec_bytes};
let entry = std::mem::size_of::<Sec>() + std::mem::size_of::<Entry>();
let sections = map_bytes(self.secs.len(), entry)
+ self.secs.values().filter(|e| matches!(e.fill, Fill::Mixed(_))).count() * 512;
let ray_entry = std::mem::size_of::<(u32, [i32; 3])>() + std::mem::size_of::<RayEntry>();
let other = map_bytes(self.rays.entries.len(), ray_entry)
+ map_bytes(self.rays.by_id.len(), std::mem::size_of::<u32>() + std::mem::size_of::<(u32, [i32; 3])>())
+ vec_bytes(&self.rays.out)
+ vec_bytes(&self.urgent)
+ vec_bytes(&self.ahead)
+ map_bytes(self.queued.len(), std::mem::size_of::<Sec>())
+ map_bytes(self.asked.len(), std::mem::size_of::<Sec>() + std::mem::size_of::<u64>())
+ vec_bytes(&self.expected)
+ map_bytes(self.changed_cols.len(), std::mem::size_of::<(i32, i32)>());
(sections, other)
}
pub fn len(&self) -> usize {
self.secs.len()
}