summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2022-03-11 11:45:14 -0500
committerPeter Zhu <peter@peterzhu.ca>2022-03-11 11:45:14 -0500
commit42e5ec941489c11a180ed75c6348419c79aefff1 (patch)
treee378659dc8545ce6e83e4a8c99abea620352f771 /array.c
parent2e4516be26e126ec9e7528d1de0d4a0b7332f6dd (diff)
Refactor duplicate code in rb_array_replace
In both cases in the if statement, we free heap allocated arrays and unshare shared arrays.
Diffstat (limited to 'array.c')
-rw-r--r--array.c19
1 files changed, 7 insertions, 12 deletions
diff --git a/array.c b/array.c
index 448bcd365e..f4dbf87fbb 100644
--- a/array.c
+++ b/array.c
@@ -4391,25 +4391,20 @@ rb_ary_replace(VALUE copy, VALUE orig)
orig = to_ary(orig);
if (copy == orig) return copy;
+ if (ARY_OWNS_HEAP_P(copy)) {
+ ary_heap_free(copy);
+ }
+ else if (ARY_SHARED_P(copy)) {
+ rb_ary_unshare(copy);
+ }
+
if (RARRAY_LEN(orig) <= RARRAY_EMBED_LEN_MAX) {
- if (ARY_OWNS_HEAP_P(copy)) {
- ary_heap_free(copy);
- }
- else if (ARY_SHARED_P(copy)) {
- rb_ary_unshare(copy);
- }
FL_SET_EMBED(copy);
ary_memcpy(copy, 0, RARRAY_LEN(orig), RARRAY_CONST_PTR_TRANSIENT(orig));
ARY_SET_LEN(copy, RARRAY_LEN(orig));
}
else {
VALUE shared_root = ary_make_shared(orig);
- if (ARY_OWNS_HEAP_P(copy)) {
- ary_heap_free(copy);
- }
- else {
- rb_ary_unshare_safe(copy);
- }
FL_UNSET_EMBED(copy);
ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));