Project

General

Profile

Feature #17109

Updated by sawa (Tsuyoshi Sawada) over 3 years ago

When I do intersection (`a & b`) or union (`a (` a | b`), b`) usually one of the array in one position arrays is more likely longer than the one in the other position. second one. I always try to remember in what order should I should write `a` and` b` for the best performance. The right answers for current implementation are `longer & shorter` and` longer | shorter`. 

 [Here](https://github.com/kryzhovnik/ruby/commit/3b5923746792db5a73bc80a2cb9fe982d41a9fa3) I suggest to make it simpler and eliminate the difference. 

 Sorry I don't know C. Though I suppose my solution might be too blunt and verbose, I hope it demonstrates the idea. 

     


     make benchmark ITEM=array_union COMPARE_RUBY=~/.rbenv/versions/2.8.0-dev/bin/ruby 

     |                            |compare-ruby|built-ruby| 
     |:-------------------------|-----------:|---------:| 
     |small-&                     |        4.315M|      4.228M| 
     |                            |         1.02x|           -| 
     |small-intersection          |        4.157M|      4.106M| 
     |                            |         1.01x|           -| 
     |big-&                       |      107.258k|    132.051k| 
     |                            |             -|       1.23x| 
     |big-intersection            |      103.245k|    128.052k| 
     |                            |             -|       1.24x| 
     |big-reverse-intersection    |       96.544k|    159.201k| 
     |                            |             -|       1.65x| 

 


 My own [test](https://gist.github.com/kryzhovnik/288953a5c89df10e2611668703a1511c) shows 20-30% perf improvement of Array#union.

Back