From 24f3485ebbcafc3f0ce867d96511cd3c606277d9 Mon Sep 17 00:00:00 2001 From: chexit Date: Sat, 21 Nov 2026 18:46:19 +0300 Subject: [PATCH] save: assemble the air section with write! instead of `+= &format!` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sim::air_text built the save text with `head += &format!(...)` per contact and `line += &format!(...)` per relief run, and Drone::air_line did the same for a route point. Each of those allocated a temporary String and re-copied the whole growing buffer, so the cost was quadratic in the section's size. The head is not covered by the fits check (only the drone/queue body is): up to 8 relief grids of 40k run-length tokens each plus 4096 contact lines. On a heavy raid the autosave's tick therefore exhausted its 300M fuel while formatting — the crash was in Sim::air_text. Format directly into the buffer now (std::fmt::Write, push_str, push), which grows it amortized with no per-token temporary. Nothing else changes: the format, every MAX_* bound and the byte-budget logic are untouched. The output is byte-identical: the same air_text over a relief-heavy raid state and over the 600-drone byte-bound state hashes and measures the same before and after (64a1787d37047447, 894 and 747942 bytes). --- airdef-mod/src/sim/durable.rs | 63 ++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/airdef-mod/src/sim/durable.rs b/airdef-mod/src/sim/durable.rs index 33421c8..a3a6254 100644 --- a/airdef-mod/src/sim/durable.rs +++ b/airdef-mod/src/sim/durable.rs @@ -21,6 +21,7 @@ //! contacts, route points and relief cells — so a saved air state can never grow the file without limit. use super::*; use std::collections::{BTreeMap, HashMap}; +use std::fmt::Write; /// The section's format version. A different one is refused rather than half-read. const VERSION: u32 = 1; @@ -164,7 +165,8 @@ impl Drone { if !self.route.is_empty() { route = self.route.len().to_string(); for p in &self.route { - route += &format!(" {} {} {}", coord(p.x), coord(p.y), coord(p.z)); + // šŸ¤– Agent: format straight into the buffer, not through a temporary `String`. + write!(route, " {} {} {}", coord(p.x), coord(p.y), coord(p.z)).unwrap(); } } format!( @@ -243,23 +245,26 @@ impl Sim { let job_relief: Vec> = self.retargets.iter().map(|j| j.relief.as_ref().and_then(|r| token_of(r, &mut reliefs))).collect(); let (kills, hits) = self.intel.sectors(); // The head, with the counts filled in later; its length is what the body is measured against. + // šŸ¤– Agent: `write!`/`push_str` format into the buffer in place. `head += &format!(...)` made a + // temporary `String` per item and re-copied the whole growing head, so a full relief grid cost + // O(n²) bytes and a heavy autosave ran out of fuel. let mut head = format!("air {VERSION} {} {} 0 0 0 {} {}\n", self.tick, self.job_next, contacts.len(), reliefs.len()); - head += "airintel"; + head.push_str("airintel"); for v in kills.iter().chain(hits.iter()) { - head += &format!(" {v}"); + write!(head, " {v}").unwrap(); } - head += "\n"; + head.push('\n'); // The bearing window (#149): bounded, and optional — an older save without it loads with // an empty window and the lifetime tally it does carry. let outcomes: Vec<&ew::Learned> = self.intel.outcomes().iter().take(ew::MAX_OUTCOMES).collect(); if !outcomes.is_empty() { - head += &format!("airlearn {}", outcomes.len()); + write!(head, "airlearn {}", outcomes.len()).unwrap(); for o in &outcomes { - head += &format!(" {} {} {}", o.sector, o.tick, u8::from(o.killed)); + write!(head, " {} {} {}", o.sector, o.tick, u8::from(o.killed)).unwrap(); } - head += "\n"; + head.push('\n'); } - head += &format!("airglobal {}\n", global.map_or_else(|| "-".into(), |t| t.to_string())); + write!(head, "airglobal {}\n", global.map_or_else(|| "-".into(), |t| t.to_string())).unwrap(); for (i, r) in reliefs.iter().enumerate() { let g = r.grid(); let mut line = format!("airrelief {i} {} {} {} {} ", coord(g.min.x), coord(g.min.z), coord(g.cell), g.size); @@ -269,20 +274,21 @@ impl Sim { match run { Some((k, n)) if k == key => run = Some((k, n + 1)), Some((k, n)) => { - line += &format!("{} ", run_token(k, n)); + write!(line, "{} ", run_token(k, n)).unwrap(); run = Some((key, 1)); } None => run = Some((key, 1)), } } if let Some((k, n)) = run { - line += &run_token(k, n); + line.push_str(&run_token(k, n)); } - head += &line; - head += "\n"; + head.push_str(&line); + head.push('\n'); } for c in &contacts { - head += &format!( + write!( + head, "aircontact {} {} {} {} {} {} {} {} {} {}\n", c.id, c.kind.spec().key, @@ -294,7 +300,8 @@ impl Sim { tick(c.emission), tick(c.destroyed), c.launches - ); + ) + .unwrap(); } // The bodies go in while the section stays inside its byte bound; a drone left out takes its // job with it (a job whose drone is not saved could never be restored). @@ -307,8 +314,8 @@ impl Sim { if !fits(&head, &body, &line) { break; } - body += &line; - body += "\n"; + body.push_str(&line); + body.push('\n'); saved.insert(d.id); drone_count += 1; } @@ -318,8 +325,8 @@ impl Sim { if !fits(&head, &body, &line) { break; } - body += &line; - body += "\n"; + body.push_str(&line); + body.push('\n'); saved.insert(d.id); queued_count += 1; } @@ -340,9 +347,10 @@ impl Sim { 1, ); let mut text = head; - text += &body; + text.push_str(&body); for (job, relief) in jobs { - text += &format!( + write!( + text, "airjob {} {} {} {} {} {} {} {} {} {}\n", job.id, job.drone, @@ -354,7 +362,8 @@ impl Sim { job.priority(), job.intent.key(), relief.map_or_else(|| "-".into(), |t| t.to_string()) - ); + ) + .unwrap(); } // Recon: the marks are memory (survive a new raid), the sorties are the aircraft still // out, and the zones already reported empty. Everything is bounded, and each line is @@ -414,7 +423,8 @@ impl Sim { s.zones.len(), ); for l in &s.pending { - body += &format!( + write!( + body, "airrecobs {} {} {} {} {} {} {} {} {} {} {}\n", l.truth, coord(l.area.x), @@ -427,18 +437,19 @@ impl Sim { l.source.name(), l.observed, l.wreck, - ); + ) + .unwrap(); } for z in &s.zones { - body += &format!("airreczone {} {} {} {}\n", coord(z.center.x), coord(z.center.y), coord(z.center.z), coord(z.radius)); + write!(body, "airreczone {} {} {} {}\n", coord(z.center.x), coord(z.center.y), coord(z.center.z), coord(z.radius)).unwrap(); } if !add(&body) { break; } sorties += 1; } - text += &format!("airrecon {} {} {} {} {}\n", self.recon.next, self.recon.drone_next, marks, sorties, checked); - text += &recon_body; + write!(text, "airrecon {} {} {} {} {}\n", self.recon.next, self.recon.drone_next, marks, sorties, checked).unwrap(); + text.push_str(&recon_body); } Some(text) }