Repository navigation
Closed
Description
We currently remove and readd modified SPTEntries(L163-165) within the loop in order to update their position in the queue:
graphhopper/core/src/main/java/com/graphhopper/routing/AbstractNonCHBidirAlgo.java
Lines 146 to 177 in 5e88f30
| private void fillEdges(SPTEntry currEdge, PriorityQueue<SPTEntry> prioQueue, IntObjectMap<SPTEntry> bestWeightMap, boolean reverse) { | |
| EdgeIterator iter = edgeExplorer.setBaseNode(currEdge.adjNode); | |
| while (iter.next()) { | |
| if (!accept(iter, currEdge.edge)) | |
| continue; | |
| final double weight = calcWeight(iter, currEdge, reverse); | |
| if (Double.isInfinite(weight)) { | |
| continue; | |
| } | |
| final int traversalId = traversalMode.createTraversalId(iter, reverse); | |
| SPTEntry entry = bestWeightMap.get(traversalId); | |
| if (entry == null) { | |
| entry = createEntry(iter, weight, currEdge, reverse); | |
| bestWeightMap.put(traversalId, entry); | |
| prioQueue.add(entry); | |
| } else if (entry.getWeightOfVisitedPath() > weight) { | |
| prioQueue.remove(entry); | |
| updateEntry(entry, iter, weight, currEdge, reverse); | |
| prioQueue.add(entry); | |
| } else | |
| continue; | |
| if (updateBestPath) { | |
| // only needed for edge-based -> skip the calculation and use dummy value otherwise | |
| double edgeWeight = traversalMode.isEdgeBased() ? weighting.calcEdgeWeight(iter, reverse) : Double.POSITIVE_INFINITY; | |
| // todo: performance - if bestWeightMapOther.get(traversalId) == null, updateBestPath will exit early and we might | |
| // have calculated the edgeWeight unnecessarily | |
| updateBestPath(edgeWeight, entry, EdgeIterator.NO_EDGE, traversalId, reverse); | |
| } | |
| } | |
| } |
This is rather expensive as PriorityQueue.remove(Object) has a complexity of O(n), checking each element in the queue for equality.
We could mark those entries instead (e.g add them to a Set) and update the Queue after the loop has finished:
Set<SPTEntry> modified = new HashSet<>();
while (iter.next()) {
//...
updateEntry(entry, iter.getEdge(), iter.getAdjNode(), origEdgeId, weight, currEdge, reverse);
modified.add(entry);
// ...
}
prioQueue.removeAll(modified);
prioQueue.addAll(modified);Metadata
Metadata
Assignees
Labels
No labels
Activity
easbar commentedon Sep 28, 2021
Ok, I see
removeAllis optimized for bulk removing multiple elements.addAllis just callingaddfor each element.Did you try this yet? Btw another option is to set a 'removed' flag for the queue entries instead of actually removing them. The flagged entries are then rejected when they are polled from the queue. I think we tried this before, but not sure how much of a difference it made and maybe it is worth trying this again.
karussell commentedon Sep 28, 2021
Could be interesting. See also #2092, #2107, #475 and #489.
easbar commentedon Sep 28, 2021
I tried this here: 04011ef
Will post benchmarks when they are ready
easbar commentedon Sep 29, 2021
Hm, unfortunately this is not faster and maybe even slightly slower, like 1.5ms vs 1.4ms. At least the way I implemented this. But maybe this can still be improved?
The strange thing I do not understand is that for non-CH my change yields a massive slow down, like 3.5s vs 0.1s. @otbutz you opened this issue for CH especially. Did you also experience that this tweak does not work for non-CH?
otbutz commentedon Sep 29, 2021
I did not check it yet. Just noticed PriorityQueue.remove(Object) taking some noticeable amount of time.
karussell commentedon Sep 29, 2021
The problem might be that PriorityQueue is extremely well tuned and hard to beat. OR that the bigger queue size is bad for iteration and polling OR that the Set makes the problems. Maybe you initialize the Set with 1000 or something or even use an ArrayList?
otbutz commentedon Sep 29, 2021
I'd profile with JMC to really get the culprit here.
https://adoptopenjdk.net/jmc.html
easbar commentedon Sep 30, 2021
I tried using an
ArrayListinstead of aHashSetfor non-CH and this was much faster (around x2), but still much slower than master. For CH it was still around the same, so no real improvement compared to master. The issue with non-CH is thatremoveAlltraverses all elements of the queue (not only the ones of the given Collection that shall be removed), so if the queue is large it takes a long time.Feel free to profile this in more detail using JMC, and re-open this issue, but currently I do not feel like trying further in this direction. If we actually wanted to speed up CH we should rather think about not using the BaseGraph/CH hybrid we are using at the moment and re-labelling the nodes according to the CH level. This would easily improve speed by around a factor of two and probably more (but require more memory).