[前][次][番号順一覧][スレッド一覧]

ruby-changes:41384

From: shugo <ko1@a...>
Date: Fri, 8 Jan 2016 00:07:11 +0900 (JST)
Subject: [ruby-changes:41384] shugo:r53456 (trunk): * enum.c (enum_min, enum_max): do the same optimization as r53454.

shugo	2016-01-08 00:07:25 +0900 (Fri, 08 Jan 2016)

  New Revision: 53456

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53456

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

  Modified files:
    trunk/ChangeLog
    trunk/enum.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53455)
+++ ChangeLog	(revision 53456)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jan  8 00:03:22 2016  Shugo Maeda  <shugo@r...>
+
+	* enum.c (enum_min, enum_max): do the same optimization as r53454.
+
 Thu Jan  7 22:32:21 2016  Kenta Murata  <mrkn@m...>
 
 	* ruby.h: undef HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P
Index: enum.c
===================================================================
--- enum.c	(revision 53455)
+++ enum.c	(revision 53456)
@@ -1412,21 +1412,32 @@ enum_none(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enum.c#L1412
     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 https://github.com/ruby/ruby/blob/trunk/enum.c#L1447
 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, arg https://github.com/ruby/ruby/blob/trunk/enum.c#L1490
 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 ob https://github.com/ruby/ruby/blob/trunk/enum.c#L1500
     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 https://github.com/ruby/ruby/blob/trunk/enum.c#L1541
 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, arg https://github.com/ruby/ruby/blob/trunk/enum.c#L1584
 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 ob https://github.com/ruby/ruby/blob/trunk/enum.c#L1594
     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 { https://github.com/ruby/ruby/blob/trunk/enum.c#L1616
     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 https://github.com/ruby/ruby/blob/trunk/enum.c#L1626
 	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, _ https://github.com/ruby/ruby/blob/trunk/enum.c#L1653
     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) {

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]