From ff02e119d5a2b6bdc147b80976c3675a1ecc947c Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 1 Dec 2026 08:38:37 -0600 Subject: [PATCH] perf(tiles): fill a window's gaps a tile at a time while its tiles load While a raster read in place waits for its tiles, the other levels kept stand in for them. Each missing pixel looked its tile up at every other level, also at levels with nothing kept, at every redraw. A level's tile is now looked up once for the run of pixels in it, and passed over whole when it isn't kept. The picture is the same. Part of #2761. --- crates/tiles/src/cog/raster.rs | 91 ++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 21 deletions(-) diff --git a/crates/tiles/src/cog/raster.rs b/crates/tiles/src/cog/raster.rs index 73d34dbd1e..b43bae2740 100644 --- a/crates/tiles/src/cog/raster.rs +++ b/crates/tiles/src/cog/raster.rs @@ -449,35 +449,45 @@ fn stand_in( let mut found: HashMap> = HashMap::new(); open.retain(|g| { let (Some(&b), Some(dst)) = (bands.get(g.k), data.get_mut(g.k)) else { return false }; + // The level's pixel under each column's centre, and the runs of columns in one of its + // tiles: a tile not kept is passed over a run at a time, not a pixel at a time (#2761). + let across = |x: u32| (((f64::from(x) + 0.5) * sx) as u32).min(ol.width.saturating_sub(1)); + let columns: Vec = (g.cols.0..g.cols.1).map(across).collect(); + let runs: Vec<(u32, &[u32], usize)> = columns + .chunk_by(|a, b| a / tw == b / tw) + .scan((g.cols.0 - cols.0) as usize, |at, run| { + let from = *at; + *at += run.len(); + Some((run.first().map_or(0, |x| x / tw), run, from)) + }) + .collect(); // Whether a pixel still empty found that level's tile not kept. let mut short = false; - // The chunk the last pixel came from: its neighbours mostly share it. - let mut last: Option<(u32, Option)> = None; for y in g.rows.0..g.rows.1 { let oy = (((f64::from(y) + 0.5) * sy) as u32).min(ol.height.saturating_sub(1)); let row = (y - rows.0) as usize * ww; - for x in g.cols.0..g.cols.1 { - let Some(v) = dst.get_mut(row + (x - cols.0) as usize).filter(|v| v.is_nan()) else { continue }; - // The pixel of that level under this one's centre. - let ox = (((f64::from(x) + 0.5) * sx) as u32).min(ol.width.saturating_sub(1)); - let tile = (oy / th) * ol.across() + ox / tw; - let Some((c, at)) = info.layout.chunk(ol, tile, b) else { + for (tx, run, from) in &runs { + let Some(out) = dst.get_mut(row + from..row + from + run.len()) else { continue }; + // Filled already: that level isn't asked for these pixels. + if !out.iter().any(|v| v.is_nan()) { + continue; + } + let Some((c, at)) = info.layout.chunk(ol, (oy / th) * ol.across() + tx, b) else { short = true; continue; }; - if last.as_ref().is_none_or(|(lc, _)| *lc != c) { - let chunk = found.entry(c).or_insert_with(|| kept(o, c)).clone(); - last = Some((c, chunk)); - } - let value = last - .as_ref() - .and_then(|(_, ch)| ch.as_ref()) - .and_then(|ch| ch.get(at)) - .and_then(|p| p.get(((oy % th) * tw + ox % tw) as usize)); - match value { - // That level's value, no data included: it is what the image has there. - Some(s) => *v = *s, - None => short = true, + let chunk = found.entry(c).or_insert_with(|| kept(o, c)); + let plane = chunk.as_ref().and_then(|ch| ch.get(at)); + let Some(line) = plane.and_then(|p| p.get(((oy % th) * tw) as usize..)) else { + short = true; + continue; + }; + for (v, ox) in out.iter_mut().zip(*run).filter(|(v, _)| v.is_nan()) { + match line.get((ox % tw) as usize) { + // That level's value, no data included: it is what the image has there. + Some(s) => *v = *s, + None => short = true, + } } } } @@ -597,6 +607,45 @@ mod tests { assert_eq!(e, format!("the GeoTIFF has 1 bands, not {}", usize::MAX)); } + /// A gap takes each pixel from the nearest level kept, whichever of its tiles are, as looking + /// each pixel up by itself does: across tile edges, in sizes that don't halve evenly (#2761). + #[test] + fn stand_ins_take_each_pixel_from_the_nearest_level_kept() { + let mut info = info_of(example_with(&Example { size: 64, tile: 16, ..Example::default() })); + let first = info.layout.levels[0].clone(); + let level = |w: u32, h: u32, t: u32| Level { width: w, height: h, tile_width: t, tile_height: t, ..first.clone() }; + info.layout.levels = vec![level(157, 121, 16), level(79, 61, 16), level(40, 31, 8), level(20, 16, 8)]; + // A tile's value says its level, its number and its pixel. + let tile = |o: usize, c: u32| { + let n = info.layout.levels[o].tile_width.pow(2); + let v = move |i: u32| (o as u32 * 1_000_000 + c * 1_000 + i) as f32; + Bands::new(vec![(0..n).map(v).collect::>()].into()) + }; + for (level, cols, rows) in [(1, (3, 71), (5, 58)), (0, (40, 157), (33, 121)), (2, (0, 40), (1, 31))] { + for seed in 0..40u32 { + let has = |o: usize, c: u32| o != level && (c * 31 + o as u32 * 17 + seed * 13) % 5 < seed % 5; + let kept = |o: usize, c: u32| has(o, c).then(|| tile(o, c)); + let (mut data, gaps) = assemble(&info, level, cols, rows, &[0], |_| None); + stand_in(&info, level, cols, rows, &[0], &mut data, &gaps, kept); + let l = &info.layout.levels; + for (i, got) in data[0].iter().enumerate() { + let w = (cols.1 - cols.0) as usize; + let (x, y) = (cols.0 + (i % w) as u32, rows.0 + (i / w) as u32); + let mut want = f32::NAN; + for o in (1..4).flat_map(|d| [level.checked_sub(d), Some(level + d)]).flatten().filter(|o| *o < 4) { + let at = |v: u32, from: u32, to: u32| (((f64::from(v) + 0.5) * f64::from(to) / f64::from(from)) as u32).min(to - 1); + let (ox, oy, t) = (at(x, l[level].width, l[o].width), at(y, l[level].height, l[o].height), l[o].tile_width); + let c = oy / t * l[o].across() + ox / t; + if want.is_nan() && has(o, c) { + want = tile(o, c)[0][(oy % t * t + ox % t) as usize]; + } + } + assert_eq!(got.to_bits(), want.to_bits(), "level {level}, seed {seed}, pixel {x}, {y}"); + } + } + } + } + /// A window's missing tiles are filled from the level kept, but not its own no-data pixels. #[test] fn stand_ins_fill_only_the_tiles_missing() {