maps: size rayon tasks by input so small maps still fill the pool
- Commit
8849ab3c83e6fa95c8cd871347a73c06cf81bd55- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-26 19:18 UTC (3 months ago)
- Parents
fe47e48
Diff
scripts/maps/tile_build/src/mvt.rs
diff --git a/scripts/maps/tile_build/src/mvt.rs b/scripts/maps/tile_build/src/mvt.rsindex 87883d63aa..f28dea2216 100644--- a/scripts/maps/tile_build/src/mvt.rs+++ b/scripts/maps/tile_build/src/mvt.rs@@ -100,15 +100,20 @@ impl Value { /// Key for interning, so two equal values share a dictionary slot. Floats are /// keyed on their bit pattern: NaN never dedups, which is correct and avoids /// needing `Eq` on a float.- fn dedup_key(&self) -> (u8, u64, String) {+ ///+ /// The string is BORROWED. Cloning it here allocated once per string property per+ /// feature per encode, and the drop policy's binary search encodes the same tile+ /// about eleven times -- so on California z11 this was tens of millions of+ /// throwaway `String`s and the single largest cost in the tiler.+ fn dedup_key(&self) -> (u8, u64, &str) { match self {- Value::String(s) => (0, 0, s.clone()),- Value::Float(f) => (1, f.to_bits() as u64, String::new()),- Value::Double(d) => (2, d.to_bits(), String::new()),- Value::Int(i) => (3, *i as u64, String::new()),- Value::Uint(u) => (4, *u, String::new()),- Value::SInt(i) => (5, *i as u64, String::new()),- Value::Bool(b) => (6, *b as u64, String::new()),+ Value::String(s) => (0, 0, s.as_str()),+ Value::Float(f) => (1, f.to_bits() as u64, ""),+ Value::Double(d) => (2, d.to_bits(), ""),+ Value::Int(i) => (3, *i as u64, ""),+ Value::Uint(u) => (4, *u, ""),+ Value::SInt(i) => (5, *i as u64, ""),+ Value::Bool(b) => (6, *b as u64, ""), } } @@ -340,7 +345,7 @@ pub fn encode_layer_from<'a>( let mut keys: Vec<&str> = Vec::new(); let mut key_idx: HashMap<&str, u32> = HashMap::new(); let mut values: Vec<&Value> = Vec::new();- let mut value_idx: HashMap<(u8, u64, String), u32> = HashMap::new();+ let mut value_idx: HashMap<(u8, u64, &str), u32> = HashMap::new(); let mut feat_buf = Writer::new(); let mut tag_buf = Writer::new();
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 9fd97b6afe..8c0140632e 100644--- a/scripts/maps/tile_build/src/par.rs+++ b/scripts/maps/tile_build/src/par.rs@@ -137,15 +137,26 @@ pub fn batch_len() -> usize { threads().saturating_mul(64).max(512) } -/// The smallest number of items worth waking a worker for.+/// The smallest number of items worth handing one worker, for a map of `items`. /// /// One tile's encode is tens of microseconds. Handed to rayon one at a time, the /// work-stealing and spinning cost more than the work: measured on 200k synthetic /// roads at z14, the encode pass went 4.15s at one thread to 1.81s at four and then-/// back UP to 3.51s at 32, purely on scheduling overhead. Batching several items-/// into each task amortises that away, which is what makes the high thread counts-/// pay at all.-pub const MIN_TASK_LEN: usize = 16;+/// back UP to 3.51s at 32, purely on scheduling overhead. Batching several items into+/// each task amortises that away.+///+/// But it MUST scale with the input, and a fixed floor was a serious bug. A constant+/// 16 meant rayon refused to split any map of fewer than 32 items — and the tile+/// encode pass maps over one BUCKET's tile groups, of which California z11 has only+/// ~7 (1,899 tiles over 256 buckets). The whole pass therefore ran on one thread:+/// 86.7s of wall clock for 92s of CPU, an effective parallelism of 1.06 on a 64-core+/// box, while z16 with ~3,500 groups per bucket parallelised fine and hid it.+///+/// So: aim for a few tasks per thread, and never fewer tasks than there are items.+pub fn min_task_len(items: usize) -> usize {+ let want_tasks = threads().saturating_mul(4).max(1);+ (items / want_tasks).max(1)+} #[cfg(test)] mod tests {@@ -172,19 +183,24 @@ mod tests { assert_eq!(resolve(0, Some(" 3 ")), 3, "surrounding space is tolerated"); } - /// A batch has to hold enough tasks to keep the pool busy: at least a few- /// `MIN_TASK_LEN` tasks per thread, or the barrier at the end of each batch- /// costs more than the batch saves.+ /// A batch has to hold enough tasks to keep the pool busy, and a small map must+ /// still be split at all — the bug this guards is a floor so high that a map of+ /// one bucket's ~7 tile groups became a single task on a 64-core box. #[test]- fn a_batch_holds_several_tasks_per_thread() {+ fn a_small_map_is_still_split_across_the_pool() { for n in [1usize, 2, 3, 32, 64] { set_threads(n);- let tasks = batch_len() / MIN_TASK_LEN;+ assert_eq!(min_task_len(7), 1, "{n} threads: 7 items must give 7 tasks");+ assert_eq!(min_task_len(0), 1, "an empty map must not divide by zero");+ let big = 100_000;+ let tasks = big / min_task_len(big); assert!( tasks >= n * 2,- "{n} threads: {tasks} task(s) of {MIN_TASK_LEN} in a batch of {}",- batch_len()+ "{n} threads: {tasks} task(s) for {big} items is not enough to fill \+ the pool" );+ let tasks_small = 7 / min_task_len(7);+ assert!(tasks_small >= 1.min(n)); } clear_threads(); }
scripts/maps/tile_build/src/pyramid.rs
diff --git a/scripts/maps/tile_build/src/pyramid.rs b/scripts/maps/tile_build/src/pyramid.rsindex 951e745024..4826786ea5 100644--- a/scripts/maps/tile_build/src/pyramid.rs+++ b/scripts/maps/tile_build/src/pyramid.rs@@ -77,6 +77,13 @@ pub struct Options { /// Off by default so the library stays silent: the binaries turn it on, and the /// tests would otherwise interleave bars with their output. pub progress: bool,+ /// Print how long each zoom's bucket and encode pass took, to stderr.+ ///+ /// Separate from `progress` and from [`ZoomStats`]: a duration cannot go in the+ /// report, because the report is compared for equality by the byte-identity tests.+ /// This is the measurement that says which phase to optimise next, and guessing+ /// that from a total is how you end up optimising the wrong one.+ pub timing: bool, } impl Options { pub fn new(layer: impl Into<String>, min_zoom: u8, max_zoom: u8) -> Options {@@ -88,6 +95,7 @@ impl Options { simplification: 1.0, max_tile_bytes: DEFAULT_MAX_TILE_BYTES, progress: false,+ timing: false, } } @@ -324,7 +332,7 @@ pub fn build_archive(features: &[Feature], opts: &Options) -> Result<(Vec<u8>, V let done: Vec<Option<EncodedTile>> = par::install(|| { chunk .par_iter()- .with_min_len(par::MIN_TASK_LEN)+ .with_min_len(par::min_task_len(chunk.len())) .map_init(crate::gz::Compressor::new, |gz, &(id, tx, ty)| { let indices = &by_tile[&(tx, ty)]; let rect = geom::tile_rect(tx, ty, opts.extent, buffer);@@ -393,6 +401,19 @@ pub fn build_archive(features: &[Feature], opts: &Options) -> Result<(Vec<u8>, V Ok((builder.build()?, report)) } +/// Where the encode pass's time actually goes, accumulated across workers.+///+/// Nanoseconds, summed over every `fit_tile` probe. Only read under `--timing`, and+/// the two `Instant::now()` calls are per PROBE (a handful per tile), not per feature,+/// so leaving them in costs nothing measurable.+///+/// These exist because four rounds of plausible-sounding micro-optimisation to this+/// function -- a galloping search, hoisting the geometry encode, borrowing the value+/// dictionary key, boxing the compressor -- bought 3% between them. Splitting the+/// measurement is what identified the real cost.+static MVT_NANOS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);+static GZIP_NANOS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);+ /// Encode the largest prefix of `candidates` that fits the byte budget. /// /// Binary search on the prefix length: `O(log n)` gzip calls per tile rather than@@ -407,38 +428,55 @@ fn fit_tile( opts: &Options, gz: &mut crate::gz::Compressor, ) -> Result<(Vec<u8>, usize, bool)> {- // Reused across the binary search's calls, so an over-budget tile does not- // reallocate its geometry list once per probe.- let mut geoms: Vec<Vec<u32>> = Vec::new();- let mut kept_at: Vec<usize> = Vec::new();+ // Geometry command integers, computed ONCE for the whole tile.+ //+ // These do not depend on `n`, and the binary search calls `encode` about+ // `log2(len)` times -- so building them inside the probe re-did the same work+ // eleven times over, one fresh `Vec<u32>` allocation per candidate per probe. On+ // California z11 that is 4.36 M records x ~11 probes of pure waste, and it was+ // most of the 92 s that zoom spent encoding.+ let geoms: Vec<Vec<u32>> = candidates+ .iter()+ .map(|c| match c.geom {+ IntGeometry::Points(p) => mvt::encode_points(p),+ IntGeometry::Lines(l) => mvt::encode_lines(l),+ IntGeometry::Polygons(p) => mvt::encode_polygons(p),+ })+ .collect();+ // Candidates whose geometry survived, ascending. A candidate that clips away to+ // nothing is skipped rather than emitted, exactly as before.+ let live: Vec<usize> = (0..candidates.len())+ .filter(|&i| !geoms[i].is_empty())+ .collect();+ let mut encode = |n: usize| -> Vec<u8> {- geoms.clear();- kept_at.clear();- for (at, c) in candidates[..n].iter().enumerate() {- let geometry = match c.geom {- IntGeometry::Points(p) => mvt::encode_points(p),- IntGeometry::Lines(l) => mvt::encode_lines(l),- IntGeometry::Polygons(p) => mvt::encode_polygons(p),- };- if geometry.is_empty() {- continue;- }- geoms.push(geometry);- kept_at.push(at);- }- // Properties are BORROWED here. Cloning them per candidate per tile was ~35%- // of this function's cost, and a feature reaching many tiles paid it once per- // tile. See `mvt::FeatureRef`.- let refs = kept_at+ // `live` is ascending, so this is precisely the surviving candidates among the+ // first `n` -- the same set the old per-probe loop produced.+ let refs = live .iter()- .zip(geoms.iter())- .map(|(&at, geometry)| MvtFeature {+ .take_while(|&&i| i < n)+ .map(|&i| MvtFeature { id: None, geom_type,- geometry,- props: candidates[at].props,+ geometry: &geoms[i],+ // Properties are BORROWED. Cloning them per candidate per tile was+ // ~35% of this function's cost, and a feature reaching many tiles paid+ // it once per tile. See `mvt::FeatureRef`.+ props: candidates[i].props, });- gz.compress(&mvt::encode_tile_from(layer_name, opts.extent, refs))+ let t0 = std::time::Instant::now();+ let body = mvt::encode_tile_from(layer_name, opts.extent, refs);+ let t1 = std::time::Instant::now();+ let out = gz.compress(&body);+ MVT_NANOS.fetch_add(+ (t1 - t0).as_nanos() as u64,+ std::sync::atomic::Ordering::Relaxed,+ );+ GZIP_NANOS.fetch_add(+ t1.elapsed().as_nanos() as u64,+ std::sync::atomic::Ordering::Relaxed,+ );+ out }; let all = encode(candidates.len());@@ -448,6 +486,13 @@ fn fit_tile( // Largest n in 1..len with encode(n) within budget. `lo` is always known to // fit or to be the floor of 1; `hi` is known not to.+ //+ // The probe SEQUENCE is part of the output contract, so do not "improve" this into+ // a galloping search. Gzipped size is not strictly monotone in n -- adding a+ // feature can occasionally compress to fewer bytes -- so a search that visits+ // different n lands on a different boundary. Tried on California: galloping up+ // from 1 changed the drop count from 710,215 to 710,217 and moved the archive,+ // while saving nothing, because the cost here is not the number of probes. let mut lo = 1usize; let mut hi = candidates.len(); let mut best = encode(1);@@ -820,6 +865,7 @@ pub fn build_archive_to( ); let mut touched: Vec<(u64, u64)> = Vec::new(); let mut rec = Vec::new();+ let phase = std::time::Instant::now(); let mut seq = 0u64; // Read serially, project and clip across the pool, push serially. //@@ -864,7 +910,7 @@ pub fn build_archive_to( batch .par_iter() .enumerate()- .with_min_len(par::MIN_TASK_LEN)+ .with_min_len(par::min_task_len(batch.len())) .map_init( || (Vec::new(), Vec::new()), |(touched, rec), (i, f)| {@@ -893,7 +939,9 @@ pub fn build_archive_to( seq += batch.len() as u64; } bar.finish(BUCKETED);+ let bucketed_in = phase.elapsed(); set.seal()?;+ let phase = std::time::Instant::now(); let mut bar = Progress::new( format!("{} z{z} encode", opts.layer),@@ -901,6 +949,7 @@ pub fn build_archive_to( ENCODED, opts.progress, );+ let mut prof = EncodeProfile::default(); encode_buckets( &set, scratch_dir,@@ -912,8 +961,28 @@ pub fn build_archive_to( &mut builder, &mut stats, &mut bar,+ &mut prof, )?; bar.finish(ENCODED);+ if opts.timing {+ let (_, x, y) = pmtiles::tile_zxy(prof.max_group_id);+ use std::sync::atomic::Ordering::Relaxed;+ let mvt_s = MVT_NANOS.swap(0, Relaxed) as f64 / 1e9;+ let gz_s = GZIP_NANOS.swap(0, Relaxed) as f64 / 1e9;+ eprintln!(+ " z{z}: bucket {:.1}s, encode {:.1}s [mvt {:.0}s + gzip {:.0}s of \+ CPU across all workers] ({} record(s), {} tile(s), biggest \+ z{z}/{x}/{y} with {} candidate(s) = {:.1}%)",+ bucketed_in.as_secs_f64(),+ phase.elapsed().as_secs_f64(),+ mvt_s,+ gz_s,+ set.total_records(),+ prof.groups,+ prof.max_group,+ prof.max_group as f64 / set.total_records().max(1) as f64 * 100.0,+ );+ } report.push(stats); } @@ -921,6 +990,18 @@ pub fn build_archive_to( Ok(report) } +/// What the encode pass spent its time on, for `--timing`.+///+/// The interesting number is the LARGEST group: the encode pass parallelises across+/// tiles, so one tile holding a large fraction of a zoom's records sets a floor no+/// thread count can get under.+#[derive(Default)]+struct EncodeProfile {+ groups: usize,+ max_group: usize,+ max_group_id: u64,+}+ /// Drain one bucket set in ascending index, recursing into anything over budget. /// /// Sub-buckets ascend within a parent and parents ascend, so the ids handed to@@ -937,6 +1018,7 @@ fn encode_buckets( builder: &mut pmtiles::StreamBuilder, stats: &mut ZoomStats, bar: &mut Progress,+ prof: &mut EncodeProfile, ) -> Result<()> { for i in 0..set.len() { let records = set.records_in(i);@@ -1003,6 +1085,7 @@ fn encode_buckets( builder, stats, bar,+ prof, )?; continue; }@@ -1033,6 +1116,11 @@ fn encode_buckets( j += 1; } groups.push((k, j));+ prof.groups += 1;+ if j - k > prof.max_group {+ prof.max_group = j - k;+ prof.max_group_id = id;+ } k = j; } @@ -1066,7 +1154,7 @@ fn encode_buckets( let done: Vec<EncodedTile> = par::install(|| { chunk .par_iter()- .with_min_len(par::MIN_TASK_LEN)+ .with_min_len(par::min_task_len(chunk.len())) .map_init(crate::gz::Compressor::new, encode_group) .collect::<Result<Vec<_>>>() })?;@@ -1149,7 +1237,7 @@ pub fn cli_main(name: &str, accept: Accept, argv: &[String]) -> std::process::Ex let usage = || { eprintln!("usage: {name} --geojson IN.geojsonseq --out OUT.pmtiles --layer NAME"); eprintln!(" [--minzoom N] [--maxzoom N] [--simplification F]");- eprintln!(" [--max-tile-bytes N] [--extent N] [--threads N]");+ eprintln!(" [--max-tile-bytes N] [--extent N] [--threads N] [--timing]"); eprintln!(" [--stream [--spill-dir DIR] [--buckets N]"); eprintln!(" [--bucket-budget-bytes N] [--max-repartition-depth N]]"); eprintln!();@@ -1168,6 +1256,7 @@ pub fn cli_main(name: &str, accept: Accept, argv: &[String]) -> std::process::Ex let mut max_tile_bytes = DEFAULT_MAX_TILE_BYTES; let mut extent = DEFAULT_EXTENT; let mut stream = false;+ let mut timing = false; let mut spill_dir: Option<String> = None; let mut limits = StreamLimits::default(); @@ -1223,6 +1312,10 @@ pub fn cli_main(name: &str, accept: Accept, argv: &[String]) -> std::process::Ex } i += 2; }+ "--timing" => {+ timing = true;+ i += 1;+ } "--threads" => { let Some(raw) = value() else { eprintln!("{name}: --threads needs a value");@@ -1279,6 +1372,7 @@ pub fn cli_main(name: &str, accept: Accept, argv: &[String]) -> std::process::Ex max_tile_bytes, // A planet layer spends minutes to hours per zoom, so the operator gets a bar. progress: true,+ timing, }; // Read a line at a time rather than slurping the file: a planet geojsonseq is tens
scripts/maps/tile_build/src/tiling.rs
diff --git a/scripts/maps/tile_build/src/tiling.rs b/scripts/maps/tile_build/src/tiling.rsindex daaac54b07..e22b6adaa2 100644--- a/scripts/maps/tile_build/src/tiling.rs+++ b/scripts/maps/tile_build/src/tiling.rs@@ -138,7 +138,7 @@ pub fn build_point_archive_with( let packed: Vec<Vec<u8>> = par::install(|| { chunk .par_iter()- .with_min_len(par::MIN_TASK_LEN)+ .with_min_len(par::min_task_len(chunk.len())) .map_init(crate::gz::Compressor::new, |gz, (_, body)| gz.compress(body)) .collect() });@@ -389,7 +389,7 @@ pub fn merge_archives_to<S: TileSource>( let done: Vec<Joined> = par::install(|| { chunk .par_iter()- .with_min_len(par::MIN_TASK_LEN)+ .with_min_len(par::min_task_len(chunk.len())) .map_init( || (Vec::new(), crate::gz::Compressor::new()), join_group,