[前][次][番号順一覧][スレッド一覧]

ruby-changes:46752

From: watson1978 <ko1@a...>
Date: Wed, 24 May 2017 15:57:13 +0900 (JST)
Subject: [ruby-changes:46752] watson1978:r58867 (trunk): Improve performance in where push the element into non shared Array object

watson1978	2017-05-24 15:57:08 +0900 (Wed, 24 May 2017)

  New Revision: 58867

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

  Log:
    Improve performance in where push the element into non shared Array object
    
    * array.c (ary_ensure_room_for_push): use rb_ary_modify_check() instead of
        rb_ary_modify() to check whether the object can be modified for non shared
        Array object. rb_ary_modify() has the codes for shared Array object too.
        In here, it has condition branch for shared / non shared Array object and
        it can use rb_ary_modify_check() which is smaller function than
        rb_ary_modify() for non shared object.
    
        rb_ary_modify_check() will be expand as inline function.
        If it will compile with GCC, Array#<< will be faster around 8%.
    
        [ruby-core:81082] [Bug #13553] [Fix GH-1609]
    
    ## Clang 802.0.42
    ### Before
                Array#<<      9.353M (?\194?\177 1.7%) i/s -     46.787M in   5.004123s
              Array#push      7.702M (?\194?\177 1.1%) i/s -     38.577M in   5.009338s
         Array#values_at      6.133M (?\194?\177 1.9%) i/s -     30.699M in   5.007772s
    
    ### After
                Array#<<      9.458M (?\194?\177 2.0%) i/s -     47.357M in   5.009069s
              Array#push      7.921M (?\194?\177 1.8%) i/s -     39.665M in   5.009151s
         Array#values_at      6.377M (?\194?\177 2.3%) i/s -     31.881M in   5.001888s
    
    ### Result
    Array#<<        -> 1.2% faster
    Array#push      -> 2.8% faster
    Array#values_at -> 3.9% faster
    
    ## GCC 7.1.0
    ### Before
                Array#<<     10.497M (?\194?\177 1.1%) i/s -     52.665M in   5.017601s
              Array#push      8.527M (?\194?\177 1.6%) i/s -     42.777M in   5.018003s
         Array#values_at      7.621M (?\194?\177 1.7%) i/s -     38.152M in   5.007910s
    
    ### After
                Array#<<     11.403M (?\194?\177 1.3%) i/s -     57.028M in   5.001849s
              Array#push      8.924M (?\194?\177 1.3%) i/s -     44.609M in   4.999940s
         Array#values_at      8.291M (?\194?\177 1.4%) i/s -     41.487M in   5.004727s
    
    ### Result
    Array#<<        -> 8.3% faster
    Array#push      -> 4.3% faster
    Array#values_at -> 8.7% faster
    
    ## Test code
    require 'benchmark/ips'
    
    Benchmark.ips do |x|
    
      x.report "Array#<<" do |i|
        i.times { [1,2] << 3 }
      end
    
      x.report "Array#push" do |i|
        i.times { [1,2].push(3) }
      end
    
      x.report "Array#values_at" do |i|
        ary = [1, 2, 3, 4, 5]
        i.times { ary.values_at(0, 2, 4) }
      end
    
    end

  Modified files:
    trunk/array.c
Index: array.c
===================================================================
--- array.c	(revision 58866)
+++ array.c	(revision 58867)
@@ -380,8 +380,11 @@ ary_ensure_room_for_push(VALUE ary, long https://github.com/ruby/ruby/blob/trunk/array.c#L380
 		}
 	    }
 	}
+	rb_ary_modify(ary);
+    }
+    else {
+	rb_ary_modify_check(ary);
     }
-    rb_ary_modify(ary);
     capa = ARY_CAPA(ary);
     if (new_len > capa) {
 	ary_double_capa(ary, new_len);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]