Permalink
Browse files

* 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
  • Loading branch information...
shugo committed Jan 7, 2016
1 parent a1115a1 commit 9f44b77a18d4d6099174c6044261eb1611a147ea
Showing with 50 additions and 28 deletions.
  1. +5 −0 ChangeLog
  2. +2 −18 array.c
  3. +27 −10 enum.c
  4. +16 −0 internal.h
View
@@ -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
View
20 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);
}
View
37 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);
}
View
@@ -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 */

0 comments on commit 9f44b77

Please sign in to comment.