Skip to content

Commit

Permalink
lazy creation of ch+base IntsRefs for CH
Browse files Browse the repository at this point in the history
  • Loading branch information
karussell committed Feb 7, 2019
1 parent 6eeb221 commit c47493c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
21 changes: 11 additions & 10 deletions core/src/main/java/com/graphhopper/storage/BaseGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class BaseGraph implements Graph {
final BitUtil bitUtil;
final EncodingManager encodingManager;
final EdgeAccess edgeAccess;
private final int bytesForFlags;
final int bytesForFlags;
// length | nodeA | nextNode | ... | nodeB
// as we use integer index in 'egdes' area => 'geometry' area is limited to 4GB (we use pos&neg values!)
private final DataAccess wayGeometry;
Expand Down Expand Up @@ -102,7 +102,7 @@ public BaseGraph(Directory dir, final EncodingManager encodingManager, boolean w
this.edgeAccess = new EdgeAccess(edges, bitUtil) {
@Override
final EdgeIterable createSingleEdge(EdgeFilter filter) {
return new EdgeIterable(BaseGraph.this, this, filter);
return new EdgeIterable(BaseGraph.this, this, filter, true);
}

@Override
Expand Down Expand Up @@ -507,7 +507,7 @@ public EdgeIteratorState edge(int nodeA, int nodeB) {

ensureNodeIndex(Math.max(nodeA, nodeB));
int edgeId = edgeAccess.internalEdgeAdd(nextEdgeId(), nodeA, nodeB);
EdgeIterable iter = new EdgeIterable(this, edgeAccess, EdgeFilter.ALL_EDGES);
EdgeIterable iter = new EdgeIterable(this, edgeAccess, EdgeFilter.ALL_EDGES, true);
boolean ret = iter.init(edgeId, nodeB);
assert ret;
if (extStorage.isRequireEdgeField())
Expand Down Expand Up @@ -551,7 +551,7 @@ final void checkAdjNodeBounds(int adjNode) {

@Override
public EdgeExplorer createEdgeExplorer(EdgeFilter filter) {
return new EdgeIterable(this, edgeAccess, filter);
return new EdgeIterable(this, edgeAccess, filter, true);
}

@Override
Expand Down Expand Up @@ -941,8 +941,8 @@ protected static class EdgeIterable extends CommonEdgeIterator implements EdgeEx
final EdgeFilter filter;
int nextEdgeId;

public EdgeIterable(BaseGraph baseGraph, EdgeAccess edgeAccess, EdgeFilter filter) {
super(-1, edgeAccess, baseGraph);
public EdgeIterable(BaseGraph baseGraph, EdgeAccess edgeAccess, EdgeFilter filter, boolean createIntsRef) {
super(-1, edgeAccess, baseGraph, createIntsRef);

if (filter == null)
throw new IllegalArgumentException("Instead null filter use EdgeFilter.ALL_EDGES");
Expand Down Expand Up @@ -1049,7 +1049,7 @@ public AllEdgeIterator(BaseGraph baseGraph) {
}

private AllEdgeIterator(BaseGraph baseGraph, EdgeAccess edgeAccess) {
super(-1, edgeAccess, baseGraph);
super(-1, edgeAccess, baseGraph, true);
}

@Override
Expand Down Expand Up @@ -1115,14 +1115,15 @@ static abstract class CommonEdgeIterator implements EdgeIteratorState {
boolean reverse = false;
boolean freshFlags;
int edgeId = -1;
final IntsRef baseIntsRef;
IntsRef baseIntsRef;
IntsRef cachedIntsRef;

public CommonEdgeIterator(long edgePointer, EdgeAccess edgeAccess, BaseGraph baseGraph) {
public CommonEdgeIterator(long edgePointer, EdgeAccess edgeAccess, BaseGraph baseGraph, boolean createIntsRef) {
this.edgePointer = edgePointer;
this.edgeAccess = edgeAccess;
this.baseGraph = baseGraph;
this.cachedIntsRef = this.baseIntsRef = new IntsRef(baseGraph.bytesForFlags / 4);
if (createIntsRef)
this.cachedIntsRef = this.baseIntsRef = new IntsRef(baseGraph.bytesForFlags / 4);
}

@Override
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/java/com/graphhopper/storage/CHGraphImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,13 @@ public int getNumNodes() {
}

class CHEdgeIteratorImpl extends EdgeIterable implements CHEdgeExplorer, CHEdgeIterator {
final IntsRef chIntsRef;
IntsRef chIntsRef;

// /**
// * @param createBaseIntsRef for creating shortcuts or detaching them we do never need the base IntsRef object
// */
public CHEdgeIteratorImpl(BaseGraph baseGraph, EdgeAccess edgeAccess, EdgeFilter filter) {
super(baseGraph, edgeAccess, filter);
chIntsRef = new IntsRef(shortcutBytesForFlags / 4);
super(baseGraph, edgeAccess, filter, false);
}

@Override
Expand Down Expand Up @@ -618,10 +620,16 @@ public final double getWeight() {
@Override
protected final void selectEdgeAccess() {
if (nextEdgeId < baseGraph.edgeCount) {
if (baseIntsRef == null)
baseIntsRef = new IntsRef(baseGraph.bytesForFlags / 4);

// iterate over edges
edgeAccess = baseGraph.edgeAccess;
cachedIntsRef = baseIntsRef;
} else {
if (chIntsRef == null)
chIntsRef = new IntsRef(shortcutBytesForFlags / 4);

// ... or shortcuts
edgeAccess = chEdgeAccess;
cachedIntsRef = chIntsRef;
Expand Down Expand Up @@ -687,7 +695,7 @@ protected final boolean checkRange() {
if (super.checkRange())
return true;

// iterate over shortcuts
// now start iterating over shortcuts
edgeAccess = chEdgeAccess;
edgeId = 0;
edgePointer = (long) edgeId * shortcutEntryBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,6 @@ public void test0SpeedButUnblocked_Issue242() {
edge12.set(carAvSpeedEnc, 0.0).set(carAccessEnc, true).setReverse(carAccessEnc, true);
edge12.setFlags(edge12.getFlags());


RoutingAlgorithm algo = createAlgo(graph);
try {
Path p = algo.calcPath(0, 2);
Expand Down

0 comments on commit c47493c

Please sign in to comment.