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

ruby-changes:62576

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Sat, 15 Aug 2020 12:09:45 +0900 (JST)
Subject: [ruby-changes:62576] ff30358d13 (master): RARRAY_AREF: convert into an inline function

https://git.ruby-lang.org/ruby.git/commit/?id=ff30358d13

From ff30358d13d24d8202f2717c43700be70bdd49d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Fri, 14 Aug 2020 14:45:23 +0900
Subject: RARRAY_AREF: convert into an inline function

RARRAY_AREF has been a macro for reasons.  We might not be able to
change that for public APIs, but why not relax the situation internally
to make it an inline function.

diff --git a/array.c b/array.c
index d3e1985..ae2a4fd 100644
--- a/array.c
+++ b/array.c
@@ -7221,7 +7221,7 @@ rb_ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VA https://github.com/ruby/ruby/blob/trunk/array.c#L7221
 	return rb_ary_new_capa(0);
       case 1:
 	i = rnds[0];
-	return rb_ary_new_from_values(1, &RARRAY_AREF(ary, i));
+	return rb_ary_new_from_args(1, RARRAY_AREF(ary, i));
       case 2:
 	i = rnds[0];
 	j = rnds[1];
diff --git a/complex.c b/complex.c
index 3e11867..b8dd616 100644
--- a/complex.c
+++ b/complex.c
@@ -19,6 +19,7 @@ https://github.com/ruby/ruby/blob/trunk/complex.c#L19
 #define NDEBUG
 #include "id.h"
 #include "internal.h"
+#include "internal/array.h"
 #include "internal/class.h"
 #include "internal/complex.h"
 #include "internal/math.h"
diff --git a/dir.c b/dir.c
index 9571108..e5b6705 100644
--- a/dir.c
+++ b/dir.c
@@ -105,6 +105,7 @@ char *strchr(char*,char); https://github.com/ruby/ruby/blob/trunk/dir.c#L105
 #include "encindex.h"
 #include "id.h"
 #include "internal.h"
+#include "internal/array.h"
 #include "internal/dir.h"
 #include "internal/encoding.h"
 #include "internal/error.h"
diff --git a/enumerator.c b/enumerator.c
index 77cf565..3ea308a 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -2087,7 +2087,8 @@ lazy_flat_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo https://github.com/ruby/ruby/blob/trunk/enumerator.c#L2087
         long i;
         LAZY_MEMO_RESET_BREAK(result);
         for (i = 0; i + 1 < RARRAY_LEN(ary); i++) {
-            lazy_yielder_yield(result, proc_index, 1, &RARRAY_AREF(ary, i));
+            const VALUE argv = RARRAY_AREF(ary, i);
+            lazy_yielder_yield(result, proc_index, 1, &argv);
         }
         if (break_p) LAZY_MEMO_SET_BREAK(result);
         if (i >= RARRAY_LEN(ary)) return 0;
diff --git a/include/ruby/internal/core/rarray.h b/include/ruby/internal/core/rarray.h
index a21500e..938e2dc 100644
--- a/include/ruby/internal/core/rarray.h
+++ b/include/ruby/internal/core/rarray.h
@@ -256,20 +256,15 @@ RARRAY_ASET(VALUE ary, long i, VALUE v) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/core/rarray.h#L256
         RB_OBJ_WRITE(ary, &ptr[i], v));
 }
 
-/* RARRAY_AREF is used as a lvalue.  Cannot be a function. */
-#if 0
-RBIMPL_ATTR_PURE_UNLESS_DEBUG()
-RBIMPL_ATTR_ARTIFICIAL()
-static inline VALUE
-RARRAY_AREF(VALUE ary, long i)
-{
-    RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
-
-    return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
-}
-#else
-# undef RARRAY_AREF
-# define RARRAY_AREF(a, i) RARRAY_CONST_PTR_TRANSIENT(a)[i]
-#endif
+/*
+ * :FIXME: we want to convert RARRAY_AREF into an inline function (to add rooms
+ * for more sanity checks).  However there were situations where the address of
+ * this macro is taken i.e. &RARRAY_AREF(...).  They cannot be possible if this
+ * is not a  macro.  Such usages are abuse, and  we eliminated them internally.
+ * However we are afraid  of similar things to remain in  the wild.  This macro
+ * remains as  it is due to  that.  If we could  warn such usages we  can set a
+ * transition path, but currently no way is found to do so.
+ */
+#define RARRAY_AREF(a, i) RARRAY_CONST_PTR_TRANSIENT(a)[i]
 
 #endif /* RBIMPL_RARRAY_H */
