rb_ary_behead: do not goto into a branch

I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
This commit is contained in:
卜部昌平 2020-06-10 13:42:30 +09:00
Родитель 09b936d89c
Коммит 2e8d8d10f2
1 изменённых файлов: 36 добавлений и 22 удалений

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

@ -1524,34 +1524,48 @@ rb_ary_shift_m(int argc, VALUE *argv, VALUE ary)
return result;
}
static VALUE
behead_shared(VALUE ary, long n)
{
assert(ARY_SHARED_P(ary));
rb_ary_modify_check(ary);
if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(ary))) {
ary_mem_clear(ary, 0, n);
}
ARY_INCREASE_PTR(ary, n);
ARY_INCREASE_LEN(ary, -n);
ary_verify(ary);
return ary;
}
static VALUE
behead_transient(VALUE ary, long n)
{
rb_ary_modify_check(ary);
RARRAY_PTR_USE_TRANSIENT(ary, ptr, {
MEMMOVE(ptr, ptr+n, VALUE, RARRAY_LEN(ary)-n);
}); /* WB: no new reference */
ARY_INCREASE_LEN(ary, -n);
ary_verify(ary);
return ary;
}
MJIT_FUNC_EXPORTED VALUE
rb_ary_behead(VALUE ary, long n)
{
if (n<=0) return ary;
rb_ary_modify_check(ary);
if (ARY_SHARED_P(ary)) {
if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(ary))) {
setup_occupied_shared:
ary_mem_clear(ary, 0, n);
}
ARY_INCREASE_PTR(ary, n);
if (n <= 0) {
return ary;
}
else if (ARY_SHARED_P(ary)) {
return behead_shared(ary, n);
}
else if (RARRAY_LEN(ary) >= ARY_DEFAULT_SIZE) {
ary_make_shared(ary);
return behead_shared(ary, n);
}
else {
if (RARRAY_LEN(ary) < ARY_DEFAULT_SIZE) {
RARRAY_PTR_USE_TRANSIENT(ary, ptr, {
MEMMOVE(ptr, ptr+n, VALUE, RARRAY_LEN(ary)-n);
}); /* WB: no new reference */
}
else {
ary_make_shared(ary);
goto setup_occupied_shared;
}
return behead_transient(ary, n);
}
ARY_INCREASE_LEN(ary, -n);
ary_verify(ary);
return ary;
}
static VALUE