Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nonnull;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;
import java.util.Arrays;

import org.apache.spark.unsafe.PlatformDependent;
Expand Down Expand Up @@ -161,18 +162,25 @@ public long getPrefix() {
// If size is greater than 4, assume we have at least 8 bytes of data to fetch.
// After getting the data, we use a mask to mask out data that is not part of the string.
long p;
long mask = 0;
if (numBytes >= 8) {
p = PlatformDependent.UNSAFE.getLong(base, offset);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we need something for this branch also?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nvm u don't need it since the reversebytes was done later.

I think it's not that big of a deal to duplicate code here, and the nested ifs really make it harder to read.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mask represents the characters that need to be cleared. p needs no modification (and thus mask of zero is correct) when numBytes is equal to zero or greater than 8.

} else if (numBytes > 4) {
mask = (1L << (8 - numBytes) * 8) - 1;
p = PlatformDependent.UNSAFE.getLong(base, offset);
p = p & ((1L << numBytes * 8) - 1);
} else if (numBytes > 0) {
mask = (1L << (8 - numBytes) * 8) - 1;
p = (long) PlatformDependent.UNSAFE.getInt(base, offset);
p = p & ((1L << numBytes * 8) - 1);
if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move the result of ByteOrder.nativeOrder into a static variable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

p <<= 32;
}
} else {
p = 0;
}
p = java.lang.Long.reverseBytes(p);
if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
p = java.lang.Long.reverseBytes(p);
}
p &= ~mask;
return p;
}

Expand Down