Skip to content

Speed up reading of turn costs #2084

Open

Description

@easbar

Currently we have this method in TurnCostStorage:

public double get(DecimalEncodedValue turnCostEnc, int fromEdge, int viaNode, int toEdge)

but it has two issues:

  1. It creates a new IntsRef object on every call, even though all it returns is a double
  2. It traverses the whole turn cost table for the given viaNode: For every node we store a list of entries TC_FROM|TC_TO|TC_FLAGS|TC_NEXT and calling the get method above traverses all of them and returns the first match. However, in practice we are typically only interested in the turn costs at a given node for a fixed fromEdge (or toEdge) and it would be better if we were able to skip the entries for other from/to edges right away.

Related: #269, #1945

Activity

  1. deleted a comment from karussell on Jul 15, 2020
  2. easbar commented on Sep 11, 2020

    @easbar
    MemberAuthor

    Here: e97235c, b6d7578, d4301f2, a12096a I copied the turn costs (for only one vehicle) to some int[] arrays and this lead to a significant reduction of CH preparation time. I take this as a proof that the turn cost reading is 'slow' for some reason. I was still looping over all turn cost entries of a given node and yet the turn cost reading was faster, so the question is why reading from TurnCostStorage is slower than from the int[] arrays.

    1. TurnCostStorage uses a byte[] array instead of an int[] array. However we tried changing to int[] in this branch: 2b19704 and this did not really help much.

    2. The creation of the IntsRef object. Or even the associated int[] array. For the turn costs we only need to read one int, maybe we should try what happens if we just pass it along without using the int[] array at all?

    Any other ideas?

  3. karussell commented on Sep 11, 2020

    @karussell
    Member

    I created a new branch less_new_intsrefs that creates only one IntsRef per request, but I fear this won't change the speed.

    The main problem might be that we use a linked list at the moment in TurnCostStorage:

    int nextTurnCostIndex = turnCosts.getInt(turnCostPtr + TC_NEXT);
    

    to add turn costs at any time while import where the other edge information is not yet available.

    And you implemented the turn cost access in a way where the values of one node are adjacent:

    for (int i = turnCostNodes[viaNode]; i < turnCostNodes[viaNode + 1]; i++) {
    

    This avoids one call of turnCosts.getInt per turn cost entry.

    We could try to find a way to compact our current format on e.g. graph.freeze() and use e.g. negative values as indication to hop to a different index or something similar.

    I even think this "linked list" versus "frozen" list could even help us to improve the speed for the base graph, see the effort in #2085.

  4. easbar commented on Sep 11, 2020

    @easbar
    MemberAuthor

    Oh I did not realize that the linked list is not sorted yet (its not like I did it where all turn cost entries of one node are adjacent to each other). But doesn't this mean all we need to do is sort the turn cost array inside TurnCostStorage, just like I did it with the int[] arrays? Looping through the linked list is most likely faster when the entries are next to each other.

  5. karussell commented on Sep 12, 2020

    @karussell
    Member

    Yes, let's try this :) (see the commit)

  6. easbar commented on Sep 12, 2020

    @easbar
    MemberAuthor

    Nice! Should we also remove TC_NEXT? The next entry should always be the one with index+1 after the sorting anyway?
    I mean we can use int turnCostIndex = baseGraph.getNodeAccess().getTurnCostIndex(nodeId); to store the index of the first tc entry of a given node. baseGraph.getNodeAccess().getTurnCostIndex(node+1) will be the end of the list (exclusive) (and the beginning of the one for node+1 of course). For nodes where there are no turn cost entries the two values will simply be the same.

  7. karussell commented on Sep 14, 2020

    @karussell
    Member

    It does not look like this change alone speeds up anything.

    Nice! Should we also remove TC_NEXT? The next entry should always be the one with index+1 after the sorting anyway?

    Yeah, will try to make the bytesPerEntry dynamic, but this is a bit tricky (and so I did not try this and hoped for the best) as we then have a different reading mechanism before and after freeze.

  8. easbar commented on Sep 14, 2020

    @easbar
    MemberAuthor

    Does it have to be dynamic? Can we not just store the turn cost entries in some temporary storage/array/etc and only write to the 'real' TurnCostStorage in optimize() at the end of the import?

  9. karussell commented on Sep 14, 2020

    @karussell
    Member

    Ah, did not thought about this. Will try :)

    The problem why I did not thought about this is that this increases our heap usage for the import and switching to MMAP won't decrease it. But 1. for turn costs the memory usage is very low at the moment and 2. for CH etc we already have heavy heap usage (that is currently not intended to move off-heap).

  10. karussell commented on Sep 18, 2020

    @karussell
    Member

    Only edge-based CH preparation seems to benefit a bit of this change (4%). IMO not really worth the change.

    If we additionally remove the IntsRef and EncodedValue handling the reduction is further 3% for edge-based CH prep and then the edge-based non-CH routing (small map) gets 5% faster.

    Likely this gets interesting when we have more turn costs in the graph.

  11. easbar commented on Sep 22, 2020

    @easbar
    MemberAuthor

    Here I changed the prepare graph to use the weighting instead of the sorted turn costs: 06f74f4

    Let's see how big the difference was (I remember something like 25%) and maybe try to rebase your branch to this branch: https://github.com/graphhopper/graphhopper/tree/prepare_graph_tcs to make a direct comparison?

    Update: The preparation took 543s when using the weighting directly vs. 445s on current master (+22%).

  12. easbar commented on Sep 18, 2023

    @easbar
    MemberAuthor

    Update again: Apparently the improvement of the edge-based CH preparation is very small for larger (Germany or larger) OSM extracts so I removed it again here: 841cb9e

  13. easbar commented on Sep 18, 2023

    @easbar
    MemberAuthor

    I also changed the turn cost DataAccess to use integers here: b4750c1

  14. easbar commented on Nov 27, 2023

    @easbar
    MemberAuthor

    the improvement of the edge-based CH preparation is very small

    For a planet-wide import we saw that removing the optimization increases the preparation time by around 10% from around 28h to 31h (for the car profile).

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions