Skip to content

Commit

Permalink
Improve decoding performance by converting string to char[] only once…
Browse files Browse the repository at this point in the history
… instead of repeatedly

Signed-off-by: Lene Preuss <[email protected]>
  • Loading branch information
Lene Preuss authored and lene committed Oct 13, 2021
1 parent 71c34cb commit e0fc38c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ obj/
.vs

*.user
*.class
25 changes: 16 additions & 9 deletions java/src/com/here/flexpolyline/PolylineEncoderDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static final List<LatLngZ> decode(String encoded) {
public static ThirdDimension getThirdDimension(String encoded) {
AtomicInteger index = new AtomicInteger(0);
AtomicLong header = new AtomicLong(0);
Decoder.decodeHeaderFromString(encoded, index, header);
Decoder.decodeHeaderFromString(encoded.toCharArray(), index, header);
return ThirdDimension.fromNum((header.get() >> 4) & 7);
}

Expand Down Expand Up @@ -186,7 +186,7 @@ private String getEncoded() {
*/
private static class Decoder {

private final String encoded;
private final char[] encoded;
private final AtomicInteger index;
private final Converter latConveter;
private final Converter lngConveter;
Expand All @@ -198,7 +198,7 @@ private static class Decoder {


public Decoder(String encoded) {
this.encoded = encoded;
this.encoded = encoded.toCharArray();
this.index = new AtomicInteger(0);
decodeHeader();
this.latConveter = new Converter(precision);
Expand All @@ -219,18 +219,18 @@ private void decodeHeader() {
thirdDimPrecision = (int) ((header.get() >> 3) & 15);
}

private static void decodeHeaderFromString(String encoded, AtomicInteger index, AtomicLong header) {
private static void decodeHeaderFromString(char[] encoded, AtomicInteger index, AtomicLong header) {
AtomicLong value = new AtomicLong(0);

// Decode the header version
if(!Converter.decodeUnsignedVarint(encoded.toCharArray(), index, value)) {
if(!Converter.decodeUnsignedVarint(encoded, index, value)) {
throw new IllegalArgumentException("Invalid encoding");
}
if (value.get() != FORMAT_VERSION) {
throw new IllegalArgumentException("Invalid format version");
}
// Decode the polyline header
if(!Converter.decodeUnsignedVarint(encoded.toCharArray(), index, value)) {
if(!Converter.decodeUnsignedVarint(encoded, index, value)) {
throw new IllegalArgumentException("Invalid encoding");
}
header.set(value.get());
Expand All @@ -240,7 +240,7 @@ private static void decodeHeaderFromString(String encoded, AtomicInteger index,
private boolean decodeOne(AtomicReference<Double> lat,
AtomicReference<Double> lng,
AtomicReference<Double> z) {
if (index.get() == encoded.length()) {
if (index.get() == encoded.length) {
return false;
}
if (!latConveter.decodeValue(encoded, index, lat)) {
Expand Down Expand Up @@ -347,11 +347,11 @@ private static boolean decodeUnsignedVarint(char[] encoded,
}

//Decode single coordinate (say lat|lng|z) starting at index
boolean decodeValue(String encoded,
boolean decodeValue(char[] encoded,
AtomicInteger index,
AtomicReference<Double> coordinate) {
AtomicLong delta = new AtomicLong();
if (!decodeUnsignedVarint(encoded.toCharArray(), index, delta)) {
if (!decodeUnsignedVarint(encoded, index, delta)) {
return false;
}
if ((delta.get() & 1) != 0) {
Expand All @@ -362,6 +362,13 @@ boolean decodeValue(String encoded,
coordinate.set(((double)lastValue / multiplier));
return true;
}

// Overloaded version for backwards compatibility
boolean decodeValue(String encoded,
AtomicInteger index,
AtomicReference<Double> coordinate) {
return decodeValue(encoded.toCharArray(), index, coordinate);
}
}

/**
Expand Down

0 comments on commit e0fc38c

Please sign in to comment.