Skip to content

Commit 59ee864

Browse files
committed
performance upgrades: avoid copying
1 parent 91613d1 commit 59ee864

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
/.idea
66
/*.iml
77
*.jfr
8+
/.gradle
9+
/bin
10+
/build

src/main/java/com/arangodb/velocypack/VPackBuilder.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,11 @@ private <T> VPackBuilder addInternal(final String attribute, final long tag, fin
652652
final VPackSlice translate = VPackSlice.attributeTranslator.translate(attribute);
653653
if (translate != null) {
654654
final byte[] trValue = translate.getRawVPack();
655-
ensureCapacity(size + trValue.length);
656-
for (int i = 0; i < trValue.length; i++) {
657-
addUnchecked(trValue[i]);
655+
int trValueLength = translate.getByteSize();
656+
int trValueStart = translate.getStart();
657+
ensureCapacity(size + trValueLength);
658+
for (int i = 0; i < trValueLength; i++) {
659+
addUnchecked(trValue[i+trValueStart]);
658660
}
659661
keyWritten = true;
660662
if (value == null) {
@@ -837,9 +839,10 @@ private void appendBinary(final byte[] value) {
837839

838840
private void appendVPack(final VPackSlice value) {
839841
final byte[] vpack = value.getRawVPack();
840-
ensureCapacity(size + vpack.length);
841-
System.arraycopy(vpack, 0, buffer, size, vpack.length);
842-
size += vpack.length;
842+
int length = value.getByteSize();
843+
ensureCapacity(size + length);
844+
System.arraycopy(vpack, value.getStart(), buffer, size, length);
845+
size += length;
843846
}
844847

845848
private void addArray(final boolean unindexed) {

src/main/java/com/arangodb/velocypack/VPackSlice.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -902,14 +902,18 @@ public Iterator<Entry<String, VPackSlice>> objectIterator() {
902902
}
903903
}
904904

905-
protected byte[] getRawVPack() {
905+
protected byte[] getVPack() {
906906
if(start == 0 && vpack.length == getByteSize()) {
907907
return vpack;
908908
}
909909

910910
return Arrays.copyOfRange(vpack, start, start + getByteSize());
911911
}
912912

913+
protected byte[] getRawVPack() {
914+
return vpack;
915+
}
916+
913917
@Override
914918
public String toString() {
915919
try {
@@ -924,7 +928,12 @@ public int hashCode() {
924928
final int prime = 31;
925929
int result = 1;
926930
result = prime * result + start;
927-
result = prime * result + Arrays.hashCode(getRawVPack());
931+
932+
int arrayHash = 1;
933+
for (int i = start, max = getByteSize(); i < max; i++)
934+
arrayHash = 31 * arrayHash + vpack[i];
935+
936+
result = prime * result + arrayHash;
928937
return result;
929938
}
930939

@@ -940,12 +949,20 @@ public boolean equals(final Object obj) {
940949
return false;
941950
}
942951
final VPackSlice other = (VPackSlice) obj;
943-
if (start != other.start) {
952+
953+
int byteSize = getByteSize();
954+
int otherByteSize = other.getByteSize();
955+
956+
if(byteSize != otherByteSize) {
944957
return false;
945958
}
946-
if (!Arrays.equals(getRawVPack(), other.getRawVPack())) {
947-
return false;
959+
960+
for(int i = 0; i < byteSize; i++) {
961+
if(vpack[i+start] != other.vpack[i+other.start]) {
962+
return false;
963+
}
948964
}
965+
949966
return true;
950967
}
951968

src/test/java/com/arangodb/velocypack/VPackUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ public class VPackUtil {
3030

3131
public static String toHex(final VPackSlice vpack) {
3232
final byte[] bytes = vpack.getRawVPack();
33-
final char[] hexChars = new char[bytes.length * 2];
33+
final int bytesLength = vpack.getByteSize();
34+
final int bytesStart = vpack.getStart();
35+
final char[] hexChars = new char[bytesLength * 2];
3436
for (int j = 0; j < bytes.length; j++) {
35-
final int v = bytes[j] & 0xFF;
37+
final int v = bytes[j+bytesStart] & 0xFF;
3638
hexChars[j * 2] = hexArray[v >>> 4];
3739
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
3840
}

0 commit comments

Comments
 (0)