Skip to content

Commit 5ae9b7f

Browse files
author
Karl Hübner
committed
Using BitUtil instead of new Helper methods
TurnEntryItem#getItemId() is faster now OSMTurnRelation#getRestrictionsAsEntries is not static anymore
1 parent a8f05da commit 5ae9b7f

File tree

6 files changed

+38
-52
lines changed

6 files changed

+38
-52
lines changed

core/src/main/java/com/graphhopper/reader/OSMReader.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.graphhopper.storage.GraphHopperStorage;
4545
import com.graphhopper.storage.GraphStorage;
4646
import com.graphhopper.storage.TurnCostStorage;
47+
import com.graphhopper.util.BitUtil;
4748
import com.graphhopper.util.DistanceCalc;
4849
import com.graphhopper.util.DistanceCalcEarth;
4950
import com.graphhopper.util.DouglasPeucker;
@@ -453,14 +454,20 @@ public void processRelation( OSMRelation relation ) throws XMLStreamException
453454

454455
public long getOsmIdOfInternalEdge( int edgeId )
455456
{
456-
return Helper.intToLong(edgeOsmIds.getInt(edgeId * 8), edgeOsmIds.getInt(edgeId * 8 + 4));
457+
BitUtil bitUtil = BitUtil.get(dir.getByteOrder());
458+
459+
byte[] bytesOsmWayId = new byte[8];
460+
edgeOsmIds.getBytes(edgeId * 8, bytesOsmWayId, 8);
461+
462+
return bitUtil.toLong(bytesOsmWayId);
457463
}
458464

