maps: mamaps_dump maps the archive instead of reading it whole (#perf)
- Commit
f3038ffc08236470562fe54003c1363f1ee55157- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-11 00:42 UTC (25 days ago)
- Parents
13ecf07
What: mamaps_dump opens its input with a read-only memmap2 mapping (already a tile_build dependency) instead of std::fs::read. Every mode sees the same bytes; each now pages in only what it walks. Why: `mamaps_dump world.mamaps --mode header` is the planet build's phase-7 publish gate. It prints fields from the ~9 KB prefix, but read the whole archive into memory first: 34 s and an 81 GB anonymous allocation on the Nov 10 planet, on a box whose memory budget is the build's tightest constraint (the box also carries 254 GB of swap that an 81 GB anon allocation can push others into). Verified: planet `--mode header` 34 s / ~81 GB -> 0.00 s / 1.5 MB peak RSS, output identical to the live build's header.tsv. CA tiles: header, summary and dict output byte-identical to baseline-bin mamaps_dump. cargo test --release -p tile_build: all pass.
Diff
scripts/maps/tile_build/src/bin/mamaps_dump.rs
diff --git a/scripts/maps/tile_build/src/bin/mamaps_dump.rs b/scripts/maps/tile_build/src/bin/mamaps_dump.rsindex 7b896c78cd..46e1f1a5dc 100644--- a/scripts/maps/tile_build/src/bin/mamaps_dump.rs+++ b/scripts/maps/tile_build/src/bin/mamaps_dump.rs@@ -92,14 +92,35 @@ fn main() -> ExitCode { usage(); return ExitCode::from(2); };- let bytes = match std::fs::read(&input) {- Ok(b) => b,+ // Mapped, not read: `--mode header` is the planet build's publish gate and touches only the+ // ~9 KB prefix, but `std::fs::read` pulled the whole archive into memory first -- 81 GB and+ // 34 s on the Nov 10 planet, and an 81 GB anonymous allocation on a box whose memory budget+ // is the build's tightest constraint. Every mode sees the same bytes either way; the other+ // modes simply page in what they walk.+ let file = match std::fs::File::open(&input) {+ Ok(f) => f, Err(e) => { eprintln!("mamaps_dump: cannot read {input}: {e}"); return ExitCode::from(1); } };- match run(&bytes, &mode, only_layer.as_deref(), only_tile) {+ let empty = file.metadata().map(|m| m.len() == 0).unwrap_or(false);+ // SAFETY: a read-only mapping of a finished archive. Nothing in this process writes the+ // file; another process truncating it mid-dump could fault the read, which for a+ // diagnostic dump of a published file is the same failure a short read would be.+ let map = if empty {+ None+ } else {+ match unsafe { memmap2::Mmap::map(&file) } {+ Ok(m) => Some(m),+ Err(e) => {+ eprintln!("mamaps_dump: cannot read {input}: {e}");+ return ExitCode::from(1);+ }+ }+ };+ let bytes: &[u8] = map.as_deref().unwrap_or(&[]);+ match run(bytes, &mode, only_layer.as_deref(), only_tile) { Ok(code) => code, Err(e) => { eprintln!("mamaps_dump: {input}: {e}");