Modern Apps git

← Modern-Apps.git

maps: box the deflate state so a tiler worker cannot overflow its stack

Commit
fe47e488ab1cb263f47db32a748aac1c80a8941a
Author
vayun-mathur <[email protected]>
When
2026-08-26 18:34 UTC (3 months ago)
Parents
6b94670

Diff

scripts/maps/osm_ingest/src/par.rs
diff --git a/scripts/maps/osm_ingest/src/par.rs b/scripts/maps/osm_ingest/src/par.rsindex d8cd7b8491..c42748bec4 100644--- a/scripts/maps/osm_ingest/src/par.rs+++ b/scripts/maps/osm_ingest/src/par.rs@@ -118,6 +118,10 @@ fn pool() -> Arc<rayon::ThreadPool> { fn build_pool(n: usize) -> std::result::Result<rayon::ThreadPool, rayon::ThreadPoolBuildError> {     rayon::ThreadPoolBuilder::new()         .num_threads(n)+        // As big as the main thread's, because that is where this work used to run.+        // rayon workers otherwise get std's 2 MiB default, so parallelising a deep+        // call path silently shrinks its stack by 4x.+        .stack_size(8 * 1024 * 1024)         .thread_name(|i| format!("osm_ingest-{i}"))         .build() }
scripts/maps/tile_build/src/gz.rs
diff --git a/scripts/maps/tile_build/src/gz.rs b/scripts/maps/tile_build/src/gz.rsindex 4486dc2ab7..fdbfde1c07 100644--- a/scripts/maps/tile_build/src/gz.rs+++ b/scripts/maps/tile_build/src/gz.rs@@ -40,8 +40,16 @@ pub fn compress(data: &[u8]) -> Vec<u8> { /// /// Output is byte-for-byte what `compress` produces — `reset` keeps the parameters, /// and `the_reusable_compressor_matches_the_one_shot_one` holds the two to it.+///+/// The state is BOXED, and that is not cosmetic. `CompressorOxide` is 65,712 bytes of+/// inline arrays — a 64 KiB LZ code buffer, a 32 KiB dictionary, a 64 KiB hash chain —+/// and the tilers hand one to `rayon`'s `map_init`, which keeps a live value per split+/// of its recursive descent. Held by value that put tens of these on a worker's stack+/// and overflowed it: a California roads build died at z15 encode with+/// `thread 'tile_build-1' has overflowed its stack`, 186 s in. On the heap the+/// per-worker slot is a pointer, and the recursion costs nothing. pub struct Compressor {-    state: miniz_oxide::deflate::core::CompressorOxide,+    state: Box<miniz_oxide::deflate::core::CompressorOxide>,     /// Grown to the largest tile seen and then reused, so the output buffer is not     /// reallocated per tile either.     scratch: Vec<u8>,@@ -65,7 +73,7 @@ impl Compressor {             0,         );         Compressor {-            state: miniz_oxide::deflate::core::CompressorOxide::new(flags),+            state: Box::new(miniz_oxide::deflate::core::CompressorOxide::new(flags)),             scratch: Vec::new(),             used: false,         }@@ -265,6 +273,22 @@ mod tests {         }     } +    /// The compressor must stay small enough to live in a per-split slot.+    ///+    /// `rayon`'s `map_init` keeps one live value per split of its recursive descent, so+    /// a large-by-value `Compressor` overflows a worker's 2 MiB stack rather than+    /// merely being wasteful — which is exactly how a California roads build died+    /// before the state was boxed. A pointer plus a `Vec` is the whole struct.+    #[test]+    fn the_compressor_state_stays_on_the_heap() {+        let size = std::mem::size_of::<Compressor>();+        assert!(+            size <= 64,+            "Compressor is {size} bytes; the deflate state must stay boxed, or the \+             tilers put one of these per rayon split on a worker's stack"+        );+    }+     #[test]     fn crc32_matches_the_known_check_value() {         // The standard CRC-32 check value for "123456789".
scripts/maps/tile_build/src/par.rs
diff --git a/scripts/maps/tile_build/src/par.rs b/scripts/maps/tile_build/src/par.rsindex 5906721c2f..9fd97b6afe 100644--- a/scripts/maps/tile_build/src/par.rs+++ b/scripts/maps/tile_build/src/par.rs@@ -110,6 +110,12 @@ fn pool() -> Arc<rayon::ThreadPool> { fn build_pool(n: usize) -> std::result::Result<rayon::ThreadPool, rayon::ThreadPoolBuildError> {     rayon::ThreadPoolBuilder::new()         .num_threads(n)+        // As big as the main thread's, because that is where this work used to run.+        // rayon workers otherwise get std's 2 MiB default, so parallelising a deep+        // call path silently shrinks its stack by 4x -- and the failure mode is an+        // abort partway through a planet build, not an error. 8 MiB matches what the+        // Rust toolchain gives `main` on Windows.+        .stack_size(8 * 1024 * 1024)         .thread_name(|i| format!("tile_build-{i}"))         .build() }