aboutsummaryrefslogtreecommitdiffstats
path: root/enum.c
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-07 15:07:25 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-07 15:07:25 +0000
commita92cf5282877a2c6e1e2a72eebdad5dbcd63a675 (patch)
tree6cda1a9449c40c5d2b9cd84151bcf03d183984d3 /enum.c
parent570a6c42717cde056afee90614be8a57f84488fd (diff)
downloadruby-a92cf5282877a2c6e1e2a72eebdad5dbcd63a675.tar.gz
* 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
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c108
1 files changed, 58 insertions, 50 deletions
diff --git a/enum.c b/enum.c
index 4aaef097e5..9f94d2a2f5 100644
--- a/enum.c
+++ b/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) {