-
Couldn't load subscription status.
- Fork 3.9k
ARROW-12032: [Rust] Optimize comparison kernels #9759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report
@@ Coverage Diff @@
## master #9759 +/- ##
==========================================
- Coverage 82.59% 82.55% -0.04%
==========================================
Files 248 249 +1
Lines 58294 58837 +543
==========================================
+ Hits 48149 48575 +426
- Misses 10145 10262 +117
Continue to review full report at Codecov.
|
|
@jorgecarleitao FYI I also tried related code from your |
| //collect (up to) 8 bits into a byte | ||
| while mask != 0 { | ||
| if let Some(value) = iterator.next() { | ||
| byte_accum |= match value { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the bool iterator could be split into chunks (for example, using https://docs.rs/itertools/0.4.2/itertools/struct.Chunks.html or alternatively using https://doc.rust-lang.org/std/primitive.slice.html#method.chunks) of 8 bool values, then each chunk is mapped into a byte by converting each bool value into a byte (for example using std::mem::transmute::<bool, u8>), then shifting according to the position in the chunk, and applying in the output byte, and finally the resulting byte iterator would be used to build the buffer directly. This is the fastest implementation I can imagine because it eliminates as many conditions / checks as possible (and conditions are the enemy of fast).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think there are some faster ways to speed up the inner loop, yours sounds like a great idea to try out. I was also looking at the arrow2 repository of @jorgecarleitao , but I think I have been looking to an older commit before which turned out to be slower (I expected it to be faster, but sometimes the compiler can be quite surprising in what compiles to efficient code.
I think the latest version is over here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you all ❤️
I admit I have spent an immoral amount of time trying to optimize bitmaps, but I have unfortunately not yet concluded what is the best way to handle them. I think that we may not being able to express to the compiler what we want it to do (some kind of operation over a single byte). @yordan-pavlov suggestion is a great one in that direction, though.
FWIW, on my computer (a VM on azure), arrow master (not this PR) is giving
eq Float32 time: [113.40 us 114.81 us 116.28 us]
eq scalar Float32 time: [96.824 us 98.638 us 101.34 us]
and arrow2 is giving
eq Float32 time: [84.519 us 86.065 us 87.772 us]
eq scalar Float32 time: [57.682 us 58.315 us 59.014 us]
This PR's idea on arrow2 (with corresponding changes) is giving me -14% on eq Float32 and +35% on eq scalar Float32. I pushed these benches to master there.
Note the difference between scalar and non-scalar: it is the exact same code on the trusted_len function, but a 30% difference in performance between them; imo this indicates that we are fighting with the compiler to try to explain what we are trying to express here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, I wrote a minimal repo to evaluate these things and added a reddit post to try to get some help / feedback on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yordan-pavlov's idea yields -50% on array-to-scalar and -10% on array-to-array 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yordan-pavlov
I am not sure where the win would be in that case.
I would expect the first idea to be compiled to roughly the same code (if all compiler optimizations work out)?
For the Vec one - I would expect that would be slower as it introduces an extra loop / allocation and barrier for optimization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Dandandan yes, I agree that it's counter-intuitive, but I find that the compiler often surprises me so it's best to try every option; I will try to extend the benchmark repo when I have a bit more free time later today
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, sometimes the result can be surprising.
I tried this variation, this compiles to the same unrolled 38 instructions:
chunk.iter().map(|&c_i| c_i == rhs).enumerate().for_each(|(i, c_i)| {
*byte |= if c_i {
1 << i
} else {
0
};There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I tried to benchmark doing the comparison separately, but it's not faster; on my machine the fastest version is:
(0..8).for_each(|i| {
if chunk[i] == rhs {
*byte = set(*byte, i)
}
});
and that's even faster than:
chunk.iter().enumerate().for_each(|(i, &c_i)| {
*byte |= unsafe { mem::transmute::<bool, u8>(c_i == rhs) << i };
});
this is with the test data configured as:
let vec = (0..20049).map(|x| (x * x + x) % 2).collect::<Vec<_>>();
I think 2000 items (the old length of the test data) is much too small for realistic benchmarking, and it would make more sense to benchmark with test data with length same as the default batch size in DataFusion (I think this was recently increased).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also see findings in the PR here (with updated benchmark). (x * x + x) % 2 will give 0 on even and 1 on uneven inputs I, so the pattern/branches will be very predictable, especially in the scalar version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks a lot, @Dandandan !
Implementation based on [this comment](apache/arrow#9759 (comment)) from @yordan-pavlov. Performance: eq Float32 time: [70.990 us 71.775 us 72.636 us] change: [-12.665% -10.799% -8.9867%] (p = 0.00 < 0.05) Performance has improved. Found 4 outliers among 100 measurements (4.00%) 3 (3.00%) high mild 1 (1.00%) high severe eq scalar Float32 time: [26.832 us 27.174 us 27.540 us] change: [-52.665% -51.655% -50.525%] (p = 0.00 < 0.05) Performance has improved.
Implementation based on [this comment](apache/arrow#9759 (comment)) from @yordan-pavlov. Performance: eq Float32 time: [70.990 us 71.775 us 72.636 us] change: [-12.665% -10.799% -8.9867%] (p = 0.00 < 0.05) Performance has improved. Found 4 outliers among 100 measurements (4.00%) 3 (3.00%) high mild 1 (1.00%) high severe eq scalar Float32 time: [26.832 us 27.174 us 27.540 us] change: [-52.665% -51.655% -50.525%] (p = 0.00 < 0.05) Performance has improved.
|
@jorgecarleitao thank you for trying out my suggestion; I am very happy to see this resulted in some tangible performance improvements. @Dandandan I love the use of godbolt.org, I should be using it more to gain further insight into what code compiles into efficient instructions. |
|
I added the faster implementations using chunks for primitives to this PR and fixed the benchmarks. |
|
Guys this is failing to compile on my machine. It looks like the CICD failed but was merged? (Sorry ignore) saw: #9796 |
This adds a function
from_trusted_len_iter_boolto speed up the creation of an array for booleans.Benchmarks are a bit noisy, but seems to be ~10-20% faster for comparison kernels. This also has some positive effect on DataFusion queries, as they contain quite some (nested) comparisons in filters. For example, executing tpch query 6 in memory is ~7% faster.