* array.c (flatten): some performance improvements, based on a patch

from Yusuke ENDOH <mame AT tsg.ne.jp> in [ruby-core:13877].
  [ruby-core:13851]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14127 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2007-12-07 03:27:20 +00:00
Родитель ff2ec563b2
Коммит 2257138aa5
2 изменённых файлов: 60 добавлений и 45 удалений

Просмотреть файл

@ -1,4 +1,8 @@
Fri Dec 7 12:22:26 2007 Nobuyoshi Nakada <nobu@ruby-lang.org> Fri Dec 7 12:27:18 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* array.c (flatten): some performance improvements, based on a patch
from Yusuke ENDOH <mame AT tsg.ne.jp> in [ruby-core:13877].
[ruby-core:13851]
* thread.c (rb_exec_recursive): use Hash instead of Array for * thread.c (rb_exec_recursive): use Hash instead of Array for
performance improvement. [ruby-core:13898] performance improvement. [ruby-core:13898]

101
array.c
Просмотреть файл

@ -2755,35 +2755,51 @@ rb_ary_nitems(VALUE ary)
return LONG2NUM(n); return LONG2NUM(n);
} }
static long static VALUE
flatten(VALUE ary, long idx, VALUE ary2, VALUE memo, int level) flatten(VALUE ary, int level, int *modified)
{ {
VALUE id; long i = 0;
long i = idx; VALUE stack, result, tmp, elt;
long n, lim = idx + RARRAY_LEN(ary2); st_table *memo;
st_data_t id;
level--; stack = rb_ary_new();
id = rb_obj_id(ary2); result = rb_ary_new();
if (rb_ary_includes(memo, id)) { memo = st_init_numtable();
st_insert(memo, (st_data_t)ary, (st_data_t)Qtrue);
*modified = 0;
while (1) {
while (i < RARRAY_LEN(ary)) {
elt = RARRAY_PTR(ary)[i++];
tmp = rb_check_array_type(elt);
if (NIL_P(tmp) || (level >= 0 && RARRAY_LEN(stack) / 2 >= level)) {
rb_ary_push(result, elt);
}
else {
*modified = 1;
id = (st_data_t)tmp;
if (st_lookup(memo, id, 0)) {
rb_raise(rb_eArgError, "tried to flatten recursive array"); rb_raise(rb_eArgError, "tried to flatten recursive array");
} }
rb_ary_push(memo, id); st_insert(memo, id, (st_data_t)Qtrue);
rb_ary_splice(ary, idx, 1, ary2); rb_ary_push(stack, ary);
if (level != 0) { rb_ary_push(stack, LONG2NUM(i));
while (i < lim) { ary = tmp;
VALUE tmp; i = 0;
}
}
if (RARRAY_LEN(stack) == 0) {
break;
}
id = (st_data_t)ary;
st_delete(memo, &id, 0);
tmp = rb_ary_pop(stack);
i = NUM2LONG(tmp);
ary = rb_ary_pop(stack);
}
tmp = rb_check_array_type(rb_ary_elt(ary, i)); return result;
if (!NIL_P(tmp)) {
n = flatten(ary, i, tmp, memo, level);
i += n; lim += n;
}
i++;
}
}
rb_ary_pop(memo);
return lim - idx - 1; /* returns number of increased items */
} }
/* /*
@ -2807,30 +2823,17 @@ flatten(VALUE ary, long idx, VALUE ary2, VALUE memo, int level)
static VALUE static VALUE
rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary) rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary)
{ {
long i = 0; int mod = 0, level = -1;
int mod = 0; VALUE result, lv;
int level = -1;
VALUE memo = Qnil;
VALUE lv;
rb_scan_args(argc, argv, "01", &lv); rb_scan_args(argc, argv, "01", &lv);
if (!NIL_P(lv)) level = NUM2INT(lv); if (!NIL_P(lv)) level = NUM2INT(lv);
if (level == 0) return ary; if (level == 0) return ary;
while (i<RARRAY_LEN(ary)) {
VALUE ary2 = RARRAY_PTR(ary)[i];
VALUE tmp;
tmp = rb_check_array_type(ary2); result = flatten(ary, level, &mod);
if (!NIL_P(tmp)) {
if (NIL_P(memo)) {
memo = rb_ary_new();
}
i += flatten(ary, i, tmp, memo, level);
mod = 1;
}
i++;
}
if (mod == 0) return Qnil; if (mod == 0) return Qnil;
rb_ary_replace(ary, result);
return ary; return ary;
} }
@ -2855,9 +2858,17 @@ rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary)
static VALUE static VALUE
rb_ary_flatten(int argc, VALUE *argv, VALUE ary) rb_ary_flatten(int argc, VALUE *argv, VALUE ary)
{ {
ary = rb_ary_dup(ary); int mod = 0, level = -1;
rb_ary_flatten_bang(argc, argv, ary); VALUE result, lv;
return ary;
rb_scan_args(argc, argv, "01", &lv);
if (!NIL_P(lv)) level = NUM2INT(lv);
if (level == 0) return ary;
result = flatten(ary, level, &mod);
if (OBJ_TAINTED(ary)) OBJ_TAINT(result);
return result;
} }
/* /*