Permalink
Browse files

* enum.c (enum_min, enum_max): do the same optimization as r53454.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53456 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
shugo committed Jan 7, 2016
1 parent 98e565e commit 3dcd4b2a98e81694424c6f001b423a68af7730c9
Showing with 62 additions and 50 deletions.
  1. +4 −0 ChangeLog
  2. +58 −50 enum.c
View
@@ -1,3 +1,7 @@
Fri Jan 8 00:03:22 2016 Shugo Maeda <shugo@ruby-lang.org>
* enum.c (enum_min, enum_max): do the same optimization as r53454.
Thu Jan 7 22:32:21 2016 Kenta Murata <mrkn@mrkn.jp>
* ruby.h: undef HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P
View
108 enum.c
@@ -1412,21 +1412,32 @@ enum_none(VALUE obj)
return memo->v1;
}
#define OPTIMIZED_CMP(a, b, data) \
((FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data, Fixnum)) ? \
(((long)a > (long)b) ? 1 : ((long)a < (long)b) ? -1 : 0) : \
(STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data, String)) ? \
rb_str_cmp(a, b) : \
rb_cmpint(rb_funcallv(a, id_cmp, 1, &b), a, b))
struct min_t {
VALUE min;
int opt_methods;
int opt_inited;
};
static VALUE
min_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
{
VALUE cmp;
struct MEMO *memo = MEMO_CAST(args);
struct min_t *memo = MEMO_FOR(struct min_t, args);
ENUM_WANT_SVALUE();
if (memo->v1 == Qundef) {
MEMO_V1_SET(memo, i);
if (memo->min == Qundef) {
memo->min = i;
}
else {
cmp = rb_funcall(i, id_cmp, 1, memo->v1);
if (rb_cmpint(cmp, i, memo->v1) < 0) {
MEMO_V1_SET(memo, i);
if (OPTIMIZED_CMP(i, memo->min, memo) < 0) {
memo->min = i;
}
}
return Qnil;
@@ -1436,17 +1447,17 @@ static VALUE
min_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
{
VALUE cmp;
struct MEMO *memo = MEMO_CAST(args);
struct min_t *memo = MEMO_FOR(struct min_t, args);
ENUM_WANT_SVALUE();
if (memo->v1 == Qundef) {
MEMO_V1_SET(memo, i);
if (memo->min == Qundef) {
memo->min = i;
}
else {
cmp = rb_yield_values(2, i, memo->v1);
if (rb_cmpint(cmp, i, memo->v1) < 0) {
MEMO_V1_SET(memo, i);
cmp = rb_yield_values(2, i, memo->min);
if (rb_cmpint(cmp, i, memo->min) < 0) {
memo->min = i;
}
}
return Qnil;
@@ -1479,7 +1490,8 @@ min_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
static VALUE
enum_min(int argc, VALUE *argv, VALUE obj)
{
struct MEMO *memo = MEMO_NEW(Qundef, 0, 0);
VALUE memo;
struct min_t *m = NEW_MEMO_FOR(struct min_t, memo);
VALUE result;
VALUE num;
@@ -1488,32 +1500,39 @@ enum_min(int argc, VALUE *argv, VALUE obj)
if (!NIL_P(num))
return nmin_run(obj, num, 0, 0);
m->min = Qundef;
m->opt_methods = 0;
m->opt_inited = 0;
if (rb_block_given_p()) {
rb_block_call(obj, id_each, 0, 0, min_ii, (VALUE)memo);
rb_block_call(obj, id_each, 0, 0, min_ii, memo);
}
else {
rb_block_call(obj, id_each, 0, 0, min_i, (VALUE)memo);
rb_block_call(obj, id_each, 0, 0, min_i, memo);
}
result = memo->v1;
result = m->min;
if (result == Qundef) return Qnil;
return result;
}
struct max_t {
VALUE max;
int opt_methods;
int opt_inited;
};
static VALUE
max_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
{
struct MEMO *memo = MEMO_CAST(args);
VALUE cmp;
struct max_t *memo = MEMO_FOR(struct max_t, args);
ENUM_WANT_SVALUE();
if (memo->v1 == Qundef) {
MEMO_V1_SET(memo, i);
if (memo->max == Qundef) {
memo->max = i;
}
else {
cmp = rb_funcall(i, id_cmp, 1, memo->v1);
if (rb_cmpint(cmp, i, memo->v1) > 0) {
MEMO_V1_SET(memo, i);
if (OPTIMIZED_CMP(i, memo->max, memo) > 0) {
memo->max = i;
}
}
return Qnil;
@@ -1522,18 +1541,18 @@ max_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
static VALUE
max_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
{
struct MEMO *memo = MEMO_CAST(args);
struct max_t *memo = MEMO_FOR(struct max_t, args);
VALUE cmp;
ENUM_WANT_SVALUE();
if (memo->v1 == Qundef) {
MEMO_V1_SET(memo, i);
if (memo->max == Qundef) {
memo->max = i;
}
else {
cmp = rb_yield_values(2, i, memo->v1);
if (rb_cmpint(cmp, i, memo->v1) > 0) {
MEMO_V1_SET(memo, i);
cmp = rb_yield_values(2, i, memo->max);
if (rb_cmpint(cmp, i, memo->max) > 0) {
memo->max = i;
}
}
return Qnil;
@@ -1565,7 +1584,8 @@ max_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
static VALUE
enum_max(int argc, VALUE *argv, VALUE obj)
{
struct MEMO *memo = MEMO_NEW(Qundef, 0, 0);
VALUE memo;
struct max_t *m = NEW_MEMO_FOR(struct max_t, memo);
VALUE result;
VALUE num;
@@ -1574,13 +1594,16 @@ enum_max(int argc, VALUE *argv, VALUE obj)
if (!NIL_P(num))
return nmin_run(obj, num, 0, 1);
m->max = Qundef;
m->opt_methods = 0;
m->opt_inited = 0;
if (rb_block_given_p()) {
rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)memo);
}
else {
rb_block_call(obj, id_each, 0, 0, max_i, (VALUE)memo);
}
result = memo->v1;
result = m->max;
if (result == Qundef) return Qnil;
return result;
}
@@ -1593,21 +1616,6 @@ struct minmax_t {
int opt_inited;
};
static int
optimized_cmp(VALUE a, VALUE b, struct minmax_t *data)
{
if (FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data, Fixnum)) {
if ((long)a > (long)b) return 1;
if ((long)a < (long)b) return -1;
return 0;
}
if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data, String)) {
return rb_str_cmp(a, b);
}
return rb_cmpint(rb_funcallv(a, id_cmp, 1, &b), a, b);
}
static void
minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
{
@@ -1618,11 +1626,11 @@ minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
memo->max = j;
}
else {
n = optimized_cmp(i, memo->min, memo);
n = OPTIMIZED_CMP(i, memo->min, memo);
if (n < 0) {
memo->min = i;
}
n = optimized_cmp(j, memo->max, memo);
n = OPTIMIZED_CMP(j, memo->max, memo);
if (n > 0) {
memo->max = j;
}
@@ -1645,7 +1653,7 @@ minmax_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
j = memo->last;
memo->last = Qundef;
n = optimized_cmp(j, i, memo);
n = OPTIMIZED_CMP(j, i, memo);
if (n == 0)
i = j;
else if (n < 0) {

0 comments on commit 3dcd4b2

Please sign in to comment.