Skip to content

perf(drivingDistance): compute get_depth in O(V) - #9

Merged
sanak merged 1 commit into
developfrom
fix/drivingdistance-get-depth
Dec 5, 2026
Merged

sanak merged 1 commit into
developfrom
fix/drivingdistance-get-depth

Conversation

@sanak

@sanak sanak commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Summary

Since 3.6.0, pgr_drivingDistance (and pgr_withPointsDD) compute the depth column with detail::get_depth, which scans all vertices once per reached vertex: O(reached × V).
On a nationwide road network (3,881,601 edges, 3,711,035 vertices), a 30-minute catchment (123,945 reached nodes) takes 161 s on develop vs 2.8 s on 3.5.1.

This PR rewrites get_depth to run in O(V): build the children lists of the predecessor tree in one pass (CSR layout), then walk it breadth first from the root.

  • Same tree membership rule as before: a node has a predecessor and is within the distance
  • remove_details is still applied first when details => false (callers reuse the modified predecessors)
  • Return type (std::map<int64_t, int64_t>) and callers are unchanged

Benchmark

Release build, PostgreSQL 17, Apple M1 Pro, median of 3 runs. SELECT count(*) FROM pgr_drivingDistance('SELECT id, source, target, cost, reverse_cost FROM roads_jp', 2077846, <limit>)

case 3.5.1 develop (2cc236d) this PR rows
3 min, all edges 2,709 ms 3,465 ms 2,446 ms 771
30 min, all edges 2,786 ms 161,097 ms 2,463 ms 123,945
30 min, 40 km bbox 403 ms 23,468 ms 405 ms 123,945

The remaining ~2.4 s is reading the edges and building the graph.

Verification

  • Output identical to develop, row by row (seq, depth, start_vid, pred, node, edge, cost, agg_cost):
    • 20 queries on the sample data and a 40×40 grid (pgr_drivingDistance and pgr_withPointsDD; one/many roots, directed/undirected, equicost, details true/false): 8,934 rows, diff empty
    • md5 of the full output of the 3 min and 30 min bbox cases on the nationwide data
  • pgTAP: dijkstra/driving_distance, withPoints/withPointsDD, spanningTree pass (Debug build)
  • docqueries: driving_distance unchanged
  • New pgTAP pgtap/dijkstra/driving_distance/edge_cases/depth.pg (13 tests): there was no direct test of the depth column of pgr_drivingDistance
    • expected depths on the sample data, depth = depth(pred) + 1 on several signatures, a 2000-vertex chain, pgr_withPointsDD with details => false
    • passes on develop and on this PR; removing the remove_details call makes test 9 fail

Not in this PR (existing issues found while testing)

  • equicost with a root that is not in the graph: drivingDistance_with_equicost does continue without ++i, so only the root rows are returned (e.g. ARRAY[11, 16, 99999]), and the missing root gets depth -1 (0 without equicost)
  • pgr_withPointsDD(..., equicost => true, details => false) returns a point (negative node) in the result
  • driving_distance_withPoints_driver.cpp uses d.at(row.node) without break, unlike to_postgres.cpp (find + break)
  • Release notes entry not added yet (needs an issue number)

Assisted-by: Claude Opus 5.5 (1M context)
@sanak
sanak merged commit a304132 into develop Dec 5, 2026
168 of 232 checks passed
@sanak
sanak deleted the fix/drivingdistance-get-depth branch December 5, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant