summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorKunshan Wang <wks1986@gmail.com>2023-08-02 17:53:01 +0800
committerPeter Zhu <peter@peterzhu.ca>2023-08-03 08:34:24 -0400
commitaeff31168ad59d29c0b5a068db0009d46558f3ab (patch)
treee54465cf969b6fcceb6d0221f80da22209dc7a26 /array.c
parent5336b2f0db928f2d89d9aedbec5337e58218ceed (diff)
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/8164
Diffstat (limited to 'array.c')
-rw-r--r--array.c9
1 files 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));