ruby-changes:41382
From: shugo <ko1@a...>
Date: Thu, 7 Jan 2016 22:06:09 +0900 (JST)
Subject: [ruby-changes:41382] shugo:r53454 (trunk): * enum.c (enum_minmax): optimize object comparison in
shugo 2016-01-07 22:06:23 +0900 (Thu, 07 Jan 2016) New Revision: 53454 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53454 Log: * enum.c (enum_minmax): optimize object comparison in Enumerable#minmax. Modified files: trunk/ChangeLog trunk/array.c trunk/enum.c trunk/internal.h Index: enum.c =================================================================== --- enum.c (revision 53453) +++ enum.c (revision 53454) @@ -1589,9 +1589,24 @@ struct minmax_t { https://github.com/ruby/ruby/blob/trunk/enum.c#L1589 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 https://github.com/ruby/ruby/blob/trunk/enum.c#L1618 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 https://github.com/ruby/ruby/blob/trunk/enum.c#L1632 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, _ https://github.com/ruby/ruby/blob/trunk/enum.c#L1645 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, struc https://github.com/ruby/ruby/blob/trunk/enum.c#L1684 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, https://github.com/ruby/ruby/blob/trunk/enum.c#L1730 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); } Index: ChangeLog =================================================================== --- ChangeLog (revision 53453) +++ ChangeLog (revision 53454) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Jan 7 22:02:21 2016 Shugo Maeda <shugo@r...> + + * enum.c (enum_minmax): optimize object comparison in + Enumerable#minmax. + Thu Jan 7 14:49:12 2016 Nobuyoshi Nakada <nobu@r...> * thread.c (rb_thread_pending_interrupt_p): no pending interrupt Index: internal.h =================================================================== --- internal.h (revision 53453) +++ internal.h (revision 53454) @@ -633,6 +633,22 @@ struct MEMO { https://github.com/ruby/ruby/blob/trunk/internal.h#L633 #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 */ Index: array.c =================================================================== --- array.c (revision 53453) +++ array.c (revision 53454) @@ -2368,22 +2368,6 @@ struct ary_sort_data { https://github.com/ruby/ruby/blob/trunk/array.c#L2368 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, v https://github.com/ruby/ruby/blob/trunk/array.c#L2399 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); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/