[前][次][番号順一覧][スレッド一覧]

ruby-changes:52773

From: ko1 <ko1@a...>
Date: Wed, 10 Oct 2018 12:52:27 +0900 (JST)
Subject: [ruby-changes:52773] ko1:r64985 (trunk): revisit `RARRAY_PTR()`.

ko1	2018-10-10 12:52:20 +0900 (Wed, 10 Oct 2018)

  New Revision: 64985

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64985

  Log:
    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.

  Modified files:
    trunk/array.c
Index: array.c
===================================================================
--- array.c	(revision 64984)
+++ array.c	(revision 64985)
@@ -171,28 +171,22 @@ ary_memfill(VALUE ary, long beg, long si https://github.com/ruby/ruby/blob/trunk/array.c#L171
 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<argc; i++) {
-		RB_OBJ_WRITE(buff_owner_ary, &ptr[i+beg], argv[i]);
-	    }
-	});
+        int i;
+        RARRAY_PTR_USE(ary, ptr, {
+            for (i=0; i<argc; i++) {
+                RB_OBJ_WRITE(buff_owner_ary, &ptr[i+beg], argv[i]);
+            }
+        });
     }
-#else
-    /* giveup write barrier (traditional way) */
-    RARRAY_PTR(buff_owner_ary);
-    MEMCPY(RARRAY_PTR(ary)+beg, argv, VALUE, argc);
-#endif
 }
 
 static void
@@ -1620,6 +1614,7 @@ rb_ary_splice(VALUE ary, long beg, long https://github.com/ruby/ruby/blob/trunk/array.c#L1614
 	}
 	if (rlen > 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) https://github.com/ruby/ruby/blob/trunk/array.c#L2293
     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;
 }
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]