Skip to content

Commit

Permalink
fix: Remove duplicate byte array allocation for CometDictionary (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya authored Mar 21, 2024
1 parent eb6c704 commit 9e59732
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,9 @@ private void initialize() {
binaries = new ByteArrayWrapper[numValues];
for (int i = 0; i < numValues; i++) {
// Need copying here since we re-use byte array for decimal
byte[] bytes = values.getBinaryDecimal(i);
byte[] copy = new byte[DECIMAL_BYTE_WIDTH];
System.arraycopy(bytes, 0, copy, 0, DECIMAL_BYTE_WIDTH);
binaries[i] = new ByteArrayWrapper(copy);
byte[] bytes = new byte[DECIMAL_BYTE_WIDTH];
bytes = values.copyBinaryDecimal(i, bytes);
binaries[i] = new ByteArrayWrapper(bytes);
}
break;
default:
Expand Down
20 changes: 14 additions & 6 deletions common/src/main/java/org/apache/comet/vector/CometVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,30 @@ public Decimal getDecimal(int i, int precision, int scale) {
}
}

/** Reads a 16-byte byte array which are encoded big-endian for decimal128. */
/**
* Reads a 16-byte byte array which are encoded big-endian for decimal128 into internal byte
* array.
*/
byte[] getBinaryDecimal(int i) {
return copyBinaryDecimal(i, DECIMAL_BYTES);
}

/** Reads a 16-byte byte array which are encoded big-endian for decimal128. */
public byte[] copyBinaryDecimal(int i, byte[] dest) {
long valueBufferAddress = getValueVector().getDataBuffer().memoryAddress();
Platform.copyMemory(
null,
valueBufferAddress + (long) i * DECIMAL_BYTE_WIDTH,
DECIMAL_BYTES,
dest,
Platform.BYTE_ARRAY_OFFSET,
DECIMAL_BYTE_WIDTH);
// Decimal is stored little-endian in Arrow, so we need to reverse the bytes here
for (int j = 0, k = DECIMAL_BYTE_WIDTH - 1; j < DECIMAL_BYTE_WIDTH / 2; j++, k--) {
byte tmp = DECIMAL_BYTES[j];
DECIMAL_BYTES[j] = DECIMAL_BYTES[k];
DECIMAL_BYTES[k] = tmp;
byte tmp = dest[j];
dest[j] = dest[k];
dest[k] = tmp;
}
return DECIMAL_BYTES;
return dest;
}

@Override
Expand Down

0 comments on commit 9e59732

Please sign in to comment.