- 
                Notifications
    You must be signed in to change notification settings 
- 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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      f233f47
              
                Use trusted_len for comparison kernels
              
              
                Dandandan 1aa7baf
              
                Fmt
              
              
                Dandandan 5f2dfa2
              
                Add documentation and doctest
              
              
                Dandandan c4cb72a
              
                Add documentation
              
              
                Dandandan 0e1c8ed
              
                Clippy
              
              
                Dandandan d51e75e
              
                Fix benches
              
              
                Dandandan aa4a4fb
              
                Add faster primitive versions for comparison kernels
              
              
                Dandandan File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
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:
https://github.com/jorgecarleitao/arrow2/blob/be905f1b1f0293ef427387bc35b2e9956ec3336f/src/bitmap/mutable.rs#L209
Uh oh!
There was an error while loading. Please reload this page.
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
and
arrow2is givingThis PR's idea on arrow2 (with corresponding changes) is giving me
-14%oneq Float32and+35%oneq 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_lenfunction, 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
Vecone - 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:
Uh oh!
There was an error while loading. Please reload this page.
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:
and that's even faster than:
this is with the test data configured as:
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).
Uh oh!
There was an error while loading. Please reload this page.
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
Also see findings in the PR here (with updated benchmark).
(x * x + x) % 2will give 0 on even and 1 on uneven inputs I, so the pattern/branches will be very predictable, especially in the scalar version.jorgecarleitao/arrow2#17