Skip to content

Speedup handling of small areas in custom models #2313

Open

Description

@otbutz

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:

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:

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.

Activity

  1. karussell commented on May 12, 2021

    @karussell
    Member

    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?

  2. otbutz commented on May 12, 2021

    @otbutz
    ContributorAuthor

    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).

    Sounds good to me.

    So basically:

    1. Start a discussion for a bigger change in https://discuss.graphhopper.com/
    2. Create a (draft) PR after general consensus has been established
    3. Handle change requests by maintainers

    We should add this to the docs and PR templates once this process is fully specified.

    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.

    The old block_area API intersection logic is basically the same as the new one in CustomWeightingHelper:

    // 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;
    }

    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;
    }

    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.

    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?

    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:
    Screenshot_20210506_111718

    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.

  3. karussell commented on May 12, 2021

    @karussell
    Member

    What are the effects in real world like percentage-wise for a measurement or JMH run?

    We should add this to the docs and PR templates once this process is fully specified.

    Good idea 👍

  4. otbutz commented on May 14, 2021

    @otbutz
    ContributorAuthor

    Here's a flamegraph after a few requests in flexible mode:

    grafik

    GraphHopper spends about 30% of its execution time creating the PointList instances for intersection checks.

  5. otbutz commented on May 14, 2021

    @otbutz
    ContributorAuthor

    Memory allocation is even worse. 95% of all allocations is caused by those PointLists and the double arrays within.

  6. easbar commented on May 14, 2021

    @easbar
    Member

    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:

    // for example
    double lat = graph.getNodeAccess().getLat(edge.getBaseNode());
  7. otbutz commented on May 14, 2021

    @otbutz
    ContributorAuthor

    GHUtility.createBBox(EdgeIteratorState) already operates based on tower nodes.

  8. easbar commented on May 14, 2021

    @easbar
    Member

    What do you mean?

    I mean to get the tower node coordinates we do not need to use fetchWayGeometry at all (but currently we do):

    public static BBox createBBox(EdgeIteratorState edgeState) {
    PointList towerNodes = edgeState.fetchWayGeometry(FetchMode.TOWER_ONLY);
    int secondIndex = towerNodes.getSize() == 1 ? 0 : 1;
    return BBox.fromPoints(towerNodes.getLat(0), towerNodes.getLon(0),
    towerNodes.getLat(secondIndex), towerNodes.getLon(secondIndex));
    }

  9. otbutz commented on May 14, 2021

    @otbutz
    ContributorAuthor

    Ah, that's what you meant.

  10. otbutz commented on May 14, 2021

    @otbutz
    ContributorAuthor

    But IMHO it's still better to rework geometry handling as a visitor. With this approach PointList generation becomes an opt-in.

    @FunctionalInterface
    interface WayGeometryVisitor {
       default void init(int pointCount, boolean is3D) {
       }
    
       void onPoint(double lat, double lon, double ele);
    }

    Our current fetchWayGeometry could be implemented using this visitor.

  11. karussell commented on Sep 29, 2022

    @karussell
    Member

    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

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions