Skip to content

Commit

Permalink
reduce bytes used when serializing Extent (#52549)
Browse files Browse the repository at this point in the history
This commit reflects comments made by Adrien in #50834
surrounding the Extent serialization.

it re-orders and negates a few values in order to save more space
  • Loading branch information
talevy committed Feb 24, 2020
1 parent 15e3b70 commit 6eb25c4
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions server/src/main/java/org/elasticsearch/common/geo/Extent.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,31 @@ static void readFromCompressed(ByteArrayDataInput input, Extent extent) {
posRight = Integer.MIN_VALUE;
break;
case POSITIVE_SET:
posRight = input.readVInt();
posLeft = Math.toIntExact(posRight - input.readVLong());
posLeft = input.readVInt();
posRight = Math.toIntExact(input.readVLong() + posLeft);
negLeft = Integer.MAX_VALUE;
negRight = Integer.MIN_VALUE;
break;
case NEGATIVE_SET:
negRight = input.readInt();
negRight = -input.readVInt();
negLeft = Math.toIntExact(negRight - input.readVLong());
posLeft = Integer.MAX_VALUE;
posRight = Integer.MIN_VALUE;
break;
case CROSSES_LAT_AXIS:
posRight = input.readInt();
negLeft = Math.toIntExact(posRight - input.readVLong());
posRight = input.readVInt();
negLeft = -input.readVInt();
posLeft = 0;
negRight = 0;
break;
default:
posRight = input.readVInt();
posLeft = Math.toIntExact(posRight - input.readVLong());
negRight = input.readInt();
case ALL_SET:
posLeft = input.readVInt();
posRight = Math.toIntExact(input.readVLong() + posLeft);
negRight = -input.readVInt();
negLeft = Math.toIntExact(negRight - input.readVLong());
break;
default:
throw new IllegalArgumentException("invalid extent values-set byte read [" + type + "]");
}
extent.reset(top, bottom, negLeft, negRight, posLeft, posRight);
}
Expand Down Expand Up @@ -165,23 +167,25 @@ void writeCompressed(ByteBuffersDataOutput output) throws IOException {
switch (type) {
case NONE_SET : break;
case POSITIVE_SET:
output.writeVInt(this.posRight);
output.writeVInt(this.posLeft);
output.writeVLong((long) this.posRight - this.posLeft);
break;
case NEGATIVE_SET:
output.writeInt(this.negRight);
output.writeVInt(-this.negRight);
output.writeVLong((long) this.negRight - this.negLeft);
break;
case CROSSES_LAT_AXIS:
output.writeInt(this.posRight);
output.writeVLong((long) this.posRight - this.negLeft);
break;
default:
output.writeVInt(this.posRight);
output.writeVInt(-this.negLeft);
break;
case ALL_SET:
output.writeVInt(this.posLeft);
output.writeVLong((long) this.posRight - this.posLeft);
output.writeInt(this.negRight);
output.writeVInt(-this.negRight);
output.writeVLong((long) this.negRight - this.negLeft);
break;
default:
throw new IllegalArgumentException("invalid extent values-set byte read [" + type + "]");
}
}

Expand Down

0 comments on commit 6eb25c4

Please sign in to comment.