Improve performance of Array#sum with float elements (#1555)

The declaration of local variable in loop, it will initialize local variable for each run of the loop with clang generated code.
So, it shouldn't declare the local variable in heavy loop.

Array#sum with float elements will be faster around 30%.

* Before
       user     system      total        real
   3.320000   0.010000   3.330000 (  3.336088)

* After
       user     system      total        real
   2.590000   0.010000   2.600000 (  2.602399)

* Test code
require 'benchmark'

Benchmark.bmbm do |x|
  ary = []
  10000.times { ary << Random.rand }

  x.report do
    50000.times do
      ary.sum
    end
  end

end
This commit is contained in:
Watson 2019-10-09 12:25:08 +09:00 коммит произвёл Kenta Murata
Родитель a14cc07f2f
Коммит 2d001003e4
1 изменённых файлов: 1 добавлений и 1 удалений

Просмотреть файл

@ -6620,12 +6620,12 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary)
* See http://link.springer.com/article/10.1007/s00607-005-0139-x
*/
double f, c;
double x, t;
f = NUM2DBL(v);
c = 0.0;
goto has_float_value;
for (; i < RARRAY_LEN(ary); i++) {
double x, t;
e = RARRAY_AREF(ary, i);
if (block_given)
e = rb_yield(e);