From fc903a066b5213fd7f99bcd9194b3727c803218f Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@ruby-lang.org>
Date: Mon, 14 Mar 2016 21:21:46 +0900
Subject: [PATCH 1/3] refactor a data structure for CMP_OPTIMIZABLE

* internal.c: add struct cmp_opt_data.

* array.c (struct ary_sort_data): use struct cmp_opt_data.

* enum.c (struct min_t, max_t, min_max_t): use struct cmp_opt_data.
---
 array.c    | 11 +++++------
 enum.c     | 31 ++++++++++++++-----------------
 internal.h | 13 +++++++++----
 3 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/array.c b/array.c
index 86cf09e..120c2f8 100644
--- a/array.c
+++ b/array.c
@@ -2364,8 +2364,7 @@ rb_ary_rotate_m(int argc, VALUE *argv, VALUE ary)
 
 struct ary_sort_data {
     VALUE ary;
-    int opt_methods;
-    int opt_inited;
+    struct cmp_opt_data cmp_opt;
 };
 
 static VALUE
@@ -2399,12 +2398,12 @@ sort_2(const void *ap, const void *bp, void *dummy)
     VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
     int n;
 
-    if (FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data, Fixnum)) {
+    if (FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data->cmp_opt, 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)) {
+    if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data->cmp_opt, String)) {
 	return rb_str_cmp(a, b);
     }
 
@@ -2448,8 +2447,8 @@ rb_ary_sort_bang(VALUE ary)
 
 	RBASIC_CLEAR_CLASS(tmp);
 	data.ary = tmp;
-	data.opt_methods = 0;
-	data.opt_inited = 0;
+	data.cmp_opt.opt_methods = 0;
+	data.cmp_opt.opt_inited = 0;
 	RARRAY_PTR_USE(tmp, ptr, {
 	    ruby_qsort(ptr, len, sizeof(VALUE),
 		       rb_block_given_p()?sort_1:sort_2, &data);
diff --git a/enum.c b/enum.c
index 681d58f..1541f61 100644
--- a/enum.c
+++ b/enum.c
@@ -1421,8 +1421,7 @@ enum_none(VALUE obj)
 
 struct min_t {
     VALUE min;
-    int opt_methods;
-    int opt_inited;
+    struct cmp_opt_data cmp_opt;
 };
 
 static VALUE
@@ -1436,7 +1435,7 @@ min_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
 	memo->min = i;
     }
     else {
-	if (OPTIMIZED_CMP(i, memo->min, memo) < 0) {
+	if (OPTIMIZED_CMP(i, memo->min, memo->cmp_opt) < 0) {
 	    memo->min = i;
 	}
     }
@@ -1501,8 +1500,8 @@ enum_min(int argc, VALUE *argv, VALUE obj)
        return nmin_run(obj, num, 0, 0);
 
     m->min = Qundef;
-    m->opt_methods = 0;
-    m->opt_inited = 0;
+    m->cmp_opt.opt_methods = 0;
+    m->cmp_opt.opt_inited = 0;
     if (rb_block_given_p()) {
 	rb_block_call(obj, id_each, 0, 0, min_ii, memo);
     }
@@ -1516,8 +1515,7 @@ enum_min(int argc, VALUE *argv, VALUE obj)
 
 struct max_t {
     VALUE max;
-    int opt_methods;
-    int opt_inited;
+    struct cmp_opt_data cmp_opt;
 };
 
 static VALUE
@@ -1531,7 +1529,7 @@ max_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
 	memo->max = i;
     }
     else {
-	if (OPTIMIZED_CMP(i, memo->max, memo) > 0) {
+	if (OPTIMIZED_CMP(i, memo->max, memo->cmp_opt) > 0) {
 	    memo->max = i;
 	}
     }
@@ -1595,8 +1593,8 @@ enum_max(int argc, VALUE *argv, VALUE obj)
        return nmin_run(obj, num, 0, 1);
 
     m->max = Qundef;
-    m->opt_methods = 0;
-    m->opt_inited = 0;
+    m->cmp_opt.opt_methods = 0;
+    m->cmp_opt.opt_inited = 0;
     if (rb_block_given_p()) {
 	rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)memo);
     }
@@ -1612,8 +1610,7 @@ struct minmax_t {
     VALUE min;
     VALUE max;
     VALUE last;
-    int opt_methods;
-    int opt_inited;
+    struct cmp_opt_data cmp_opt;
 };
 
 static void
@@ -1626,11 +1623,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->cmp_opt);
 	if (n < 0) {
 	    memo->min = i;
 	}
-	n = OPTIMIZED_CMP(j, memo->max, memo);
+	n = OPTIMIZED_CMP(j, memo->max, memo->cmp_opt);
 	if (n > 0) {
 	    memo->max = j;
 	}
@@ -1653,7 +1650,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->cmp_opt);
     if (n == 0)
         i = j;
     else if (n < 0) {
@@ -1743,8 +1740,8 @@ enum_minmax(VALUE obj)
 
     m->min = Qundef;
     m->last = Qundef;
-    m->opt_methods = 0;
-    m->opt_inited = 0;
+    m->cmp_opt.opt_methods = 0;
+    m->cmp_opt.opt_inited = 0;
     if (rb_block_given_p()) {
 	rb_block_call(obj, id_each, 0, 0, minmax_ii, memo);
 	if (m->last != Qundef)
diff --git a/internal.h b/internal.h
index 76746cc..aab4734 100644
--- a/internal.h
+++ b/internal.h
@@ -685,13 +685,18 @@ enum {
     cmp_optimizable_count
 };
 
+struct cmp_opt_data {
+    int opt_methods;
+    int opt_inited;
+};
+
 #define CMP_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(cmp_opt_,type))
 #define CMP_OPTIMIZABLE(data, type) \
-    (((data)->opt_inited & CMP_OPTIMIZABLE_BIT(type)) ? \
-     ((data)->opt_methods & CMP_OPTIMIZABLE_BIT(type)) : \
-     (((data)->opt_inited |= CMP_OPTIMIZABLE_BIT(type)), \
+    (((data).opt_inited & CMP_OPTIMIZABLE_BIT(type)) ? \
+     ((data).opt_methods & CMP_OPTIMIZABLE_BIT(type)) : \
+     (((data).opt_inited |= CMP_OPTIMIZABLE_BIT(type)), \
       rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \
-      ((data)->opt_methods |= CMP_OPTIMIZABLE_BIT(type))))
+      ((data).opt_methods |= CMP_OPTIMIZABLE_BIT(type))))
 
 /* ment is in method.h */
 
-- 
2.5.3

