From aeff31168ad59d29c0b5a068db0009d46558f3ab Mon Sep 17 00:00:00 2001 From: Kunshan Wang Date: Wed, 2 Aug 2023 17:53:01 +0800 Subject: [PATCH] No computing embed_capa_max in ary_make_partial ary_make_partial now uses the actual embed_capa of an allocated heap array to guide whether make an embedded copy or a slice view. --- array.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/array.c b/array.c index d5e9d62c37..b4190702d2 100644 --- a/array.c +++ b/array.c @@ -1194,10 +1194,10 @@ ary_make_partial(VALUE ary, VALUE klass, long offset, long len) assert(len >= 0); assert(offset+len <= RARRAY_LEN(ary)); - const size_t rarray_embed_capa_max = (sizeof(struct RArray) - offsetof(struct RArray, as.ary)) / sizeof(VALUE); - - if ((size_t)len <= rarray_embed_capa_max && ary_embeddable_p(len)) { - VALUE result = ary_alloc_embed(klass, len); + VALUE result = ary_alloc_heap(klass); + size_t embed_capa = ary_embed_capa(result); + if ((size_t)len <= embed_capa) { + FL_SET_EMBED(result); ary_memcpy(result, 0, len, RARRAY_CONST_PTR(ary) + offset); ARY_SET_EMBED_LEN(result, len); return result; @@ -1205,7 +1205,6 @@ ary_make_partial(VALUE ary, VALUE klass, long offset, long len) else { VALUE shared = ary_make_shared(ary); - VALUE result = ary_alloc_heap(klass); assert(!ARY_EMBED_P(result)); ARY_SET_PTR(result, RARRAY_CONST_PTR(ary));