ruby-changes:71366
From: Peter <ko1@a...>
Date: Sat, 12 Mar 2022 01:46:28 +0900 (JST)
Subject: [ruby-changes:71366] 42e5ec9414 (master): Refactor duplicate code in rb_array_replace
https://git.ruby-lang.org/ruby.git/commit/?id=42e5ec9414 From 42e5ec941489c11a180ed75c6348419c79aefff1 Mon Sep 17 00:00:00 2001 From: Peter Zhu <peter@p...> Date: Fri, 11 Mar 2022 11:45:14 -0500 Subject: 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) https://github.com/ruby/ruby/blob/trunk/array.c#L4391 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)); -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/