mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
a1115a1b47
commit
9f44b77a18
4 changed files with 50 additions and 28 deletions
|
@ -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>
|
Thu Jan 7 14:49:12 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* thread.c (rb_thread_pending_interrupt_p): no pending interrupt
|
* thread.c (rb_thread_pending_interrupt_p): no pending interrupt
|
||||||
|
|
20
array.c
20
array.c
|
@ -2368,22 +2368,6 @@ struct ary_sort_data {
|
||||||
int opt_inited;
|
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
|
static VALUE
|
||||||
sort_reentered(VALUE ary)
|
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;
|
VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
|
||||||
int n;
|
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;
|
||||||
if ((long)a < (long)b) return -1;
|
if ((long)a < (long)b) return -1;
|
||||||
return 0;
|
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);
|
return rb_str_cmp(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
37
enum.c
37
enum.c
|
@ -1589,9 +1589,24 @@ struct minmax_t {
|
||||||
VALUE min;
|
VALUE min;
|
||||||
VALUE max;
|
VALUE max;
|
||||||
VALUE last;
|
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
|
static void
|
||||||
minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
|
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;
|
memo->max = j;
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
if (n < 0) {
|
||||||
memo->min = i;
|
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) {
|
if (n > 0) {
|
||||||
memo->max = j;
|
memo->max = j;
|
||||||
}
|
}
|
||||||
|
@ -1617,7 +1632,7 @@ minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
|
||||||
static VALUE
|
static VALUE
|
||||||
minmax_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
|
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;
|
int n;
|
||||||
VALUE j;
|
VALUE j;
|
||||||
|
|
||||||
|
@ -1630,7 +1645,7 @@ minmax_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
|
||||||
j = memo->last;
|
j = memo->last;
|
||||||
memo->last = Qundef;
|
memo->last = Qundef;
|
||||||
|
|
||||||
n = rb_cmpint(rb_funcall(j, id_cmp, 1, i), j, i);
|
n = optimized_cmp(j, i, memo);
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
i = j;
|
i = j;
|
||||||
else if (n < 0) {
|
else if (n < 0) {
|
||||||
|
@ -1669,7 +1684,7 @@ minmax_ii_update(VALUE i, VALUE j, struct minmax_t *memo)
|
||||||
static VALUE
|
static VALUE
|
||||||
minmax_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
|
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;
|
int n;
|
||||||
VALUE j;
|
VALUE j;
|
||||||
|
|
||||||
|
@ -1715,18 +1730,20 @@ minmax_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
|
||||||
static VALUE
|
static VALUE
|
||||||
enum_minmax(VALUE obj)
|
enum_minmax(VALUE obj)
|
||||||
{
|
{
|
||||||
struct MEMO *memo = MEMO_NEW(Qundef, Qundef, Qundef);
|
VALUE memo;
|
||||||
struct minmax_t *m = (struct minmax_t *)&memo->v1;
|
struct minmax_t *m = NEW_MEMO_FOR(struct minmax_t, memo);
|
||||||
|
|
||||||
m->min = Qundef;
|
m->min = Qundef;
|
||||||
m->last = Qundef;
|
m->last = Qundef;
|
||||||
|
m->opt_methods = 0;
|
||||||
|
m->opt_inited = 0;
|
||||||
if (rb_block_given_p()) {
|
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)
|
if (m->last != Qundef)
|
||||||
minmax_ii_update(m->last, m->last, m);
|
minmax_ii_update(m->last, m->last, m);
|
||||||
}
|
}
|
||||||
else {
|
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)
|
if (m->last != Qundef)
|
||||||
minmax_i_update(m->last, m->last, m);
|
minmax_i_update(m->last, m->last, m);
|
||||||
}
|
}
|
||||||
|
|
16
internal.h
16
internal.h
|
@ -633,6 +633,22 @@ struct MEMO {
|
||||||
#define NEW_MEMO_FOR(type, value) \
|
#define NEW_MEMO_FOR(type, value) \
|
||||||
((value) = rb_ary_tmp_new_fill(type_roomof(type, VALUE)), 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 */
|
/* ment is in method.h */
|
||||||
|
|
||||||
/* global variable */
|
/* global variable */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue