1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
mrkn 2016-05-17 15:08:33 +00:00
parent 41d002bbad
commit d5595a9627
7 changed files with 56 additions and 13 deletions

View file

@ -1,3 +1,14 @@
Tue May 18 00:05:00 2016 Kenta Murata <mrkn@mrkn.jp>
* 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@mrkn.jp>
* enum.c (enum_sum): [DOC] Write documentation.

25
enum.c
View file

@ -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) {

View file

@ -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)))

View file

@ -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);

View file

@ -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

View file

@ -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(*)

View file

@ -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