diff --git a/crates/engine/src/map/service.rs b/crates/engine/src/map/service.rs index dc52919269..ccf3262675 100644 --- a/crates/engine/src/map/service.rs +++ b/crates/engine/src/map/service.rs @@ -361,6 +361,8 @@ struct State { memory_bytes: usize, use_counter: u64, events: Vec, + /// The decoded bytes the `Ready` events in `events` hold (at most about [`MEMORY_BYTES`]). + events_bytes: usize, stats: ServiceStats, shutdown: bool, /// The activity row while there is work, and the jobs finished since it appeared. @@ -383,6 +385,23 @@ struct State { } impl State { + /// Report a tile that is available. The images waiting for [`TileService::poll`] are bounded + /// like those in memory: past [`MEMORY_BYTES`], the oldest waiting arrivals are dropped (a tile + /// still in memory is drawn from there; one evicted is looked for again while in view). + fn push_ready(&mut self, key: TileKey, image: Arc, stale: bool) { + self.events_bytes = self.events_bytes.saturating_add(image.rgba.len()); + self.events.push(TileEvent::Ready { key, image, stale }); + while self.events_bytes > MEMORY_BYTES { + // the oldest arrival other than the one just reported + let Some(i) = self.events.iter().position(|e| matches!(e, TileEvent::Ready { .. })).filter(|i| *i + 1 < self.events.len()) else { + break; + }; + if let TileEvent::Ready { image, .. } = self.events.remove(i) { + self.events_bytes = self.events_bytes.saturating_sub(image.rgba.len()); + } + } + } + fn wanted(&self, k: &TileKey) -> bool { self.rank.contains_key(k) } @@ -666,7 +685,9 @@ impl TileService { /// Tiles that arrived (or failed) since the last call. pub fn poll(&self) -> Vec { - std::mem::take(&mut self.shared.lock().events) + let mut st = self.shared.lock(); + st.events_bytes = 0; + std::mem::take(&mut st.events) } pub fn stats(&self) -> ServiceStats { @@ -849,7 +870,7 @@ fn disk_job(sh: &Shared, k: TileKey) { let fresh = meta.is_fresh(now); let image = Arc::new(img); st.remember(k, Arc::clone(&image), meta.expires); - st.events.push(TileEvent::Ready { key: k, image, stale: !fresh }); + st.push_ready(k, image, !fresh); if !fresh && sh.transport.is_some() && st.wanted(&k) { st.revalidated.insert(k); st.net_q.push(k); @@ -988,7 +1009,7 @@ fn net_job(sh: &Shared, k: TileKey, started: u64) { } let image = Arc::new(img); st.remember(k, Arc::clone(&image), meta.expires); - st.events.push(TileEvent::Ready { key: k, image, stale: false }); + st.push_ready(k, image, false); } Err(e) => fail(&mut st, format!("{url}: {e}"), false), } @@ -1028,13 +1049,24 @@ fn net_job(sh: &Shared, k: TileKey, started: u64) { } let image = Arc::new(img); st.remember(k, Arc::clone(&image), meta.expires); - st.events.push(TileEvent::Ready { key: k, image, stale: meta.expires <= now }); + st.push_ready(k, image, meta.expires <= now); } // replaced meanwhile: allowed to be revalidated again, against the copy there now (Err(true), _) => { st.revalidated.remove(&k); } - (Ok(_), Some(Err(e))) => fail(&mut st, format!("{url}: {e}"), false), + (Ok(_), Some(Err(e))) => { + // the cached copy is damaged: off the disk (unless replaced since), so the next + // try fetches it whole instead of validating the same bytes again + drop(st); + if let (Some(c), Some(old)) = (&sh.cache, &cached) + && c.read(k).is_some_and(|t| t.bytes == old.bytes) + { + c.remove(k); + } + let mut st = sh.lock(); + fail(&mut st, format!("{url}: {e}"), false); + } _ => fail(&mut st, format!("{url}: 304 for a tile that isn't cached"), false), } } diff --git a/crates/engine/src/map/tests_service.rs b/crates/engine/src/map/tests_service.rs index 7823811e77..1ea634fc11 100644 --- a/crates/engine/src/map/tests_service.rs +++ b/crates/engine/src/map/tests_service.rs @@ -791,6 +791,9 @@ fn large_tiles_are_kept_within_the_memory_budget() { let in_memory = svc.stats().in_memory; assert!(in_memory <= MEMORY_BYTES / (1024 * 1024 * 4), "{in_memory} tiles of 4 MB"); assert!(in_memory > 0); + // the arrivals waiting for the UI's first poll are bounded too, not every image decoded + let waiting: usize = svc.poll().iter().filter_map(|e| if let TileEvent::Ready { image, .. } = e { Some(image.rgba.len()) } else { None }).sum(); + assert!(waiting <= MEMORY_BYTES + 1024 * 1024 * 4, "{waiting} bytes waiting"); } /// A view wanting more than the memory holds: the tiles evicted are looked for again when the same