ruby-changes:72621
From: Peter <ko1@a...>
Date: Thu, 21 Jul 2022 22:03:03 +0900 (JST)
Subject: [ruby-changes:72621] 1c9acb6bb1 (master): Refactor macros of array.c
https://git.ruby-lang.org/ruby.git/commit/?id=1c9acb6bb1 From 1c9acb6bb1822f9d914b40dcea0b3ead849165cd Mon Sep 17 00:00:00 2001 From: Peter Zhu <peter@p...> Date: Wed, 20 Jul 2022 15:30:19 -0400 Subject: Refactor macros of array.c Move some macros in array.c to internal/array.h so that other files can also access these macros. --- array.c | 42 +++++++----------------------------------- common.mk | 1 + gc.c | 28 +++++++++------------------- internal/array.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- transient_heap.c | 3 ++- 5 files changed, 73 insertions(+), 56 deletions(-) diff --git a/array.c b/array.c index deea0834d7..180f2ff20f 100644 --- a/array.c +++ b/array.c @@ -53,23 +53,6 @@ should_be_T_ARRAY(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L53 return RB_TYPE_P(ary, T_ARRAY); } -RBIMPL_ATTR_MAYBE_UNUSED() -static int -should_not_be_shared_and_embedded(VALUE ary) -{ - return !FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG); -} - -#define ARY_SHARED_P(ary) \ - (assert(should_be_T_ARRAY((VALUE)(ary))), \ - assert(should_not_be_shared_and_embedded((VALUE)ary)), \ - FL_TEST_RAW((ary),ELTS_SHARED)!=0) - -#define ARY_EMBED_P(ary) \ - (assert(should_be_T_ARRAY((VALUE)(ary))), \ - assert(should_not_be_shared_and_embedded((VALUE)ary)), \ - FL_TEST_RAW((ary), RARRAY_EMBED_FLAG) != 0) - #define ARY_HEAP_PTR(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr) #define ARY_HEAP_LEN(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len) #define ARY_HEAP_CAPA(a) (assert(!ARY_EMBED_P(a)), assert(!ARY_SHARED_ROOT_P(a)), \ @@ -147,7 +130,6 @@ should_not_be_shared_and_embedded(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L130 RARRAY(ary)->as.heap.aux.capa = (n); \ } while (0) -#define ARY_SHARED_ROOT(ary) (assert(ARY_SHARED_P(ary)), RARRAY(ary)->as.heap.aux.shared_root) #define ARY_SET_SHARED(ary, value) do { \ const VALUE _ary_ = (ary); \ const VALUE _value_ = (value); \ @@ -158,11 +140,6 @@ should_not_be_shared_and_embedded(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L140 RB_OBJ_WRITE(_ary_, &RARRAY(_ary_)->as.heap.aux.shared_root, _value_); \ } while (0) -#define RARRAY_SHARED_ROOT_FLAG FL_USER12 -#define ARY_SHARED_ROOT_P(ary) (assert(should_be_T_ARRAY((VALUE)(ary))), \ - FL_TEST_RAW((ary), RARRAY_SHARED_ROOT_FLAG)) -#define ARY_SHARED_ROOT_REFCNT(ary) \ - (assert(ARY_SHARED_ROOT_P(ary)), RARRAY(ary)->as.heap.aux.capa) #define ARY_SHARED_ROOT_OCCUPIED(ary) (!ARY_LITERAL_P(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1) #define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \ assert(ARY_SHARED_ROOT_P(ary)); \ @@ -175,11 +152,6 @@ should_not_be_shared_and_embedded(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L152 FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \ } while (0) -#define RARRAY_LITERAL_FLAG FL_USER15 -#define ARY_LITERAL_P(ary) \ - (assert(should_be_T_ARRAY((VALUE)(ary))), \ - FL_TEST_RAW((ary), RARRAY_LITERAL_FLAG)) - static inline void ARY_SET(VALUE a, long i, VALUE v) { @@ -251,8 +223,8 @@ ary_verify_(VALUE ary, const char *file, int line) https://github.com/ruby/ruby/blob/trunk/array.c#L223 { assert(RB_TYPE_P(ary, T_ARRAY)); - if (FL_TEST(ary, ELTS_SHARED)) { - VALUE root = RARRAY(ary)->as.heap.aux.shared_root; + if (ARY_SHARED_P(ary)) { + VALUE root = ARY_SHARED_ROOT(ary); const VALUE *ptr = ARY_HEAP_PTR(ary); const VALUE *root_ptr = RARRAY_CONST_PTR_TRANSIENT(root); long len = ARY_HEAP_LEN(ary), root_len = RARRAY_LEN(root); @@ -597,7 +569,7 @@ rb_ary_decrement_share(VALUE shared_root) https://github.com/ruby/ruby/blob/trunk/array.c#L569 static void rb_ary_unshare(VALUE ary) { - VALUE shared_root = RARRAY(ary)->as.heap.aux.shared_root; + VALUE shared_root = ARY_SHARED_ROOT(ary); rb_ary_decrement_share(shared_root); FL_UNSET_SHARED(ary); } @@ -770,10 +742,10 @@ VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L742 rb_ary_shared_with_p(VALUE ary1, VALUE ary2) { if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) && - !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) && - RARRAY(ary1)->as.heap.aux.shared_root == RARRAY(ary2)->as.heap.aux.shared_root && - RARRAY(ary1)->as.heap.len == RARRAY(ary2)->as.heap.len) { - return Qtrue; + !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) && + ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) && + ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) { + return Qtrue; } return Qfalse; } diff --git a/common.mk b/common.mk index 2edee06520..d3cb32ad85 100644 --- a/common.mk +++ b/common.mk @@ -15797,6 +15797,7 @@ transcode.$(OBJEXT): {$(VPATH)}subst.h https://github.com/ruby/ruby/blob/trunk/common.mk#L15797 transcode.$(OBJEXT): {$(VPATH)}transcode.c transcode.$(OBJEXT): {$(VPATH)}transcode_data.h transient_heap.$(OBJEXT): $(hdrdir)/ruby/ruby.h +transient_heap.$(OBJEXT): $(top_srcdir)/internal/array.h transient_heap.$(OBJEXT): $(top_srcdir)/internal/compilers.h transient_heap.$(OBJEXT): $(top_srcdir)/internal/gc.h transient_heap.$(OBJEXT): $(top_srcdir)/internal/hash.h diff --git a/gc.c b/gc.c index 6246c8d637..4f92c371be 100644 --- a/gc.c +++ b/gc.c @@ -7205,10 +7205,10 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L7205 break; case T_ARRAY: - if (FL_TEST(obj, ELTS_SHARED)) { - VALUE root = any->as.array.as.heap.aux.shared_root; + if (ARY_SHARED_P(obj)) { + VALUE root = ARY_SHARED_ROOT(obj); gc_mark(objspace, root); - } + } else { long i, len = RARRAY_LEN(obj); const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(obj); @@ -7217,8 +7217,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L7217 } if (LIKELY(during_gc)) { - if (!FL_TEST_RAW(obj, RARRAY_EMBED_FLAG) && - RARRAY_TRANSIENT_P(obj)) { + if (!ARY_EMBED_P(obj) && RARRAY_TRANSIENT_P(obj)) { rb_transient_heap_mark(obj, ptr); } } @@ -9927,8 +9926,7 @@ gc_ref_update_array(rb_objspace_t * objspace, VALUE v) https://github.com/ruby/ruby/blob/trunk/gc.c#L9926 { long i, len; - if (FL_TEST(v, ELTS_SHARED)) - return; + if (ARY_SHARED_P(v)) return; len = RARRAY_LEN(v); if (len > 0) { @@ -10435,7 +10433,7 @@ gc_update_object_references(rb_objspace_t *objspace, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L10433 return; case T_ARRAY: - if (FL_TEST(obj, ELTS_SHARED)) { + if (ARY_SHARED_P(obj)) { UPDATE_IF_MOVED(objspace, any->as.array.as.heap.aux.shared_root); } else { @@ -13750,14 +13748,6 @@ rb_method_type_name(rb_method_type_t type) https://github.com/ruby/ruby/blob/trunk/gc.c#L13748 rb_bug("rb_method_type_name: unreachable (type: %d)", type); } -/* from array.c */ -# define ARY_SHARED_P(ary) \ - (GC_ASSERT(!FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG)), \ - FL_TEST((ary),ELTS_SHARED)!=0) -# define ARY_EMBED_P(ary) \ - (GC_ASSERT(!FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG)), \ - FL_TEST((ary), RARRAY_EMBED_FLAG)!=0) - static void rb_raw_iseq_info(char *const buff, const size_t buff_size, const rb_iseq_t *iseq) { @@ -13862,11 +13852,11 @@ rb_raw_obj_info_buitin_type(char *const buff, const size_t buff_size, const VALU https://github.com/ruby/ruby/blob/trunk/gc.c#L13852 UNEXPECTED_NODE(rb_raw_obj_info); break; case T_ARRAY: - if (FL_TEST(obj, ELTS_SHARED)) { + if (ARY_SHARED_P(obj)) { APPEND_S("shared -> "); - rb_raw_obj_info(BUFF_ARGS, RARRAY(obj)->as.heap.aux.shared_root); + rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj)); } - else if (FL_TEST(obj, RARRAY_EMBED_FLAG)) { + else if (ARY_EMBED_P(obj)) { APPEND_F("[%s%s] len: %ld (embed)", C(ARY_EMBED_P(obj), "E"), C(ARY_SHARED_P(obj), "S"), diff --git a/internal/array.h b/internal/array.h index aa3b540153..0c8ad3f3d6 100644 --- a/internal/array.h +++ b/internal/array.h @@ -18,7 +18,9 @@ https://github.com/ruby/ruby/blob/trunk/internal/array.h#L18 # define ARRAY_DEBUG (0+RUBY_DEBUG) #endif -#define RARRAY_PTR_IN_USE_FLAG FL_USER14 +#define RARRAY_SHARED_ROOT_FLAG FL_USER12 +#define RARRAY_PTR_IN_USE_FLAG FL_USER14 +#define RARRAY_LITERAL_FLAG FL_USER15 /* array.c */ VALUE rb_ary_last(int, const VALUE *, VALUE); @@ -73,6 +75,57 @@ ARY_PTR_USING_P(VALUE ary) https://github.com/ruby/ruby/blob/trunk/internal/array.h#L75 return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG); } +RBIMPL_ATTR_MAYBE_UNUSED() +static inline int +ary_should_not_be_shared_and_embedded(VALUE ary) +{ + return !FL_ALL_RAW(ary, ELTS_SHARED|RARRAY_EMBED_FLAG); +} + +static inline bool +ARY_SHARED_P(VALUE ary) +{ + assert(RB_TYPE_P(ary, T_ARRAY)); + assert(ary_should_not_be_shared_and_embedded(ary)); + return FL_TEST_RAW(ary, ELTS_SHARED); +} + +static inline bool +ARY_EMBED_P(VALUE ary) +{ + assert(RB_TYPE_P(ary, T_ARRAY)); + assert(ary_should_not_be_shared_and_embedded(ary)); + return FL_TEST_RAW(ary, RARRAY_EMBED_FLAG); +} + +static inline VALUE +ARY_SHARED_ROOT(VALUE ary) +{ + assert(ARY_SHARED_P(ary)); + return RARRAY(ary)->as.heap.aux.shared_root; +} + +static inline bool +ARY_SHARED_ROOT_P(VALUE ary) +{ + assert(RB_TYPE_P(ary, T_ARRAY)); + return FL_TEST_RAW(ary, RARRAY_SHARED_ROOT_FLAG); +} + +static inline long +ARY_SHARED_ROOT_REFCNT(VALUE ary) +{ + assert(ARY_SHARED_ROOT_P(ary)); + return RARRAY(ary)->as.heap.aux.capa; +} + +static inline bool +ARY_LITERAL_P(VALUE ary) +{ + assert(RB_TYPE_P(ary, T_ARRAY)); (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/