aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-07 13:06:23 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-07 13:06:23 +0000
commit8c4ab4996901013aa41e8a169fb0c0fa98cca3c4 (patch)
tree4d218c733f53d9c2fd9d53ad8a43d8a2080b0c08
parentc4e2d30fbfadda4087b5168099b4602ae806c667 (diff)
downloadruby-8c4ab4996901013aa41e8a169fb0c0fa98cca3c4.tar.gz
* enum.c (enum_minmax): optimize object comparison in
Enumerable#minmax. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53454 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--array.c20
-rw-r--r--enum.c37
-rw-r--r--internal.h16
4 files changed, 50 insertions, 28 deletions
diff --git a/ChangeLog b/ChangeLog
index 4c832bc9fc..71ba8fad1f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Jan 7 22:02:21 2016 Shugo Maeda <shugo@ruby-lang.org>
+
+ * enum.c (enum_minmax): optimize object comparison in
+ Enumerable#minmax.
+
Thu Jan 7 14:49:12 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* thread.c (rb_thread_pending_interrupt_p): no pending interrupt
diff --git a/array.c b/array.c
index dd14837b76..e31f67467f 100644
--- a/array.c
+++ b/array.c
@@ -2368,22 +2368,6 @@ struct ary_sort_data {
int opt_inited;
};
-enum {
- sort_opt_Fixnum,
- sort_opt_String,
- sort_optimizable_count
-};
-
-#define STRING_P(s) (RB_TYPE_P((s), T_STRING) && CLASS_OF(s) == rb_cString)
-
-#define SORT_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(sort_opt_,type))
-#define SORT_OPTIMIZABLE(data, type) \
- (((data)->opt_inited & SORT_OPTIMIZABLE_BIT(type)) ? \
- ((data)->opt_methods & SORT_OPTIMIZABLE_BIT(type)) : \
- (((data)->opt_inited |= SORT_OPTIMIZABLE_BIT(type)), \
- rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \
- ((data)->opt_methods |= SORT_OPTIMIZABLE_BIT(type))))
-
static VALUE
sort_reentered(VALUE ary)
{
@@ -2415,12 +2399,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) && SORT_OPTIMIZABLE(data, Fixnum)) {
+ 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) && SORT_OPTIMIZABLE(data, String)) {
+ if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data, String)) {
return rb_str_cmp(a, b);
}
diff --git a/enum.c b/enum.c
index 9ce5e6084e..4aaef097e5 100644
--- a/enum.c
+++ b/enum.c
@@ -1589,9 +1589,24 @@ struct minmax_t {
VALUE min;
VALUE max;
VALUE last;
+ int opt_methods;
+ int opt_inited;
};
-STATIC_ASSERT(minmax_t, sizeof(struct minmax_t) <= sizeof(struct MEMO) - offsetof(struct MEMO, v1));
+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)
@@ -1603,11 +1618,11 @@ minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
memo->max = j;
}
else {
- n = rb_cmpint(rb_funcall(i, id_cmp, 1, memo->min), i, memo->min);
+ n = optimized_cmp(i, memo->min, memo);
if (n < 0) {
memo->min = i;
}
- n = rb_cmpint(rb_funcall(j, id_cmp, 1, memo->max), j, memo->max);
+ n = optimized_cmp(j, memo->max, memo);
if (n > 0) {
memo->max = j;
}
@@ -1617,7 +1632,7 @@ minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
static VALUE
minmax_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
{
- struct minmax_t *memo = (struct minmax_t *)&MEMO_CAST(_memo)->v1;
+ struct minmax_t *memo = MEMO_FOR(struct minmax_t, _memo);
int n;
VALUE j;
@@ -1630,7 +1645,7 @@ minmax_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
j = memo->last;
memo->last = Qundef;
- n = rb_cmpint(rb_funcall(j, id_cmp, 1, i), j, i);
+ n = optimized_cmp(j, i, memo);
if (n == 0)
i = j;
else if (n < 0) {
@@ -1669,7 +1684,7 @@ minmax_ii_update(VALUE i, VALUE j, struct minmax_t *memo)
static VALUE
minmax_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
{
- struct minmax_t *memo = (struct minmax_t *)&MEMO_CAST(_memo)->v1;
+ struct minmax_t *memo = MEMO_FOR(struct minmax_t, _memo);
int n;
VALUE j;
@@ -1715,18 +1730,20 @@ minmax_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
static VALUE
enum_minmax(VALUE obj)
{
- struct MEMO *memo = MEMO_NEW(Qundef, Qundef, Qundef);
- struct minmax_t *m = (struct minmax_t *)&memo->v1;
+ VALUE memo;
+ struct minmax_t *m = NEW_MEMO_FOR(struct minmax_t, memo);
m->min = Qundef;
m->last = Qundef;
+ m->opt_methods = 0;
+ m->opt_inited = 0;
if (rb_block_given_p()) {
- rb_block_call(obj, id_each, 0, 0, minmax_ii, (VALUE)memo);
+ rb_block_call(obj, id_each, 0, 0, minmax_ii, memo);
if (m->last != Qundef)
minmax_ii_update(m->last, m->last, m);
}
else {
- rb_block_call(obj, id_each, 0, 0, minmax_i, (VALUE)memo);
+ rb_block_call(obj, id_each, 0, 0, minmax_i, memo);
if (m->last != Qundef)
minmax_i_update(m->last, m->last, m);
}
diff --git a/internal.h b/internal.h
index 95068ac0cd..5622998ff8 100644
--- a/internal.h
+++ b/internal.h
@@ -633,6 +633,22 @@ struct MEMO {
#define NEW_MEMO_FOR(type, value) \
((value) = rb_ary_tmp_new_fill(type_roomof(type, VALUE)), MEMO_FOR(type, value))
+#define STRING_P(s) (RB_TYPE_P((s), T_STRING) && CLASS_OF(s) == rb_cString)
+
+enum {
+ cmp_opt_Fixnum,
+ cmp_opt_String,
+ cmp_optimizable_count
+};
+
+#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)), \
+ rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \
+ ((data)->opt_methods |= CMP_OPTIMIZABLE_BIT(type))))
+
/* ment is in method.h */
/* global variable */