ruby-changes:42960
From: mrkn <ko1@a...>
Date: Wed, 18 May 2016 00:08:37 +0900 (JST)
Subject: [ruby-changes:42960] mrkn:r55034 (trunk): Optimize enum_sum for a range from int to int
mrkn 2016-05-18 00:08:33 +0900 (Wed, 18 May 2016) New Revision: 55034 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55034 Log: 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. Modified files: trunk/ChangeLog trunk/enum.c trunk/internal.h trunk/numeric.c trunk/test/ruby/test_enum.rb trunk/test/ruby/test_hash.rb trunk/test/ruby/test_range.rb Index: test/ruby/test_range.rb =================================================================== --- test/ruby/test_range.rb (revision 55033) +++ test/ruby/test_range.rb (revision 55034) @@ -631,9 +631,4 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L631 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 Index: test/ruby/test_enum.rb =================================================================== --- test/ruby/test_enum.rb (revision 55033) +++ test/ruby/test_enum.rb (revision 55034) @@ -905,4 +905,20 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L905 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 Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 55033) +++ test/ruby/test_hash.rb (revision 55034) @@ -1415,11 +1415,6 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1415 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(*) Index: numeric.c =================================================================== --- numeric.c (revision 55033) +++ numeric.c (revision 55034) @@ -3908,8 +3908,8 @@ fix_ge(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3908 } } -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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4950 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); Index: internal.h =================================================================== --- internal.h (revision 55033) +++ internal.h (revision 55034) @@ -1097,6 +1097,7 @@ VALUE rb_int_round(VALUE num, int ndigit https://github.com/ruby/ruby/blob/trunk/internal.h#L1097 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))) Index: ChangeLog =================================================================== --- ChangeLog (revision 55033) +++ ChangeLog (revision 55034) @@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue May 18 00:05:00 2016 Kenta Murata <mrkn@m...> + + * 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 <mrkn@m...> * enum.c (enum_sum): [DOC] Write documentation. Index: enum.c =================================================================== --- enum.c (revision 55033) +++ enum.c (revision 55034) @@ -3699,6 +3699,8 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L3699 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 ob https://github.com/ruby/ruby/blob/trunk/enum.c#L3715 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) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/