From ce86f79c7b3b7eb31f054fefd22cce5fc8a589d1 Mon Sep 17 00:00:00 2001 From: mrkn Date: Tue, 17 May 2016 15:08:33 +0000 Subject: Optimize enum_sum for a range from int to int * enum.c (enum_sum): Optimize for a range from int to int. * test/ruby/test_enum.rb (test_range_sum): Move from test_range.rb, and add assertions for some conditions. * test/ruby/test_enum.rb (test_hash_sum): Move from test_hash.rb. * test/ruby/test_hash.rb, test/ruby/test_range.rb: Remove test_sum. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55034 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ enum.c | 25 +++++++++++++++++++++++++ internal.h | 1 + numeric.c | 6 +++--- test/ruby/test_enum.rb | 16 ++++++++++++++++ test/ruby/test_hash.rb | 5 ----- test/ruby/test_range.rb | 5 ----- 7 files changed, 56 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1aa6a398e3..20dab87235 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Tue May 18 00:05:00 2016 Kenta Murata + + * enum.c (enum_sum): Optimize for a range from int to int. + + * test/ruby/test_enum.rb (test_range_sum): Move from test_range.rb, + and add assertions for some conditions. + + * test/ruby/test_enum.rb (test_hash_sum): Move from test_hash.rb. + + * test/ruby/test_hash.rb, test/ruby/test_range.rb: Remove test_sum. + Tue May 17 23:08:00 2016 Kenta Murata * enum.c (enum_sum): [DOC] Write documentation. diff --git a/enum.c b/enum.c index 56d304c7a9..54e1364de7 100644 --- a/enum.c +++ b/enum.c @@ -3699,6 +3699,8 @@ static VALUE enum_sum(int argc, VALUE* argv, VALUE obj) { struct enum_sum_memo memo; + VALUE beg, end; + int excl; if (rb_scan_args(argc, argv, "01", &memo.v) == 0) memo.v = LONG2FIX(0); @@ -3713,6 +3715,29 @@ enum_sum(int argc, VALUE* argv, VALUE obj) memo.c = 0.0; } + if (RTEST(rb_range_values(obj, &beg, &end, &excl))) { + if (!memo.block_given && !memo.float_value && + (FIXNUM_P(beg) || RB_TYPE_P(beg, T_BIGNUM)) && + (FIXNUM_P(end) || RB_TYPE_P(end, T_BIGNUM))) { + if (excl) { + if (FIXNUM_P(end)) + end = LONG2FIX(FIX2LONG(end) - 1); + else + end = rb_big_minus(end, LONG2FIX(1)); + } + if (rb_int_ge(end, beg)) { + VALUE a; + a = rb_int_plus(rb_int_minus(end, beg), LONG2FIX(1)); + a = rb_int_mul(a, rb_int_plus(end, beg)); + a = rb_int_idiv(a, LONG2FIX(2)); + return rb_int_plus(memo.v, a); + } + else { + return memo.v; + } + } + } + rb_block_call(obj, id_each, 0, 0, enum_sum_iter_i, (VALUE)&memo); if (memo.float_value) { diff --git a/internal.h b/internal.h index 78ae77d806..2816737390 100644 --- a/internal.h +++ b/internal.h @@ -1097,6 +1097,7 @@ VALUE rb_int_round(VALUE num, int ndigits); VALUE rb_int2str(VALUE num, int base); VALUE rb_dbl_hash(double d); VALUE rb_fix_plus(VALUE x, VALUE y); +VALUE rb_int_ge(VALUE x, VALUE y); #if USE_FLONUM #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n))) diff --git a/numeric.c b/numeric.c index 0ebaa95ecb..c61f5d0982 100644 --- a/numeric.c +++ b/numeric.c @@ -3908,8 +3908,8 @@ fix_ge(VALUE x, VALUE y) } } -static VALUE -int_ge(VALUE x, VALUE y) +VALUE +rb_int_ge(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_ge(x, y); @@ -4950,7 +4950,7 @@ Init_Numeric(void) rb_define_method(rb_cInteger, "===", int_equal, 1); rb_define_method(rb_cInteger, "==", int_equal, 1); rb_define_method(rb_cInteger, ">", int_gt, 1); - rb_define_method(rb_cInteger, ">=", int_ge, 1); + rb_define_method(rb_cInteger, ">=", rb_int_ge, 1); rb_define_method(rb_cInteger, "<", int_lt, 1); rb_define_method(rb_cInteger, "<=", int_le, 1); diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index ed464a285e..df75a80c50 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -905,4 +905,20 @@ class TestEnumerable < Test::Unit::TestCase assert_equal(6, [1r, 2, 3r].each.sum) EOS end + + def test_hash_sum + histogram = { 1 => 6, 2 => 4, 3 => 3, 4 => 7, 5 => 5, 6 => 4 } + assert_equal(100, histogram.sum {|v, n| v * n }) + end + + def test_range_sum + assert_int_equal(55, (1..10).sum) + assert_float_equal(55.0, (1..10).sum(0.0)) + assert_int_equal(90, (5..10).sum {|v| v * 2 }) + assert_float_equal(90.0, (5..10).sum(0.0) {|v| v * 2 }) + assert_int_equal(0, (2..0).sum) + assert_int_equal(5, (2..0).sum(5)) + assert_int_equal(2, (2..2).sum) + assert_int_equal(42, (2...2).sum(42)) + end end diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 03a712d85b..6a8e7f0423 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1415,11 +1415,6 @@ class TestHash < Test::Unit::TestCase assert_equal([10, 20, 30], [1, 2, 3].map(&h)) end - def test_sum - histogram = { 1 => 6, 2 => 4, 3 => 3, 4 => 7, 5 => 5, 6 => 4 } - assert_equal(100, histogram.sum {|v, n| v * n }) - end - class TestSubHash < TestHash class SubHash < Hash def reject(*) diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb index 431ed0dc30..2bff105936 100644 --- a/test/ruby/test_range.rb +++ b/test/ruby/test_range.rb @@ -631,9 +631,4 @@ class TestRange < Test::Unit::TestCase end (a.."c").each {|x, &b| assert_nil(b)} end - - def test_sum - assert_equal(55, (1..10).sum) - assert_equal(110, (1..10).sum {|v| v * 2 }) - end end -- cgit v1.2.3