kazuyukitanimura commented on a change in pull request #34611:
URL: https://github.com/apache/spark/pull/34611#discussion_r750833865
##########
File path:
sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java
##########
@@ -179,6 +180,18 @@ public WritableColumnVector reserveDictionaryIds(int
capacity) {
*/
protected abstract void reserveInternal(int capacity);
+ /**
+ * Each byte of the returned value (long) has one bit from `bits`. I.e. it
is equivalent to
+ * byte[] a = {(byte)(bits >> 0 & 1), (byte)(bits >> 1 & 1),
+ * (byte)(bits >> 2 & 1), (byte)(bits >> 3 & 1),
+ * (byte)(bits >> 4 & 1), (byte)(bits >> 5 & 1),
+ * (byte)(bits >> 6 & 1), (byte)(bits >> 7 & 1)};
+ * return ByteBuffer.wrap(a).getLong();
+ */
+ protected final long toBitPerByte(int bits) {
+ return ((bits * 0x8040201008040201L) >>> 7) & 0x101010101010101L;
Review comment:
The purpose is for converting `0x000000FF` to `0x0101010101010101L` as
minimum ops as possible.
The simple shit mask would be something like
```((bits << 56) | (bits << 47) | (bits << 38) | (bits << 29) | (bits << 20)
| (bits << 11) | (bits <<2) | (bits >> 7)) & 0x101010101010101L```
that requires 16 ops (8 Shifts, 7 ORs, and 1 And.)
The equivalent
```((bits * 0x8040201008040201L) >>> 7) & 0x101010101010101L```
requires only 3 ops (1 Multiplication, 1 Shift, and 1 And.)
Multiplication with modern CPUs is as fast as 1 clock cycle. So this code
will save 13 ops.
I benchmarked several times. The variance between benchmark results was too
large to see the gain.
Given `bits * 0x8040201008040201L` is easy enough to understand, I kept it.
In theory, it should be faster.
Updated the comment, hopefully it is clearer now.
--
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]