From 165b446166c65f4dd905569981d188cc3eee8a17 Mon Sep 17 00:00:00 2001 From: ko1 Date: Wed, 10 Oct 2018 03:52:20 +0000 Subject: [PATCH] revisit `RARRAY_PTR()`. * array.c (ary_memcpy0): remove traditional `RARRAY_PTR()` code. It's enough stable. * array.c (rb_ary_splice): add comment about wb-unprotect. * array.c (rotate_count): use `RARRAY_PTR_USE()` instead of `RARRAY_PTR()` to avoid wb-unprotect. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- array.c | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/array.c b/array.c index 7300b4f985..bf120a450a 100644 --- a/array.c +++ b/array.c @@ -171,28 +171,22 @@ ary_memfill(VALUE ary, long beg, long size, VALUE val) static void ary_memcpy0(VALUE ary, long beg, long argc, const VALUE *argv, VALUE buff_owner_ary) { -#if 1 assert(!ARY_SHARED_P(buff_owner_ary)); if (argc > (int)(128/sizeof(VALUE)) /* is magic number (cache line size) */) { - rb_gc_writebarrier_remember(buff_owner_ary); - RARRAY_PTR_USE(ary, ptr, { - MEMCPY(ptr+beg, argv, VALUE, argc); - }); + rb_gc_writebarrier_remember(buff_owner_ary); + RARRAY_PTR_USE(ary, ptr, { + MEMCPY(ptr+beg, argv, VALUE, argc); + }); } else { - int i; - RARRAY_PTR_USE(ary, ptr, { - for (i=0; i 0) { if (rofs != -1) rptr = RARRAY_CONST_PTR(ary) + rofs; + /* give up wb-protected ary */ MEMMOVE(RARRAY_PTR(ary) + beg, rptr, VALUE, rlen); } } @@ -2298,24 +2293,27 @@ rotate_count(long cnt, long len) return (cnt < 0) ? (len - (~cnt % len) - 1) : (cnt % len); } +static void +ary_rotate_ptr(VALUE *ptr, long len, long cnt) +{ + --len; + if (cnt < len) ary_reverse(ptr + cnt, ptr + len); + if (--cnt > 0) ary_reverse(ptr, ptr + cnt); + if (len > 0) ary_reverse(ptr, ptr + len); +} + VALUE rb_ary_rotate(VALUE ary, long cnt) { rb_ary_modify(ary); if (cnt != 0) { - VALUE *ptr = RARRAY_PTR(ary); - long len = RARRAY_LEN(ary); - - if (len > 0 && (cnt = rotate_count(cnt, len)) > 0) { - --len; - if (cnt < len) ary_reverse(ptr + cnt, ptr + len); - if (--cnt > 0) ary_reverse(ptr, ptr + cnt); - if (len > 0) ary_reverse(ptr, ptr + len); - return ary; - } + long len = RARRAY_LEN(ary); + if (len > 0 && (cnt = rotate_count(cnt, len)) > 0) { + RARRAY_PTR_USE(ary, ptr, ary_rotate_ptr(ptr, len, cnt)); + return ary; + } } - return Qnil; }