diff --git a/internal.h b/internal.h
index da0dab5..303029a 100644
--- a/internal.h
+++ b/internal.h
@@ -31,6 +31,9 @@ https://github.com/ruby/ruby/blob/trunk/internal.h#L31
 /* Following macros were formerly defined in this header but moved to somewhere
  * else.  In order to detect them we undef here. */
 
+/* internal/array.h */
+#undef RARRAY_AREF
+
 /* internal/class.h */
 #undef RClass
 #undef RCLASS_SUPER
diff --git a/internal/array.h b/internal/array.h
index 2e53d43..c9f2b47 100644
--- a/internal/array.h
+++ b/internal/array.h
@@ -100,4 +100,15 @@ RARY_TRANSIENT_UNSET(VALUE ary) https://github.com/ruby/ruby/blob/trunk/internal/array.h#L100
     })
 #endif
 
+#undef RARRAY_AREF
+RBIMPL_ATTR_PURE_UNLESS_DEBUG()
+RBIMPL_ATTR_ARTIFICIAL()
+static inline VALUE
+RARRAY_AREF(VALUE ary, long i)
+{
+    RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
+
+    return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
+}
+
 #endif /* INTERNAL_ARRAY_H */
diff --git a/marshal.c b/marshal.c
index 480ae59..2c6ed7a 100644
--- a/marshal.c
+++ b/marshal.c
@@ -22,6 +22,7 @@ https://github.com/ruby/ruby/blob/trunk/marshal.c#L22
 #include "encindex.h"
 #include "id_table.h"
 #include "internal.h"
+#include "internal/array.h"
 #include "internal/bignum.h"
 #include "internal/class.h"
 #include "internal/encoding.h"
diff --git a/pack.c b/pack.c
index b6d252a..9fecc31 100644
--- a/pack.c
+++ b/pack.c
@@ -17,6 +17,7 @@ https://github.com/ruby/ruby/blob/trunk/pack.c#L17
 #include <sys/types.h>
 
 #include "internal.h"
+#include "internal/array.h"
 #include "internal/bits.h"
 #include "internal/string.h"
 #include "internal/symbol.h"
diff --git a/random.c b/random.c
index 8a59d1c..d213f69 100644
--- a/random.c
+++ b/random.c
@@ -56,6 +56,7 @@ https://github.com/ruby/ruby/blob/trunk/random.c#L56
 #endif
 
 #include "internal.h"
+#include "internal/array.h"
 #include "internal/compilers.h"
 #include "internal/numeric.h"
 #include "internal/random.h"
diff --git a/rational.c b/rational.c
index 967cdce..aa90342 100644
--- a/rational.c
+++ b/rational.c
@@ -24,6 +24,7 @@ https://github.com/ruby/ruby/blob/trunk/rational.c#L24
 #define NDEBUG
 #include "id.h"
 #include "internal.h"
+#include "internal/array.h"
 #include "internal/complex.h"
 #include "internal/gc.h"
 #include "internal/numeric.h"
diff --git a/transcode.c b/transcode.c
index fdc3514..3a37c63 100644
--- a/transcode.c
+++ b/transcode.c
@@ -14,6 +14,7 @@ https://github.com/ruby/ruby/blob/trunk/transcode.c#L14
 #include <ctype.h>
 
 #include "internal.h"
+#include "internal/array.h"
 #include "internal/inits.h"
 #include "internal/object.h"
 #include "internal/string.h"
-- 
cgit v0.10.2


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

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