LRC-MAP-VIEW: tiles waiting to be drawn count against the memory budget

From review on the Map view: the images in arrival events waiting for the UI's
next poll were not counted, so a burst of large tiles before a frame could hold
far more than the decoded-memory budget (96 MB for 24 tiles of 1024 px). They are
now bounded by it too: past it, the oldest waiting arrivals are dropped (a tile
still in memory is drawn from there; one evicted is looked for again while it is
in view, the map repainting every 100 ms while a tile is missing).

Also: a cached copy that fails to decode after a 304 is taken off the disk
(unless replaced since), so the next try fetches it whole.
This commit is contained in:
Bruno D. committed 2026-12-02 17:52:48 -05:00
1 parent 61e9037cd5
commit 63dc151d2e
2 files changed
+40 -5

No files matched your search

+37 -5
View File
@@ -361,6 +361,8 @@ struct State {
memory_bytes: usize,
use_counter: u64,
events: Vec<TileEvent>,
/// 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<TileImage>, 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<TileEvent> {
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),
}
}
+3
View File
@@ -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