Skip to content

Sort graph for better cache locality - #3177

Merged
easbar merged 3 commits into
masterfrom
sort_graph
Jul 7, 2025
Merged

easbar merged 3 commits into
masterfrom
sort_graph

Conversation

@easbar

@easbar easbar commented Jul 7, 2025 •

Copy link
Copy Markdown
Member

This PR sorts the graph along a Hilbert curve during import. This speeds up routing, but also the node-based CH preparation. The sorting is done in-place but still requires some extra memory during import: eight bytes per node, so around 4GB for planet OSM. However, the overall memory requirement might be unchanged since other steps of the import like location index building and CH/LM preparation require more memory anyway. In any case the sorting can be disabled by setting graph.sort: false in the config file.

@easbar easbar added this to the 11.0 milestone Jul 7, 2025
@otbutz

otbutz commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

@easbar

easbar commented Jul 7, 2025

Copy link
Copy Markdown
Member Author

Here I measured the routing speed for the car profile (without turn costs, no instructions and calc_points=false) on a planet import using random queries (for 0.5km and 20km beeline distance between start and target points) for increasing Hilbert order (=increasing sorting precision):

image

The y-axis shows the routing time relative to the time I measured for the unsorted graph in % (100=no change). The connection between routing speed and Hilbert order can be seen very clearly. The more we sort the graph the faster the routing becomes, and at some point we hit a plateau where further sorting has no advantage. This depends on the routing distance, but since we use Hilbert order 31 we use a sufficiently high precision anyway. The dashed horizontal lines show the relative routing time for a randomized sorting order (much slower) and DFS sorting that we used previously (#2919) (slower, at least in this experiment).

@easbar
easbar merged commit 761468b into master Jul 7, 2025
@easbar
easbar deleted the sort_graph branch July 7, 2025 09:10
@otbutz

otbutz commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

So, if I've interpreted your graph correctly, we're talking about a ~20% improvement for routes of more than 20 km?

@easbar

easbar commented Jul 7, 2025

Copy link
Copy Markdown
Member Author

For the measurement I described, yes. For the car profile with turn costs it was more like a 13% improvement. I did not do further experiments with instructions or calc_points=true, but since there is basically no downside to the sorting I think it is worth enabling it by default anyway. I'd be happy to hear how this sorting affects performance in everyone's GraphHopper setup :)

This branch was previously deployed

1 inactive deployment
benchmarks — 691926df Deployed Jul 7, 2025 by easbar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants