Repository navigation
perf(analysis): reuse the routing graph between routes on the same network (#1998) - #2247
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
| fn route_on(graph: &Network, ds: &Dataset, from: Coord, to: Coord, snap_m: f64) -> Result<Route, String> { | ||
| crate::control::check()?; | ||
| let mut nodes = graph.nodes.clone(); | ||
| let mut adj = graph.adj.clone(); |
There was a problem hiding this comment.
Performance / memory (medium confidence): every route_on call clones the whole nodes and adj vectors. When the cached graph is near the 256 MiB cap, each route transiently allocates about the same again, and concurrent routes on one network each make their own copy. That copy isn't counted against the cache budget or memory::thread_scratch, and the doc comment on shortest_route only describes the cache. Endpoint attachments could go in a small overlay (extra node ids and edges appended after the shared graph) so the shared graph is never copied. If the copy stays, document the transient peak next to the 256 MiB claim.
There was a problem hiding this comment.
Before this PR each route built the whole graph and computed every edge's length, so the per-route copy costs less than before, not more. An overlay for the endpoints instead of a copy is left for the release quality pass.
| } | ||
| } | ||
|
|
||
| const GRAPH_CACHE_BYTES: usize = 256 << 20; |
There was a problem hiding this comment.
Memory retention (low-medium confidence): the cache holds the graph in a process-wide static for as long as the process runs, even after the project that owns the layer is closed. The Weak keeps feature data from being retained, but the graph can still be up to 256 MiB. The PR body discloses this, but nothing releases it on project close, and a Python or embedded process with several projects keeps one graph alive. Consider clearing the cache when a session drops, or making the cap smaller.
There was a problem hiding this comment.
Yes, as the PR body says: up to 256 MiB stays until a route on another network replaces it. Dropping it when the project closes is left for the release quality pass.
| let bytes = graph.bytes().saturating_add(rows.len().saturating_mul(size_of::<usize>())); | ||
| crate::control::check()?; | ||
| let mut cached = cache.lock().unwrap_or_else(|e| e.into_inner()); | ||
| *cached = (bytes <= GRAPH_CACHE_BYTES).then(|| CachedNetwork { features, rows: rows.to_vec(), cost_field, graph: graph.clone() }); |
There was a problem hiding this comment.
Test gap (AGENTS "test both ways"): the over-cap branch (bytes <= GRAPH_CACHE_BYTES).then(...) has no test. A graph above the cap should be rebuilt on each call and must not be published. Lowering the cap for a test would cover it, for example by making GRAPH_CACHE_BYTES a parameter of network_for. Also, the cold-build path and the cache-hit path both return identical results, but nothing checks that a rebuild after an over-cap call still returns the same route.
There was a problem hiding this comment.
An extra test for working code; left for the release quality pass.
| /// Heap storage retained by the cache, including spare capacity in every adjacency list. | ||
| fn bytes(&self) -> usize { | ||
| size_of::<Network>() | ||
| .saturating_add(2 * size_of::<usize>()) |
There was a problem hiding this comment.
Nit (quality pass): 2 * size_of::<usize>() is an unexplained constant. It appears to account for the Arc strong and weak counters, so a comment saying so would help. The doc comment says "heap storage", but size_of::<Network>() is the inline struct and not heap. Left for the release quality pass.
There was a problem hiding this comment.
Left for the release quality pass.
Code reviewConfidence is in brackets. I posted four inline comments on Bugs
Security
Performance
Quality
AGENTS.md
Checked, not raised: the cache-identity logic. Prior review threads: none on this PR, so no concerns were skipped as already raised. Change sizeCompared: the whole PR against main (merge base ff3ae0e to PR head 7b39a99).
|
|
On the Cancel point: whether |
📖 Docs preview
GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the |
Summary
Routing rebuilt the line network for every route. In the trial in #1998, 190 origin–destination pairs run as 190
analysis.routecalls took 35 times the reference: each call recomputed every edge's ellipsoidal length before searching.The last graph built is now kept and reused by the next route on the same network:
On the trial's 190 routes: 201 s → 33 s wall time, 139 s → 27 s CPU, with all 190 answers and exported GeoJSON files identical byte for byte.
Reaches: every surface that runs
analysis.route(the app, the command line, Python, scripts and models, the Assistant and MCP); nothing in the interface or saved files changes.Testing
route_graph_reuse_preserves_results_and_follows_network_edits: a reused graph gives bit-identical routes, endpoints don't leak between routes, an edit, a changed row set or a different cost field rebuilds, the cache doesn't keep feature data alive, and Cancel reaches both building and cached routes.cargo test -p rustgis-analysis(613 passed); the 190-route replay above.cargo xtask cion the fork's CI.UI evidence
No UI change.
Related issue
Part of #1998 (the routing graph line; service area and OD matrix stay on the roadmap).