mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enum.c (enum_min, enum_max): do the same optimization as r53454.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53456 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
98e565ec78
commit
3dcd4b2a98
2 changed files with 62 additions and 50 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Fri Jan 8 00:03:22 2016 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
|
* enum.c (enum_min, enum_max): do the same optimization as r53454.
|
||||||
|
|
||||||
Thu Jan 7 22:32:21 2016 Kenta Murata <mrkn@mrkn.jp>
|
Thu Jan 7 22:32:21 2016 Kenta Murata <mrkn@mrkn.jp>
|
||||||
|
|
||||||
* ruby.h: undef HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P
|
* ruby.h: undef HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P
|
||||||
|
|
108
enum.c
108
enum.c
|
@ -1412,21 +1412,32 @@ enum_none(VALUE obj)
|
||||||
return memo->v1;
|
return memo->v1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define OPTIMIZED_CMP(a, b, data) \
|
||||||
|
((FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data, Fixnum)) ? \
|
||||||
|
(((long)a > (long)b) ? 1 : ((long)a < (long)b) ? -1 : 0) : \
|
||||||
|
(STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data, String)) ? \
|
||||||
|
rb_str_cmp(a, b) : \
|
||||||
|
rb_cmpint(rb_funcallv(a, id_cmp, 1, &b), a, b))
|
||||||
|
|
||||||
|
struct min_t {
|
||||||
|
VALUE min;
|
||||||
|
int opt_methods;
|
||||||
|
int opt_inited;
|
||||||
|
};
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
min_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
min_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
{
|
{
|
||||||
VALUE cmp;
|
struct min_t *memo = MEMO_FOR(struct min_t, args);
|
||||||
struct MEMO *memo = MEMO_CAST(args);
|
|
||||||
|
|
||||||
ENUM_WANT_SVALUE();
|
ENUM_WANT_SVALUE();
|
||||||
|
|
||||||
if (memo->v1 == Qundef) {
|
if (memo->min == Qundef) {
|
||||||
MEMO_V1_SET(memo, i);
|
memo->min = i;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cmp = rb_funcall(i, id_cmp, 1, memo->v1);
|
if (OPTIMIZED_CMP(i, memo->min, memo) < 0) {
|
||||||
if (rb_cmpint(cmp, i, memo->v1) < 0) {
|
memo->min = i;
|
||||||
MEMO_V1_SET(memo, i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Qnil;
|
return Qnil;
|
||||||
|
@ -1436,17 +1447,17 @@ static VALUE
|
||||||
min_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
min_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
{
|
{
|
||||||
VALUE cmp;
|
VALUE cmp;
|
||||||
struct MEMO *memo = MEMO_CAST(args);
|
struct min_t *memo = MEMO_FOR(struct min_t, args);
|
||||||
|
|
||||||
ENUM_WANT_SVALUE();
|
ENUM_WANT_SVALUE();
|
||||||
|
|
||||||
if (memo->v1 == Qundef) {
|
if (memo->min == Qundef) {
|
||||||
MEMO_V1_SET(memo, i);
|
memo->min = i;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cmp = rb_yield_values(2, i, memo->v1);
|
cmp = rb_yield_values(2, i, memo->min);
|
||||||
if (rb_cmpint(cmp, i, memo->v1) < 0) {
|
if (rb_cmpint(cmp, i, memo->min) < 0) {
|
||||||
MEMO_V1_SET(memo, i);
|
memo->min = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Qnil;
|
return Qnil;
|
||||||
|
@ -1479,7 +1490,8 @@ min_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
static VALUE
|
static VALUE
|
||||||
enum_min(int argc, VALUE *argv, VALUE obj)
|
enum_min(int argc, VALUE *argv, VALUE obj)
|
||||||
{
|
{
|
||||||
struct MEMO *memo = MEMO_NEW(Qundef, 0, 0);
|
VALUE memo;
|
||||||
|
struct min_t *m = NEW_MEMO_FOR(struct min_t, memo);
|
||||||
VALUE result;
|
VALUE result;
|
||||||
VALUE num;
|
VALUE num;
|
||||||
|
|
||||||
|
@ -1488,32 +1500,39 @@ enum_min(int argc, VALUE *argv, VALUE obj)
|
||||||
if (!NIL_P(num))
|
if (!NIL_P(num))
|
||||||
return nmin_run(obj, num, 0, 0);
|
return nmin_run(obj, num, 0, 0);
|
||||||
|
|
||||||
|
m->min = 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, min_ii, (VALUE)memo);
|
rb_block_call(obj, id_each, 0, 0, min_ii, memo);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rb_block_call(obj, id_each, 0, 0, min_i, (VALUE)memo);
|
rb_block_call(obj, id_each, 0, 0, min_i, memo);
|
||||||
}
|
}
|
||||||
result = memo->v1;
|
result = m->min;
|
||||||
if (result == Qundef) return Qnil;
|
if (result == Qundef) return Qnil;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct max_t {
|
||||||
|
VALUE max;
|
||||||
|
int opt_methods;
|
||||||
|
int opt_inited;
|
||||||
|
};
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
max_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
max_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
{
|
{
|
||||||
struct MEMO *memo = MEMO_CAST(args);
|
struct max_t *memo = MEMO_FOR(struct max_t, args);
|
||||||
VALUE cmp;
|
|
||||||
|
|
||||||
ENUM_WANT_SVALUE();
|
ENUM_WANT_SVALUE();
|
||||||
|
|
||||||
if (memo->v1 == Qundef) {
|
if (memo->max == Qundef) {
|
||||||
MEMO_V1_SET(memo, i);
|
memo->max = i;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cmp = rb_funcall(i, id_cmp, 1, memo->v1);
|
if (OPTIMIZED_CMP(i, memo->max, memo) > 0) {
|
||||||
if (rb_cmpint(cmp, i, memo->v1) > 0) {
|
memo->max = i;
|
||||||
MEMO_V1_SET(memo, i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Qnil;
|
return Qnil;
|
||||||
|
@ -1522,18 +1541,18 @@ max_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
static VALUE
|
static VALUE
|
||||||
max_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
max_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
{
|
{
|
||||||
struct MEMO *memo = MEMO_CAST(args);
|
struct max_t *memo = MEMO_FOR(struct max_t, args);
|
||||||
VALUE cmp;
|
VALUE cmp;
|
||||||
|
|
||||||
ENUM_WANT_SVALUE();
|
ENUM_WANT_SVALUE();
|
||||||
|
|
||||||
if (memo->v1 == Qundef) {
|
if (memo->max == Qundef) {
|
||||||
MEMO_V1_SET(memo, i);
|
memo->max = i;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cmp = rb_yield_values(2, i, memo->v1);
|
cmp = rb_yield_values(2, i, memo->max);
|
||||||
if (rb_cmpint(cmp, i, memo->v1) > 0) {
|
if (rb_cmpint(cmp, i, memo->max) > 0) {
|
||||||
MEMO_V1_SET(memo, i);
|
memo->max = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Qnil;
|
return Qnil;
|
||||||
|
@ -1565,7 +1584,8 @@ max_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
|
||||||
static VALUE
|
static VALUE
|
||||||
enum_max(int argc, VALUE *argv, VALUE obj)
|
enum_max(int argc, VALUE *argv, VALUE obj)
|
||||||
{
|
{
|
||||||
struct MEMO *memo = MEMO_NEW(Qundef, 0, 0);
|
VALUE memo;
|
||||||
|
struct max_t *m = NEW_MEMO_FOR(struct max_t, memo);
|
||||||
VALUE result;
|
VALUE result;
|
||||||
VALUE num;
|
VALUE num;
|
||||||
|
|
||||||
|
@ -1574,13 +1594,16 @@ enum_max(int argc, VALUE *argv, VALUE obj)
|
||||||
if (!NIL_P(num))
|
if (!NIL_P(num))
|
||||||
return nmin_run(obj, num, 0, 1);
|
return nmin_run(obj, num, 0, 1);
|
||||||
|
|
||||||
|
m->max = 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, max_ii, (VALUE)memo);
|
rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)memo);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rb_block_call(obj, id_each, 0, 0, max_i, (VALUE)memo);
|
rb_block_call(obj, id_each, 0, 0, max_i, (VALUE)memo);
|
||||||
}
|
}
|
||||||
result = memo->v1;
|
result = m->max;
|
||||||
if (result == Qundef) return Qnil;
|
if (result == Qundef) return Qnil;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1593,21 +1616,6 @@ struct minmax_t {
|
||||||
int opt_inited;
|
int opt_inited;
|
||||||
};
|
};
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
@ -1618,11 +1626,11 @@ minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
|
||||||
memo->max = j;
|
memo->max = j;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
n = optimized_cmp(i, memo->min, memo);
|
n = OPTIMIZED_CMP(i, memo->min, memo);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
memo->min = i;
|
memo->min = i;
|
||||||
}
|
}
|
||||||
n = optimized_cmp(j, memo->max, memo);
|
n = OPTIMIZED_CMP(j, memo->max, memo);
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
memo->max = j;
|
memo->max = j;
|
||||||
}
|
}
|
||||||
|
@ -1645,7 +1653,7 @@ minmax_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
|
||||||
j = memo->last;
|
j = memo->last;
|
||||||
memo->last = Qundef;
|
memo->last = Qundef;
|
||||||
|
|
||||||
n = optimized_cmp(j, i, memo);
|
n = OPTIMIZED_CMP(j, i, memo);
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
i = j;
|
i = j;
|
||||||
else if (n < 0) {
|
else if (n < 0) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue