comphead commented on code in PR #7830:
URL: https://github.com/apache/datafusion-comet/pull/7830#discussion_r4778319327
##########
spark/src/main/java/org/apache/comet/vector/CometPlainVector.java:
##########
@@ -57,24 +64,29 @@ public CometPlainVector(ValueVector vector) {
public CometPlainVector(ValueVector vector, boolean isUuid) {
super(vector, vector.getField(), isUuid);
- // NullType doesn't have data buffer.
if (vector instanceof NullVector) {
- this.valueBufferAddress = -1;
- } else {
+ // NullType doesn't have a data buffer.
+ this.vectorKind = VectorKind.NULL;
+ this.valueBufferAddress = -1L;
+ this.offsetBufferAddress = -1L;
+ } else if (vector instanceof BaseFixedWidthVector) {
+ this.vectorKind = VectorKind.FIXED_WIDTH;
+ this.valueBufferAddress = vector.getDataBuffer().memoryAddress();
+ this.offsetBufferAddress = -1L;
+ } else if (vector instanceof BaseVariableWidthVector) {
+ this.vectorKind = VectorKind.VARIABLE_WIDTH;
this.valueBufferAddress = vector.getDataBuffer().memoryAddress();
- }
- isBaseFixedWidthVector = valueVector instanceof BaseFixedWidthVector;
- if (vector instanceof BaseVariableWidthVector) {
this.offsetBufferAddress =
((BaseVariableWidthVector) vector).getOffsetBuffer().memoryAddress();
- this.isLargeVarWidth = false;
} else if (vector instanceof BaseLargeVariableWidthVector) {
+ this.vectorKind = VectorKind.LARGE_VARIABLE_WIDTH;
+ this.valueBufferAddress = vector.getDataBuffer().memoryAddress();
this.offsetBufferAddress =
((BaseLargeVariableWidthVector)
vector).getOffsetBuffer().memoryAddress();
- this.isLargeVarWidth = true;
} else {
- this.offsetBufferAddress = -1;
- this.isLargeVarWidth = false;
+ this.vectorKind = VectorKind.OTHER;
+ this.valueBufferAddress = vector.getDataBuffer().memoryAddress();
+ this.offsetBufferAddress = -1L;
}
Review Comment:
Three of the four branches repeat `this.valueBufferAddress =
vector.getDataBuffer().memoryAddress()`, and `-1L` is still written to
`valueBufferAddress` and `offsetBufferAddress` for `NULL` and `OTHER`. So the
sentinels are not gone, and the PR text that says this removes sentinel
coupling is only half true. Computing `vectorKind` first and then assigning
each address once would be shorter and would make it clear which kinds own
which buffer.
##########
spark/src/main/java/org/apache/comet/vector/CometPlainVector.java:
##########
@@ -136,32 +148,37 @@ public double getDouble(int rowId) {
@Override
public UTF8String getUTF8String(int rowId) {
if (isNullAt(rowId)) return null;
- if (offsetBufferAddress != -1) {
- long offset;
- int length;
- if (isLargeVarWidth) {
- offset = Platform.getLong(null, offsetBufferAddress + rowId * 8L);
- length = (int) (Platform.getLong(null, offsetBufferAddress + (rowId +
1L) * 8L) - offset);
- } else {
- offset = Platform.getInt(null, offsetBufferAddress + rowId * 4L);
- length = Platform.getInt(null, offsetBufferAddress + (rowId + 1L) *
4L) - (int) offset;
- }
- return UTF8String.fromAddress(null, valueBufferAddress + offset, length);
- } else if (isBaseFixedWidthVector) {
- BaseFixedWidthVector fixedWidthVector = (BaseFixedWidthVector)
valueVector;
- int length = fixedWidthVector.getTypeWidth();
- int offset = rowId * length;
- byte[] result = new byte[length];
- Platform.copyMemory(
- null, valueBufferAddress + offset, result,
Platform.BYTE_ARRAY_OFFSET, length);
-
- if (!isUuid) {
- return UTF8String.fromBytes(result);
- } else {
- return UTF8String.fromString(convertToUuid(result).toString());
- }
- } else {
- throw new IllegalStateException("Unsupported UTF8 vector type: " +
valueVector.getName());
+ switch (vectorKind) {
Review Comment:
`getUTF8String` and `getBinary` run once per row. Before, the common case
was one `long` compare on `offsetBufferAddress` plus a boolean. Now `switch
(vectorKind)` goes through javac's synthetic `$SwitchMap` array indexed by
`ordinal()`, which is an extra array load with a bounds check per call, and the
JIT does not fold a static array element into a constant. I expect this to be
small but I did not decompile or benchmark it. Please add a before and after
number for reading a string column and a binary column row by row (an existing
benchmark under `spark/src/test/scala/org/apache/spark/sql/benchmark` would
do), or keep the hot `VARIABLE_WIDTH` check as a plain comparison and use the
enum only for the rare kinds.
##########
spark/src/main/java/org/apache/comet/vector/CometPlainVector.java:
##########
@@ -170,20 +187,23 @@ public byte[] getBinary(int rowId) {
if (isNullAt(rowId)) return null;
long offset;
int length;
- if (offsetBufferAddress != -1) {
- if (isLargeVarWidth) {
+ switch (vectorKind) {
+ case VARIABLE_WIDTH:
+ int intOffset = Platform.getInt(null, offsetBufferAddress + rowId *
4L);
+ offset = intOffset;
+ length = Platform.getInt(null, offsetBufferAddress + (rowId + 1L) *
4L) - intOffset;
+ break;
+ case LARGE_VARIABLE_WIDTH:
offset = Platform.getLong(null, offsetBufferAddress + rowId * 8L);
length = (int) (Platform.getLong(null, offsetBufferAddress + (rowId +
1L) * 8L) - offset);
- } else {
- offset = Platform.getInt(null, offsetBufferAddress + rowId * 4L);
- length = Platform.getInt(null, offsetBufferAddress + (rowId + 1L) *
4L) - (int) offset;
- }
- } else if (valueVector instanceof BaseFixedWidthVector) {
- BaseFixedWidthVector fixedWidthVector = (BaseFixedWidthVector)
valueVector;
- length = fixedWidthVector.getTypeWidth();
- offset = rowId * length;
- } else {
- throw new IllegalStateException("Unsupported binary vector type: " +
valueVector.getName());
+ break;
+ case FIXED_WIDTH:
+ BaseFixedWidthVector fixedWidthVector = (BaseFixedWidthVector)
valueVector;
+ length = fixedWidthVector.getTypeWidth();
+ offset = (long) rowId * length;
Review Comment:
The PR description says this removes the hot-path `instanceof
BaseFixedWidthVector` from `getBinary`. The `FIXED_WIDTH` case still casts
`valueVector` and calls `getTypeWidth()`, and before the change that
`instanceof` was only reached after the offset check failed, so variable-width
reads never paid for it. Please drop that claim from the description. Also,
`(long) rowId * length` here and at line 169 changes the old `int`
multiplication to `long`. That is an improvement, but it is a behavior change
that the title and description do not mention. Either say so or keep the
refactor behavior-identical. Finally, `case VARIABLE_WIDTH` declares `int
intOffset` without braces while `getUTF8String` braces each case, so please use
one style.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]