Repository navigation
Open
Description
The current implementation defaults to costly Polygon intersection checks which requires that the tower nodes of each examined edge are loaded to create a BBox:
graphhopper/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomWeightingHelper.java
Lines 60 to 65 in 0522c49
| public static boolean in(Polygon p, EdgeIteratorState edge) { | |
| BBox bbox = GHUtility.createBBox(edge); | |
| if (p.getBounds().intersects(bbox)) | |
| return p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); // TODO PERF: cache bbox and edge wayGeometry for multiple area | |
| return false; | |
| } |
The old BlockArea implementation preloads all edge ids for small areas which allows for faster id based filtering of non-virtual edges:
graphhopper/core/src/main/java/com/graphhopper/storage/GraphEdgeIdFinder.java
Lines 214 to 239 in e806a5c
| public final boolean intersects(EdgeIteratorState edgeState) { | |
| PointList pointList = null; | |
| BBox bbox = null; | |
| for (int shapeIdx = 0; shapeIdx < blockedShapes.size(); shapeIdx++) { | |
| GHIntHashSet blockedEdges = edgesList.get(shapeIdx); | |
| // blockedEdges acts as cache that is only useful when filled and for non-virtual edges | |
| if (!blockedEdges.isEmpty() && edgeState.getEdge() < baseEdgeCount) { | |
| if (blockedEdges.contains(edgeState.getEdge())) | |
| return true; | |
| continue; | |
| } | |
| // compromise: avoid expensive fetch of pillar nodes, which isn't yet fast for being used in Weighting.calc | |
| if (bbox == null) | |
| bbox = GHUtility.createBBox(edgeState); | |
| Shape shape = blockedShapes.get(shapeIdx); | |
| if (shape.getBounds().intersects(bbox)) { | |
| if (pointList == null) | |
| pointList = edgeState.fetchWayGeometry(FetchMode.ALL).makeImmutable(); | |
| if (shape.intersects(pointList)) | |
| return true; | |
| } | |
| } | |
| return false; | |
| } |
We should implement something similar for the CustomModel handling.
Metadata
Metadata
Assignees
Labels
No labels
Activity
karussell commentedon May 12, 2021
Can you describe the problem in more detail to decide if e.g. the relative invasive proposed PR is worth the effort?
I would also like to improve the process how the GraphHopper community can better work together without disappointing contributors but also without putting a pressure on core contributors to accept contributions when they are huge. E.g. I think a draft directly means that you as the creator still need to work on it and often this can be better solved when certain open questions can be answered on the forum with a link to your branch. And then the change which is ready from your side is proposed as a PR and is also worth (from your side) to be reviewed. I understand that time could be wasted if everything is finished but this can be avoided with a discussion up-front where at least the direction is clear (e.g. the SpatialRule/SpeedModel matter is unclear and a polished PR might not be worth without a solid concept and deeper discussion).
In this issue here I think it would help what areas you used and how the performance differs (maybe even including the proposed changes as a comparison). The existing block_area API is a legacy part and not necessarily how things should be. So we should question also performance tweaks, especially if they improve a query only by 10% or something and at the same time make code more complicated or introduce more or heavy dependencies. E.g. you also found in the import procedure that point-in-polygons can be very fast, especially when we use PreparedPolygon and maybe the difference is not a big problem in reality?
otbutz commentedon May 12, 2021
Sounds good to me.
So basically:
We should add this to the docs and PR templates once this process is fully specified.
The old block_area API intersection logic is basically the same as the new one in CustomWeightingHelper:
graphhopper/core/src/main/java/com/graphhopper/storage/GraphEdgeIdFinder.java
Lines 226 to 239 in e806a5c
graphhopper/core/src/main/java/com/graphhopper/routing/weighting/custom/CustomWeightingHelper.java
Lines 60 to 65 in 0522c49
Apart from caching the bbox and edge geometry the sole difference is that BlockArea also uses edge id based filtering for small areas. If the shapes size is below a certain threshold we lookup all edge ids for it and use this set to speedup checks.
The problem isn't really the point-in-polygon logic but retrieving the geometries from the graph. This has to be done for each visited edge and is therefore quite costly as described in #2305.
Here is a flamegraph of the memory allocations for the block_area logic if it can't use edge id filtering:

All of the top entries listed above(except AstarEntry) are a result of loading geometries for these checks.
We can't avoid this for large areas but edge id filtering provides a significant speedup for smaller ones.
karussell commentedon May 12, 2021
What are the effects in real world like percentage-wise for a measurement or JMH run?
Good idea 👍
otbutz commentedon May 14, 2021
Here's a flamegraph after a few requests in flexible mode:
GraphHopper spends about 30% of its execution time creating the PointList instances for intersection checks.
otbutz commentedon May 14, 2021
Memory allocation is even worse. 95% of all allocations is caused by those PointLists and the double arrays within.
easbar commentedon May 14, 2021
Can you measure the absolute time difference for some experiment that resembles your use-case? Not sure if this helps here but getting the coordinates for tower nodes should be possible without loading the way geometry and/or creating a
PointList:otbutz commentedon May 14, 2021
GHUtility.createBBox(EdgeIteratorState)already operates based on tower nodes.easbar commentedon May 14, 2021
What do you mean?
I mean to get the tower node coordinates we do not need to use
fetchWayGeometryat all (but currently we do):graphhopper/core/src/main/java/com/graphhopper/util/GHUtility.java
Lines 1016 to 1021 in 9a0a032
otbutz commentedon May 14, 2021
Ah, that's what you meant.
otbutz commentedon May 14, 2021
But IMHO it's still better to rework geometry handling as a visitor. With this approach PointList generation becomes an opt-in.
Our current
fetchWayGeometrycould be implemented using this visitor.karussell commentedon Sep 29, 2022
Please note, that we already do not load the geometry if there are only tower nodes: https://github.com/graphhopper/graphhopper/blob/master/core/src/main/java/com/graphhopper/storage/BaseGraph.java#L404