459465
public int getInternalNodeIdOfOsmNode( long nodeOsmId )
460466
{
461467
int id = getNodeMap().get(nodeOsmId);
462-
if(id < TOWER_NODE) {
463-
return -id - 3;
468+
if (id < TOWER_NODE)
469+
{
470+
return -id - 3;
464471
}
465472
return EMPTY;
466473
}
@@ -734,9 +741,11 @@ EdgeIteratorState addEdge( int fromIndex, int toIndex, PointList pointList, long
734741
private void storeOSMWayID( int edgeId, long osmWayID )
735742
{
736743
long ptr = (long) edgeId * 8;
744+
745+
BitUtil bitUtil = BitUtil.get(dir.getByteOrder());
746+
737747
edgeOsmIds.incCapacity(ptr + 8);
738-
edgeOsmIds.setInt(ptr, Helper.longToIntLeft(osmWayID));
739-
edgeOsmIds.setInt(ptr + 4, Helper.longToIntRight(osmWayID));
748+
edgeOsmIds.setBytes(ptr, bitUtil.fromLong(osmWayID), 8);
740749
}
741750

742751
/**

core/src/main/java/com/graphhopper/reader/OSMTurnRelation.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,16 @@ private boolean isValid()
4747
/**
4848
* transforms this relation into a collection of node cost entries
4949
*
50-
* @param g the graph which provides node cost tables
5150
* @param edgeOutFilter an edge filter which only allows outgoing edges
5251
* @param edgeInFilter an edge filter which only allows incoming edges
5352
* @return a collection of node cost entries which can be added to the graph later
5453
*/
55-
public static Collection<TurnCostTableEntry> getRestrictionAsEntries( OSMTurnRelation turn, TurnCostEncoder encoder,
54+
public Collection<TurnCostTableEntry> getRestrictionAsEntries( TurnCostEncoder encoder,
5655
EdgeExplorer edgeOutExplorer, EdgeExplorer edgeInExplorer, OSMReader osmReader )
5756
{
5857
final Set<TurnCostTableEntry> entries = new HashSet<TurnCostTableEntry>();
5958

60-
int viaNodeId = osmReader.getInternalNodeIdOfOsmNode(turn.viaOsm);
59+
int viaNodeId = osmReader.getInternalNodeIdOfOsmNode(this.viaOsm);
6160

6261
try
6362
{
@@ -73,7 +72,7 @@ public static Collection<TurnCostTableEntry> getRestrictionAsEntries( OSMTurnRel
7372

7473
while (iter.next())
7574
{
76-
if (osmReader.getOsmIdOfInternalEdge(iter.getEdge()) == turn.fromOsm)
75+
if (osmReader.getOsmIdOfInternalEdge(iter.getEdge()) == this.fromOsm)
7776
{
7877
edgeIdFrom = iter.getEdge();
7978
break;
@@ -84,14 +83,14 @@ public static Collection<TurnCostTableEntry> getRestrictionAsEntries( OSMTurnRel
8483
iter = edgeOutExplorer.setBaseNode(viaNodeId);
8584
if (edgeIdFrom != EdgeIterator.NO_EDGE)
8685
{
87-
if (turn.restriction == TYPE_NO_U_TURN || turn.restriction == TYPE_NO_LEFT_TURN || turn.restriction == TYPE_NO_RIGHT_TURN
88-
|| turn.restriction == TYPE_NO_STRAIGHT_ON)
86+
if (this.restriction == TYPE_NO_U_TURN || this.restriction == TYPE_NO_LEFT_TURN || this.restriction == TYPE_NO_RIGHT_TURN
87+
|| this.restriction == TYPE_NO_STRAIGHT_ON)
8988
{
9089
// if we have a restriction of TYPE_NO_* we add restriction only to
9190
// the given turn (from, via, to)
9291
while (iter.next())
9392
{
94-
if (iter.getEdge() != edgeIdFrom && osmReader.getOsmIdOfInternalEdge(iter.getEdge()) == turn.toOsm)
93+
if (iter.getEdge() != edgeIdFrom && osmReader.getOsmIdOfInternalEdge(iter.getEdge()) == this.toOsm)
9594
{
9695
final TurnCostTableEntry entry = new TurnCostTableEntry();
9796
entry.nodeVia = viaNodeId;
@@ -102,14 +101,14 @@ public static Collection<TurnCostTableEntry> getRestrictionAsEntries( OSMTurnRel
102101
}
103102
}
104103

105-
} else if (turn.restriction == TYPE_ONLY_RIGHT_TURN || turn.restriction == TYPE_ONLY_LEFT_TURN
106-
|| turn.restriction == TYPE_ONLY_STRAIGHT_ON)
104+
} else if (this.restriction == TYPE_ONLY_RIGHT_TURN || this.restriction == TYPE_ONLY_LEFT_TURN
105+
|| this.restriction == TYPE_ONLY_STRAIGHT_ON)
107106
{
108107
// if we have a restriction of TYPE_ONLY_* we add restriction to
109108
// any turn possibility (from, via, * ) except the given turn
110109
while (iter.next())
111110
{
112-
if (iter.getEdge() != edgeIdFrom && osmReader.getOsmIdOfInternalEdge(iter.getEdge()) != turn.toOsm)
111+
if (iter.getEdge() != edgeIdFrom && osmReader.getOsmIdOfInternalEdge(iter.getEdge()) != this.toOsm)
113112
{
114113
final TurnCostTableEntry entry = new TurnCostTableEntry();
115114
entry.nodeVia = viaNodeId;
@@ -124,7 +123,7 @@ public static Collection<TurnCostTableEntry> getRestrictionAsEntries( OSMTurnRel
124123
} catch (Exception e)
125124
{
126125
LoggerFactory.getLogger(OSMTurnRelation.class).warn(
127-
"Could not built node costs table for relation of node [osmId:" + turn.viaOsm + "].", e);
126+
"Could not built node costs table for relation of node [osmId:" + this.viaOsm + "].", e);
128127
}
129128
return entries;
130129
}
@@ -140,15 +139,11 @@ public static class TurnCostTableEntry
140139
public long flags;
141140

142141
/**
143-
* @return an unique id if (edgeFrom, edgeTo, nodeVia) to avoid doubled entries during parsing
142+
* @return an unique id (edgeFrom, edgeTo) to avoid doubled entries during parsing
144143
*/
145-
public int getItemId()
144+
public long getItemId()
146145
{
147-
int id = 1;
148-
id = id * 11 + edgeFrom;
149-
id = id * 3 + edgeTo;
150-
id = id * 7 + nodeVia;
151-
return id;
146+
return ((long)edgeFrom) << 32 | ((long)edgeTo);
152147
}
153148
}
154149

core/src/main/java/com/graphhopper/routing/util/CarFlagEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public Collection<TurnCostTableEntry> analyzeTurnRelation( OSMTurnRelation turnR
249249
edgeOutExplorer = osmReader.getGraphStorage().createEdgeExplorer(new DefaultEdgeFilter(this, false, true));
250250
edgeInExplorer = osmReader.getGraphStorage().createEdgeExplorer(new DefaultEdgeFilter(this, true, false));
251251
}
252-
return OSMTurnRelation.getRestrictionAsEntries(turnRelation, this, edgeOutExplorer, edgeInExplorer, osmReader);
252+
return turnRelation.getRestrictionAsEntries(this, edgeOutExplorer, edgeInExplorer, osmReader);
253253
}
254254

255255
@Override

core/src/main/java/com/graphhopper/routing/util/EncodingManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
package com.graphhopper.routing.util;
1919

20+
import gnu.trove.map.TLongObjectMap;
21+
import gnu.trove.map.hash.TLongObjectHashMap;
22+
2023
import java.util.ArrayList;
2124
import java.util.Arrays;
2225
import java.util.Collection;
@@ -398,7 +401,7 @@ private static int determineRequiredBits( int value )
398401

399402
public Collection<TurnCostTableEntry> analyzeTurnRelation( OSMTurnRelation turnRelation, OSMReader osmReader )
400403
{
401-
Map<Integer, TurnCostTableEntry> entries = new HashMap<Integer, TurnCostTableEntry>();
404+
TLongObjectMap<TurnCostTableEntry> entries = new TLongObjectHashMap<OSMTurnRelation.TurnCostTableEntry>();
402405

403406
int encoderCount = edgeEncoders.size();
404407
for (int i = 0; i < encoderCount; i++)
@@ -416,6 +419,6 @@ public Collection<TurnCostTableEntry> analyzeTurnRelation( OSMTurnRelation turnR
416419

417420
}
418421

419-
return entries.values();
422+
return entries.valueCollection();
420423
}
421424
}

core/src/main/java/com/graphhopper/util/Helper.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -389,25 +389,4 @@ public static String firstBig( String sayText )
389389

390390
return Character.toUpperCase(sayText.charAt(0)) + sayText.substring(1);
391391
}
392-
393-
/**
394-
* @return a long composed of two integers
395-
*/
396-
public static long intToLong(int left, int right) {
397-
return (long) left << 32 | (long) right & 0xFFFFFFFFL;
398-
}
399-
400-
/**
401-
* @return the first half (first 32 bit) of a long
402-
*/
403-
public static int longToIntLeft(long number) {
404-
return (int) (number >>> 32);
405-
}
406-
407-
/**
408-
* @return the second half (last 32 bit) of a long
409-
*/
410-
public static int longToIntRight(long number) {
411-
return (int) (number & 0xFFFFFFFFL);
412-
}
413392
}

core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ public Collection<TurnCostTableEntry> analyzeTurnRelation( OSMTurnRelation turnR
253253
manager.registerEncoder(car);
254254

255255
//turn cost entries for car and foot are for the same relations (same viaNode, edgeFrom and edgeTo), turn cost entry for bike is for another relation (different viaNode)
256-
turnCostEntry_car.nodeVia = 1;
257-
turnCostEntry_foot.nodeVia = 1;
258-
turnCostEntry_bike.nodeVia = 2;
256+
turnCostEntry_car.edgeFrom = 1;
257+
turnCostEntry_foot.edgeFrom = 1;
258+
turnCostEntry_bike.edgeFrom = 2;
259259

260260
//calculating arbitrary flags using the encoders
261261
turnCostEntry_car.flags = car.getTurnFlags(true, 20);
@@ -273,7 +273,7 @@ public Collection<TurnCostTableEntry> analyzeTurnRelation( OSMTurnRelation turnR
273273

274274
for (TurnCostTableEntry entry : entries)
275275
{
276-
if (entry.nodeVia == 1)
276+
if (entry.edgeFrom == 1)
277277
{ //the first entry provides turn flags for car and foot only
278278
assertEquals(assertFlag1, entry.flags);
279279
assertTrue(car.isTurnRestricted(entry.flags));
@@ -283,7 +283,7 @@ public Collection<TurnCostTableEntry> analyzeTurnRelation( OSMTurnRelation turnR
283283
assertEquals(20, car.getTurnCosts(entry.flags));
284284
assertEquals(0, foot.getTurnCosts(entry.flags));
285285
assertEquals(0, bike.getTurnCosts(entry.flags));
286-
} else if (entry.nodeVia == 2)
286+
} else if (entry.edgeFrom == 2)
287287
{ //the 2nd entry provides turn flags for bike only
288288
assertEquals(assertFlag2, entry.flags);
289289
assertFalse(car.isTurnRestricted(entry.flags));

0 commit comments

Comments
 (0)