From 08223efed57805ddb302ea681eeae03f5900b901 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Thu, 11 Nov 2021 14:10:42 -0800 Subject: [PATCH] Whoops, forgot to not leak all the RAMs --- abstutil/src/collections.rs | 4 ++++ game/src/ltn/route.rs | 4 ++++ map_model/src/map.rs | 5 +++++ map_model/src/pathfind/pathfinder.rs | 7 +++++++ 4 files changed, 20 insertions(+) diff --git a/abstutil/src/collections.rs b/abstutil/src/collections.rs index 4c221440d3..83e3e53950 100644 --- a/abstutil/src/collections.rs +++ b/abstutil/src/collections.rs @@ -230,6 +230,10 @@ impl VecMap { pub fn is_empty(&self) -> bool { self.inner.is_empty() } + + pub fn clear(&mut self) { + self.inner.clear(); + } } impl Default for VecMap { diff --git a/game/src/ltn/route.rs b/game/src/ltn/route.rs index b02be128cd..c6892785a4 100644 --- a/game/src/ltn/route.rs +++ b/game/src/ltn/route.rs @@ -115,6 +115,10 @@ impl State for RoutePlanner { let panel_outcome = self.panel.event(ctx); if let Outcome::Clicked(ref x) = panel_outcome { if x == "Back to editing modal filters" { + // We'll cache a custom pathfinder per set of avoided roads. Avoid leaking memory + // by clearing this out + app.primary.map.clear_custom_pathfinder_cache(); + return Transition::ConsumeState(Box::new(|state, ctx, app| { let state = state.downcast::().ok().unwrap(); vec![super::viewer::Viewer::new_state( diff --git a/map_model/src/map.rs b/map_model/src/map.rs index 505d314bbf..6f1c8cc639 100644 --- a/map_model/src/map.rs +++ b/map_model/src/map.rs @@ -605,6 +605,11 @@ impl Map { self.pathfinder.should_use_transit(self, start, end) } + /// Clear any pathfinders with custom RoutingParams, created previously with `cache_custom` + pub fn clear_custom_pathfinder_cache(&self) { + self.pathfinder.clear_custom_pathfinder_cache(); + } + /// Return the cost of a single path, and also a mapping from every directed road to the cost /// of getting there from the same start. This can be used to understand why an alternative /// route wasn't chosen. diff --git a/map_model/src/pathfind/pathfinder.rs b/map_model/src/pathfind/pathfinder.rs index 35eb4f266f..8fd5fcba91 100644 --- a/map_model/src/pathfind/pathfinder.rs +++ b/map_model/src/pathfind/pathfinder.rs @@ -213,6 +213,13 @@ impl Pathfinder { result } + pub fn clear_custom_pathfinder_cache(&self) { + self.cached_alternatives + .get_or(|| RefCell::new(VecMap::new())) + .borrow_mut() + .clear(); + } + pub fn all_costs_from( &self, req: PathRequest,