You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It creates a new IntsRef object on every call, even though all it returns is a double
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.
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.
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.
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?
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.
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.
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.
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.
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' TurnCostStoragein optimize() at the end of the import?
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).
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.
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
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).
Activity
easbar commentedon Sep 11, 2020
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.
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.
The creation of the
IntsRefobject. 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?
karussell commentedon Sep 11, 2020
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:
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:
This avoids one call of
turnCosts.getIntper 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.
easbar commentedon Sep 11, 2020
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.
TurnCostStorage optimize: make turn cost entries adjacent, #2084
karussell commentedon Sep 12, 2020
Yes, let's try this :) (see the commit)
easbar commentedon Sep 12, 2020
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.karussell commentedon Sep 14, 2020
It does not look like this change alone speeds up anything.
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.
easbar commentedon Sep 14, 2020
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'
TurnCostStoragein optimize()at the end of the import?karussell commentedon Sep 14, 2020
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).
use in-memory turn cost array while constructing graph. Use DataAcces…
karussell commentedon Sep 18, 2020
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.
easbar commentedon Sep 22, 2020
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%).
easbar commentedon Sep 18, 2023
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
easbar commentedon Sep 18, 2023
I also changed the turn cost DataAccess to use integers here: b4750c1
easbar commentedon Nov 27, 2023
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).