Skip to content

Commit 58bd9e5

Browse files
committed
Halve the time to make mutations. Don't recalculate the map-wide router until the UI actualy needs it.
1 parent 42450dc commit 58bd9e5

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

backend/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl LTN {
336336

337337
/// Returns GJ with two LineStrings, before and after
338338
#[wasm_bindgen(js_name = compareRoute)]
339-
pub fn compare_route(&self, x1: f64, y1: f64, x2: f64, y2: f64) -> Result<String, JsValue> {
339+
pub fn compare_route(&mut self, x1: f64, y1: f64, x2: f64, y2: f64) -> Result<String, JsValue> {
340340
let pt1 = self.map.mercator.pt_to_mercator(Coord { x: x1, y: y1 });
341341
let pt2 = self.map.mercator.pt_to_mercator(Coord { x: x2, y: y2 });
342342
Ok(serde_json::to_string(&self.map.compare_route(pt1, pt2)).map_err(err_to_js)?)

backend/src/map_model.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ pub struct MapModel {
2121

2222
// TODO Wasteful, can share some
2323
pub router_original: Router,
24-
pub router_current: Router,
24+
// Calculated lazily
25+
pub router_current: Option<Router>,
2526

2627
// TODO Keep edits / state here or not?
2728
pub modal_filters: BTreeMap<RoadID, ModalFilter>,
@@ -138,7 +139,8 @@ impl MapModel {
138139
}
139140

140141
fn after_edited(&mut self) {
141-
self.router_current = Router::new(&self.roads, &self.intersections, &self.modal_filters);
142+
// Invalidate it
143+
self.router_current = None;
142144
}
143145

144146
pub fn add_many_modal_filters(
@@ -308,14 +310,23 @@ impl MapModel {
308310
Ok(())
309311
}
310312

311-
pub fn compare_route(&self, pt1: Coord, pt2: Coord) -> GeoJson {
313+
// Lazily builds the router if needed.
314+
pub fn compare_route(&mut self, pt1: Coord, pt2: Coord) -> GeoJson {
315+
if self.router_current.is_none() {
316+
self.router_current = Some(Router::new(
317+
&self.roads,
318+
&self.intersections,
319+
&self.modal_filters,
320+
));
321+
}
322+
312323
let mut features = Vec::new();
313324
if let Some(linestring) = self.router_original.route(self, pt1, pt2) {
314325
let mut f = Feature::from(Geometry::from(&self.mercator.to_wgs84(&linestring)));
315326
f.set_property("kind", "before");
316327
features.push(f);
317328
}
318-
if let Some(linestring) = self.router_current.route(self, pt1, pt2) {
329+
if let Some(linestring) = self.router_current.as_ref().unwrap().route(self, pt1, pt2) {
319330
let mut f = Feature::from(Geometry::from(&self.mercator.to_wgs84(&linestring)));
320331
f.set_property("kind", "after");
321332
features.push(f);

backend/src/scrape.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ pub fn scrape_osm(input_bytes: &[u8], study_area_name: Option<String>) -> Result
6767

6868
let modal_filters = BTreeMap::new();
6969
let router_original = Router::new(&roads, &intersections, &modal_filters);
70-
let router_current = router_original.clone();
7170

7271
Ok(MapModel {
7372
roads,
@@ -77,7 +76,7 @@ pub fn scrape_osm(input_bytes: &[u8], study_area_name: Option<String>) -> Result
7776
study_area_name,
7877

7978
router_original,
80-
router_current,
79+
router_current: None,
8180

8281
modal_filters,
8382
undo_stack: Vec::new(),

0 commit comments

Comments
 (0)