From 42e5ec941489c11a180ed75c6348419c79aefff1 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 11 Mar 2022 11:45:14 -0500 Subject: [PATCH] Refactor duplicate code in rb_array_replace In both cases in the if statement, we free heap allocated arrays and unshare shared arrays. --- array.c | 19 +++++++------------ 1 file 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));