ARROW-7216: [Java] Improve the performance of setting/clearing individual bits#5872
ARROW-7216: [Java] Improve the performance of setting/clearing individual bits#5872liyafan82 wants to merge 3 commits intoapache:masterfrom
Conversation
20182ef to
58b9735
Compare
| * @param index index to be set | ||
| */ | ||
| public static void setValidityBitToZero(ArrowBuf validityBuffer, int index) { | ||
| final int byteIndex = byteIndex(index); |
There was a problem hiding this comment.
can this delegate to setValidityBit instead? I think it is likely JIT will inline and do dead-code elimination?
There was a problem hiding this comment.
I am sorry I don't understand this comment. You mean this method should be called from setValidityBit?
There was a problem hiding this comment.
sorry I thought I replied here, the performance benchmarks show the JIT will not eliminate the branches. we should comment on why code duplication is necessary instead of calling setValidityBit below.
There was a problem hiding this comment.
I see. Thanks for the clarification.
I have added comments explicitly to discuss this.
There was a problem hiding this comment.
I know this mimics the existing setValidityBitToOne, but I think unsetBit would be a better name (we can consider adding an alias for setValidityBitToOne as "setBit" as well?
There was a problem hiding this comment.
Sounds good to me. Will provide the alias later.
There was a problem hiding this comment.
Alias provided. Please take a look. Thank you.
| final int bitIndex = bitIndex(index); | ||
| byte currentByte = validityBuffer.getByte(byteIndex); | ||
| final byte bitMask = (byte) (1L << bitIndex); | ||
| int currentByte = validityBuffer.getByte(byteIndex); |
There was a problem hiding this comment.
please provide a comment on why you are assigning a byte to an int.
There was a problem hiding this comment.
Good point. Comment added.
| final int bitIndex = bitIndex(index); | ||
| byte currentByte = validityBuffer.getByte(byteIndex); | ||
| final byte bitMask = (byte) (1L << bitIndex); | ||
| int currentByte = validityBuffer.getByte(byteIndex); |
There was a problem hiding this comment.
please comment on the assignment.
There was a problem hiding this comment.
Comment added. Thank you.
| @@ -74,12 +89,12 @@ public static void setValidityBitToOne(ArrowBuf validityBuffer, int index) { | |||
| public static void setValidityBit(ArrowBuf validityBuffer, int index, int value) { | |||
There was a problem hiding this comment.
is this still used? can it be marked as deprecated?
There was a problem hiding this comment.
I am afraid not. This will be used in scenarios where the bit to set is unknown a priori.
For example, in BaseVariableWidthVector#public void set(int index, int isSet, int start, int end, ArrowBuf buffer).
emkornfield
left a comment
There was a problem hiding this comment.
Thanks a few comments/suggestions.
|
+1, thank you. |
…dual bits Setting/clearing individual bits are key operations for Arrow. In this issue, we improve the performance these operations by: 1. replacing arithmetic operations with bit-wise operations 2. remove unnecessary casts between int/byte 3. provide new API to remove the if branch Benchmark results show that for clearing a bit, the performance improve by 11%, and for general set/clear operation, the performance improve by 4.7%: before: BitVectorHelperBenchmarks.setValidityBitBenchmark avgt 5 4.524 ± 0.015 us/op after: BitVectorHelperBenchmarks.setValidityBitBenchmark avgt 5 4.313 ± 0.011 us/op BitVectorHelperBenchmarks.setValidityBitToZeroBenchmark avgt 5 4.020 ± 0.016 us/op Closes apache#5872 from liyafan82/fly_1120_clear and squashes the following commits: cb745b5 <liyafan82> Discuss the reason for duplicate logic aec3b04 <liyafan82> Use better method names 58b9735 <liyafan82> Improve the performance of setting/clearing individual bits Authored-by: liyafan82 <fan_li_ya@foxmail.com> Signed-off-by: Micah Kornfield <emkornfield@gmail.com>
Setting/clearing individual bits are key operations for Arrow. In this issue, we improve the performance these operations by:
Benchmark results show that for clearing a bit, the performance improve by 11%, and for general set/clear operation, the performance improve by 4.7%:
before:
BitVectorHelperBenchmarks.setValidityBitBenchmark avgt 5 4.524 ± 0.015 us/op
after:
BitVectorHelperBenchmarks.setValidityBitBenchmark avgt 5 4.313 ± 0.011 us/op
BitVectorHelperBenchmarks.setValidityBitToZeroBenchmark avgt 5 4.020 ± 0.016 us/op