Skip to content

Commit 7be19e1

Browse files
karusselleasbar
andauthored
Edge CH speedup (graphhopper#1542)
* speed up of edge based CH * Makes use of Graph#getBaseNode also in queries. Signed-off-by: easbar <[email protected]> * Renames getBaseNode->getOtherNode. Signed-off-by: easbar <[email protected]> * minor tweak * Adds back EdgeAcces#getOtherNode (was removed in master). Signed-off-by: easbar <[email protected]> Co-authored-by: easbar <[email protected]>
1 parent 7b72475 commit 7be19e1

File tree

12 files changed

+57
-5
lines changed

12 files changed

+57
-5
lines changed

core/src/main/java/com/graphhopper/routing/AbstractBidirectionEdgeCHNoSOD.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ protected int getOrigEdgeId(EdgeIteratorState edge, boolean reverse) {
159159

160160
@Override
161161
protected int getTraversalId(EdgeIteratorState edge, int origEdgeId, boolean reverse) {
162-
EdgeIteratorState iterState = graph.getEdgeIteratorState(origEdgeId, edge.getAdjNode());
163-
return GHUtility.createEdgeKey(iterState.getBaseNode(), iterState.getAdjNode(), iterState.getEdge(), reverse);
162+
int baseNode = graph.getOtherNode(origEdgeId, edge.getAdjNode());
163+
return GHUtility.createEdgeKey(baseNode, edge.getAdjNode(), origEdgeId, reverse);
164164
}
165165

166166
@Override

core/src/main/java/com/graphhopper/routing/QueryGraph.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,14 @@ public GraphExtension getExtension() {
746746
return wrappedExtension;
747747
}
748748

749+
@Override
750+
public int getOtherNode(int edge, int node) {
751+
if (isVirtualEdge(edge)) {
752+
return getEdgeIteratorState(edge, node).getBaseNode();
753+
}
754+
return mainGraph.getOtherNode(edge, node);
755+
}
756+
749757
private UnsupportedOperationException exc() {
750758
return new UnsupportedOperationException("QueryGraph cannot be modified.");
751759
}

core/src/main/java/com/graphhopper/routing/ch/WitnessPathSearcher.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.graphhopper.routing.util.DefaultEdgeFilter;
2525
import com.graphhopper.routing.weighting.TurnWeighting;
2626
import com.graphhopper.storage.CHGraph;
27-
import com.graphhopper.storage.GraphHopperStorage;
2827
import com.graphhopper.util.*;
2928

3029
import java.util.Arrays;
@@ -61,7 +60,6 @@
6160
* searches.
6261
*
6362
* @author easbar
64-
*
6563
*/
6664
public class WitnessPathSearcher {
6765
private static final int NO_NODE = -1;
@@ -476,7 +474,8 @@ private CHEntry getEntryForKey(int edgeKey) {
476474
}
477475

478476
private int getEdgeKey(int edge, int adjNode) {
479-
return GHUtility.getEdgeKey(chGraph, edge, adjNode, false);
477+
int baseNode = chGraph.getOtherNode(edge, adjNode);
478+
return GHUtility.createEdgeKey(baseNode, adjNode, edge, false);
480479
}
481480

482481
private double calcTurnWeight(int inEdge, int viaNode, int outEdge) {

core/src/main/java/com/graphhopper/storage/BaseGraph.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,12 @@ public GraphExtension getExtension() {
794794
return extStorage;
795795
}
796796

797+
@Override
798+
public int getOtherNode(int edge, int node) {
799+
long edgePointer = edgeAccess.toPointer(edge);
800+
return edgeAccess.getOtherNode(node, edgePointer);
801+
}
802+
797803
public void setAdditionalEdgeField(long edgePointer, int value) {
798804
if (extStorage.isRequireEdgeField() && E_ADDITIONAL >= 0)
799805
edges.setInt(edgePointer + E_ADDITIONAL, value);

core/src/main/java/com/graphhopper/storage/CHGraph.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,5 @@ public interface CHGraph extends Graph {
9393
* @return true if contraction can be started (add shortcuts and set levels), false otherwise
9494
*/
9595
boolean isReadyForContraction();
96+
9697
}

core/src/main/java/com/graphhopper/storage/CHGraphImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ public boolean isReadyForContraction() {
234234
return isReadyForContraction;
235235
}
236236

237+
@Override
238+
public int getOtherNode(int edge, int node) {
239+
EdgeAccess edgeAccess = isShortcut(edge) ? chEdgeAccess : baseGraph.edgeAccess;
240+
long edgePointer = edgeAccess.toPointer(edge);
241+
return edgeAccess.getOtherNode(node, edgePointer);
242+
}
243+
237244
void _prepareForContraction() {
238245
if (isReadyForContraction) {
239246
return;

core/src/main/java/com/graphhopper/storage/EdgeAccess.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ final int getLinkB(long edgePointer) {
142142
return edges.getInt(edgePointer + E_LINKB);
143143
}
144144

145+
final int getOtherNode(int nodeThis, long edgePointer) {
146+
int nodeA = getNodeA(edgePointer);
147+
return nodeThis == nodeA ? getNodeB(edgePointer) : nodeA;
148+
}
149+
145150
/**
146151
* Writes plain edge information to the edges index
147152
*/

core/src/main/java/com/graphhopper/storage/Graph.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public interface Graph {
8282
*/
8383
EdgeIteratorState getEdgeIteratorState(int edgeId, int adjNode);
8484

85+
/**
86+
* @return the 'opposite' node of a given edge, so if there is an edge 3-2 and node =2 this returns 3
87+
*/
88+
int getOtherNode(int edge, int node);
89+
8590
/**
8691
* @return all edges in this graph, where baseNode will be the smaller node.
8792
*/
@@ -114,4 +119,5 @@ public interface Graph {
114119
* @return the graph extension like a TurnCostExtension
115120
*/
116121
GraphExtension getExtension();
122+
117123
}

core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ public GraphExtension getExtension() {
462462
return baseGraph.getExtension();
463463
}
464464

465+
@Override
466+
public int getOtherNode(int edge, int node) {
467+
return baseGraph.getOtherNode(edge, node);
468+
}
469+
465470
private Collection<CHGraphImpl> getAllCHGraphs() {
466471
// todo: this method is only used to have a 'view' on the two collections. we could also create this only once
467472
// as long as the graph collections are only modified in the constructor (otherwise we would have to make sure

reader-gtfs/src/main/java/com/graphhopper/reader/gtfs/GraphSupport.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ public Graph copyTo(Graph g) {
304304
public GraphExtension getExtension() {
305305
throw new UnsupportedOperationException();
306306
}
307+
308+
@Override
309+
public int getOtherNode(int edge, int node) {
310+
throw new UnsupportedOperationException();
311+
}
307312
};
308313
}
309314
}

reader-gtfs/src/main/java/com/graphhopper/reader/gtfs/RealtimeFeed.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ public Graph copyTo(Graph g) {
245245
public GraphExtension getExtension() {
246246
throw new RuntimeException();
247247
}
248+
249+
@Override
250+
public int getOtherNode(int edge, int node) {
251+
throw new UnsupportedOperationException();
252+
}
248253
};
249254

250255
Map<GtfsStorage.Validity, Integer> operatingDayPatterns = new HashMap<>(staticGtfs.getOperatingDayPatterns());

reader-gtfs/src/main/java/com/graphhopper/reader/gtfs/WrapperGraph.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,9 @@ public Graph copyTo(Graph g) {
289289
public GraphExtension getExtension() {
290290
return baseGraph.getExtension();
291291
}
292+
293+
@Override
294+
public int getOtherNode(int edge, int node) {
295+
return baseGraph.getOtherNode(edge, node);
296+
}
292297
}

0 commit comments

Comments
 (0)