From e46ad43e043ad38788a7cf27b926f540f5aa3021 Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 25 Nov 2026 21:54:10 -0600 Subject: [PATCH] perf(map): a level the display copy found saves too little isn't thinned again (#1227) A layer drawn zoomed in on Web Mercator first tries a display copy at the view's level. When that copy drops too few vertices it is refused, and the exact path then built the same level again from the same rows, only to refuse it for the same reason: a second pass over the whole layer for nothing. The display attempt now says why it made no level, and the exact path skips a level it found saves too little. --- crates/map/src/lib.rs | 49 +++++++++++++++++++++++++++++++++---------- crates/map/src/lod.rs | 31 ++++++++++++++++++++------- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index dd71189725..22d6ebce7b 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -308,9 +308,10 @@ pub struct Layer { /// The active Mercator display level; full projection stays lazy for interaction. #[serde(skip)] display: Option>, - /// Recent unsuccessful display attempts, keyed by their geometry, level and budget inputs. + /// Recent unsuccessful display attempts, keyed by their geometry, level and budget inputs, + /// with why each made no level. #[serde(skip)] - display_rejected: Vec, + display_rejected: Vec<(lod::Rejected, lod::Refused)>, /// Recent display levels, shared by snapshots and bounded together with `thinned`. #[serde(skip)] display_levels: Vec>, @@ -1337,15 +1338,18 @@ impl Layer { return true; } let attempt = lod::Rejected { revision: self.geometry_rev, level, rows: self.dataset.features.len(), budget, whole_layer_eligible }; - if let Some(i) = self.display_rejected.iter().position(|a| *a == attempt) { + if let Some(i) = self.display_rejected.iter().position(|(a, _)| *a == attempt) { let hit = self.display_rejected.remove(i); self.display_rejected.insert(0, hit); return false; } - let Some(display) = lod::display(&self.dataset.features, rows, self.geometry_rev, level, budget, whole_layer_eligible) else { - self.display_rejected.insert(0, attempt); - self.display_rejected.truncate(lod::LEVELS_KEPT); - return false; + let display = match lod::display(&self.dataset.features, rows, self.geometry_rev, level, budget, whole_layer_eligible) { + Ok(display) => display, + Err(why) => { + self.display_rejected.insert(0, (attempt, why)); + self.display_rejected.truncate(lod::LEVELS_KEPT); + return false; + } }; let display = std::sync::Arc::new(display); self.display_levels.insert(0, display.clone()); @@ -1436,9 +1440,23 @@ impl Layer { } None => { let t = lod::tolerance(level); + let lonlat_rows = matches!(flat, projection::Flat::Mercator) && self.dataset.coord_crs() == Crs::LonLat; + // A display attempt that built this level from the same rows found it drops too + // few vertices ([`lod::Refused::SavesTooLittle`]): the copy below is built the same + // way, its saving counted against the same vertices (Web Mercator moves none), so + // it would be dropped too. + let saves_too_little = lonlat_rows + && self.display_rejected.iter().any(|(a, why)| { + *why == lod::Refused::SavesTooLittle(lod::Rows::LonLat) + && a.revision == self.geometry_rev + && a.level == level + && a.rows == self.dataset.features.len() + }); // Projected and thinned at once from longitude/latitude rows; from any other // system, the projected geometry is thinned. - let copy = if matches!(flat, projection::Flat::Mercator) && self.dataset.coord_crs() == Crs::LonLat { + let copy = if saves_too_little { + None + } else if lonlat_rows { lod::project_budgeted(&self.dataset.features, lod::Rows::LonLat, level, budget) } else { lod::thin_budgeted(&self.projected, t, budget) @@ -4491,7 +4509,7 @@ mod tests { l.clear_cache(); assert!(l.display_cache().is_none()); assert_eq!(l.view_cache_bytes(), rustgis_data::source::memory::features_bytes(&original)); - assert!(lod::display(&original, lod::Rows::LonLat, 0, 12, 1, false).is_none()); + assert!(lod::display(&original, lod::Rows::LonLat, 0, 12, 1, false).is_err()); assert!(!long_line(100).prepare_display(&flat, 12_000.0, false)); assert_eq!(l.dataset.features, original); } @@ -4668,7 +4686,7 @@ mod tests { let copy = lod::project_budgeted(&l.dataset.features, lod::Rows::LonLat, 20, usize::MAX).unwrap(); let budget = lod::cache_bytes(©); assert_eq!(lod::project_budgeted(&l.dataset.features, lod::Rows::LonLat, 20, budget).unwrap(), copy); - assert!(lod::display(&l.dataset.features, lod::Rows::LonLat, 0, 20, budget, true).is_none()); + assert!(lod::display(&l.dataset.features, lod::Rows::LonLat, 0, 20, budget, true).is_err()); } /// Tilted bands and zoom reversals reuse bounded display levels shared by snapshots. @@ -4928,7 +4946,12 @@ mod tests { let mut l = long_line(lod::THIN_ABOVE + 1000); assert!(!l.prepare_display(&flat, 0.001, false)); let built = lod::DISPLAY_BUILDS.get(); - l.thinned_in(&flat, "mercator", 0.001); + let levels = lod::LEVEL_BUILDS.get(); + let drawn = l.thinned_in(&flat, "mercator", 0.001).to_vec(); + // The level the display attempt found saves too little isn't built again: the full + // projection is drawn, as when it was. + assert_eq!(lod::LEVEL_BUILDS.get(), levels, "the refused level is built once"); + assert_eq!(drawn, l.projected_cache()); assert!(!l.prepare_display(&flat, 0.001, false)); assert_eq!(lod::DISPLAY_BUILDS.get(), built, "thinned_in builds geometry only"); l.thinned_in(&flat, "mercator", 0.001); @@ -4937,6 +4960,10 @@ mod tests { l.touch_geometry(); assert!(!l.prepare_display(&flat, 0.001, false)); assert_eq!(lod::DISPLAY_BUILDS.get(), built + 1, "a changed geometry must retry"); + // A level no display attempt refused is built by thinned_in itself. + let levels = lod::LEVEL_BUILDS.get(); + l.thinned_in(&flat, "mercator", 0.0005); + assert_eq!(lod::LEVEL_BUILDS.get(), levels + 1); } #[test] diff --git a/crates/map/src/lod.rs b/crates/map/src/lod.rs index 05d2d2d52d..48fc494578 100644 --- a/crates/map/src/lod.rs +++ b/crates/map/src/lod.rs @@ -57,10 +57,23 @@ pub(crate) struct Rejected { pub whole_layer_eligible: bool, } +/// Why [`display`] made no level. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Refused { + /// Too few vertices to thin, or past the budget. + Unfit, + /// Built from rows on the map as `Rows` has them, and found to drop fewer than + /// [`MIN_SAVING`] of the vertices: a copy built the same way, by [`project_budgeted`], would + /// too. + SavesTooLittle(Rows), +} + #[cfg(test)] thread_local! { /// Counts display construction on this test thread, including candidates later rejected. pub(crate) static DISPLAY_BUILDS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// Counts the levels [`project_budgeted`] builds on this test thread, kept or not. + pub(crate) static LEVEL_BUILDS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// One bounded display level, independent of the full geometry used for interaction. @@ -111,20 +124,20 @@ pub(crate) fn display( level: i32, budget: usize, whole_layer_eligible: bool, -) -> Option { +) -> Result { #[cfg(test)] DISPLAY_BUILDS.set(DISPLAY_BUILDS.get() + 1); let eligible: usize = features.iter().filter_map(|f| f.geometry.as_ref()).map(thinnable).sum(); if !whole_layer_eligible && eligible <= THIN_ABOVE { - return None; + return Err(Refused::Unfit); } - let extent_bytes = features.len().checked_mul(std::mem::size_of::())?; - let budget = budget.checked_sub(extent_bytes)?; - let geometries = project_budgeted(features, rows, level, budget)?; + let extent_bytes = features.len().checked_mul(std::mem::size_of::()).ok_or(Refused::Unfit)?; + let budget = budget.checked_sub(extent_bytes).ok_or(Refused::Unfit)?; + let geometries = project_budgeted(features, rows, level, budget).ok_or(Refused::Unfit)?; let all: usize = features.iter().filter_map(|f| f.geometry.as_ref()).map(vertices).sum(); let kept: usize = geometries.iter().flatten().map(vertices).sum(); if !whole_layer_eligible && kept as f64 > all as f64 * (1.0 - MIN_SAVING) { - return None; + return Err(Refused::SavesTooLittle(rows)); } // Mercator is monotone in both axes; two opposite corners bound every source vertex. Rows in // map coordinates already have their extents there. @@ -142,8 +155,8 @@ pub(crate) fn display( } }) .collect(); - let bytes = cache_bytes(&geometries).checked_add(extent_bytes)?; - Some(Display { revision, level, whole_layer_eligible, geometries, extents, bytes }) + let bytes = cache_bytes(&geometries).checked_add(extent_bytes).ok_or(Refused::Unfit)?; + Ok(Display { revision, level, whole_layer_eligible, geometries, extents, bytes }) } /// Builds only display geometry within `budget`: Mercator from rows in longitude/latitude, the @@ -151,6 +164,8 @@ pub(crate) fn display( /// their own eligibility and saving rules; exact consumers already have extents and need no second /// extent table. pub(crate) fn project_budgeted(features: &[rustgis_data::Feature], rows: Rows, level: i32, budget: usize) -> Option>> { + #[cfg(test)] + LEVEL_BUILDS.set(LEVEL_BUILDS.get() + 1); let tol = tolerance(level); map_budgeted( features,