[ruby-cvs:67238] nobu:r60057 (trunk): array.c: improve operations on small arrays

nobu at ruby-lang.org nobu at ruby-lang.org
Fri Sep 29 16:43:22 JST 2017


nobu	2017-09-29 16:43:22 +0900 (Fri, 29 Sep 2017)

  New Revision: 60057

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60057

  Log:
    array.c: improve operations on small arrays
    
    [Feature #13884]
    
    Reduce number of memory allocations for "and", "or" and "diff"
    operations on small arrays
    
    Very often, arrays are used to filter parameters and to select
    interesting items from 2 collections and very often these
    collections are small enough, for example:
    
    ```ruby
    SAFE_COLUMNS = [:id, :title, :created_at]
    
    def columns
      @all_columns & SAFE_COLUMNS
    end
    ```
    
    In this patch, I got rid of unnecessary memory allocations for
    small arrays when "and", "or" and "diff" operations are performed.
    
    name             | HEAD  | PATCH
    -----------------+------:+------:
    array_small_and  | 0.615 | 0.263
    array_small_diff | 0.676 | 0.282
    array_small_or   | 0.953 | 0.463
    
    name             | PATCH
    -----------------+------:
    array_small_and  | 2.343
    array_small_diff | 2.392
    array_small_or   | 2.056
    
    name             | HEAD  | PATCH
    -----------------+------:+------:
    array_small_and  | 1.429 | 1.005
    array_small_diff | 1.493 | 0.878
    array_small_or   | 1.672 | 1.152
    
    name             | PATCH
    -----------------+------:
    array_small_and  | 1.422
    array_small_diff | 1.700
    array_small_or   | 1.452
    
    Author:    Dmitry Bochkarev <dimabochkarev at gmail.com>

  Added files:
    trunk/benchmark/bm_array_small_and.rb
    trunk/benchmark/bm_array_small_diff.rb
    trunk/benchmark/bm_array_small_or.rb
  Modified files:
    trunk/array.c
    trunk/spec/ruby/core/array/intersection_spec.rb
    trunk/spec/ruby/core/array/minus_spec.rb
    trunk/spec/ruby/core/array/union_spec.rb
    trunk/test/ruby/test_array.rb


More information about the ruby-cvs mailing list