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) }