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

ruby-changes:52774

From: ko1 <ko1@a...>
Date: Wed, 10 Oct 2018 13:17:06 +0900 (JST)
Subject: [ruby-changes:52774] ko1:r64986 (trunk): revisit `RARRAY_PTR()`.

ko1	2018-10-10 13:17:01 +0900 (Wed, 10 Oct 2018)

  New Revision: 64986

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

  Log:
    revisit `RARRAY_PTR()`.
    
    * array.c (yield_indexed_values): use RARRAY_AREF/ASET instead of
      using RARRAY_PTR().
    
    * enum.c (nmin_filter): ditto.
    
    * proc.c (rb_sym_to_proc): ditto.
    
    * enum.c (rb_nmin_run): use RARRAY_PTR_USE() instead of RARRAY_PTR().
      It is safe because they don't make new referecen from an array.

  Modified files:
    trunk/array.c
    trunk/enum.c
    trunk/proc.c
Index: array.c
===================================================================
--- array.c	(revision 64985)
+++ array.c	(revision 64986)
@@ -5228,11 +5228,9 @@ static int https://github.com/ruby/ruby/blob/trunk/array.c#L5228
 yield_indexed_values(const VALUE values, const long r, const long *const p)
 {
     const VALUE result = rb_ary_new2(r);
-    VALUE *const result_array = RARRAY_PTR(result);
-    const VALUE *const values_array = RARRAY_CONST_PTR(values);
     long i;
 
-    for (i = 0; i < r; i++) result_array[i] = values_array[p[i]];
+    for (i = 0; i < r; i++) RARRAY_ASET(result, i, RARRAY_AREF(values, p[i]));
     ARY_SET_LEN(result, r);
     rb_yield(result);
     return !RBASIC(values)->klass;
Index: enum.c
===================================================================
--- enum.c	(revision 64985)
+++ enum.c	(revision 64986)
@@ -1440,7 +1440,7 @@ nmin_filter(struct nmin_data *data) https://github.com/ruby/ruby/blob/trunk/enum.c#L1440
 #undef GETPTR
 #undef SWAP
 
-    data->limit = RARRAY_PTR(data->buf)[store_index*eltsize]; /* the last pivot */
+    data->limit = RARRAY_AREF(data->buf, store_index*eltsize); /* the last pivot */
     data->curlen = data->n;
     rb_ary_resize(data->buf, data->n * eltsize);
 }
@@ -1518,18 +1518,22 @@ rb_nmin_run(VALUE obj, VALUE num, int by https://github.com/ruby/ruby/blob/trunk/enum.c#L1518
     result = data.buf;
     if (by) {
 	long i;
-	ruby_qsort(RARRAY_PTR(result),
-	           RARRAY_LEN(result)/2,
-		   sizeof(VALUE)*2,
-		   data.cmpfunc, (void *)&data);
-	for (i=1; i<RARRAY_LEN(result); i+=2) {
-	    RARRAY_PTR(result)[i/2] = RARRAY_PTR(result)[i];
-	}
+        RARRAY_PTR_USE(result, ptr, {
+            ruby_qsort(ptr,
+                       RARRAY_LEN(result)/2,
+                       sizeof(VALUE)*2,
+                       data.cmpfunc, (void *)&data);
+            for (i=1; i<RARRAY_LEN(result); i+=2) {
+                ptr[i/2] = ptr[i];
+            }
+        });
 	rb_ary_resize(result, RARRAY_LEN(result)/2);
     }
     else {
-	ruby_qsort(RARRAY_PTR(result), RARRAY_LEN(result), sizeof(VALUE),
-		   data.cmpfunc, (void *)&data);
+        RARRAY_PTR_USE(result, ptr, {
+            ruby_qsort(ptr, RARRAY_LEN(result), sizeof(VALUE),
+                       data.cmpfunc, (void *)&data);
+        });
     }
     if (rev) {
         rb_ary_reverse(result);
Index: proc.c
===================================================================
--- proc.c	(revision 64985)
+++ proc.c	(revision 64986)
@@ -1206,7 +1206,6 @@ rb_sym_to_proc(VALUE sym) https://github.com/ruby/ruby/blob/trunk/proc.c#L1206
     VALUE proc;
     long index;
     ID id;
-    VALUE *aryp;
 
     if (!sym_proc_cache) {
 	sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
@@ -1217,14 +1216,13 @@ rb_sym_to_proc(VALUE sym) https://github.com/ruby/ruby/blob/trunk/proc.c#L1216
     id = SYM2ID(sym);
     index = (id % SYM_PROC_CACHE_SIZE) << 1;
 
-    aryp = RARRAY_PTR(sym_proc_cache);
-    if (aryp[index] == sym) {
-	return aryp[index + 1];
+    if (RARRAY_AREF(sym_proc_cache, index) == sym) {
+        return RARRAY_AREF(sym_proc_cache, index + 1);
     }
     else {
-	proc = sym_proc_new(rb_cProc, ID2SYM(id));
-	aryp[index] = sym;
-	aryp[index + 1] = proc;
+        proc = sym_proc_new(rb_cProc, ID2SYM(id));
+        RARRAY_ASET(sym_proc_cache, index, sym);
+        RARRAY_ASET(sym_proc_cache, index + 1, proc);
 	return proc;
     }
 }

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

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