[Feature #20589] Resize arrays in rb_ary_freeze
and use it for freezing arrays
#11030
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[Feature #20589]
While working on a separate issue we found that in some cases
ary_heap_realloc
was being called on frozen arrays. To fix this, this change does the following:rb_ary_freeze
to assert the type is an array, return if already frozen, and shrink the capacity if it is not embedded, shared or a shared root.rb_obj_freeze
withrb_ary_freeze
when the object is always an array.ary_heap_realloc
, ensure the new capa is set withARY_SET_CAPA
. Previously the change in capa was not set. 4) Adds an assertion toary_heap_realloc
that the array is not frozen.Some of this work was originally done in
#2640, referencing this issue https://bugs.ruby-lang.org/issues/16291. There didn't appear to be any objections to this PR, it appears to have simply lost traction.
The original PR made changes to arrays and strings at the same time, this PR only does arrays. Also it was old enough that rather than revive that branch I've made a new one. I added Lourens as co-author in addtion to Aaron who helped me with this patch.
The original PR made this change for performance reasons, and while that's still true for this PR, the goal of this PR is to avoid calling
ary_heap_realloc
on frozen arrays. The capacity should be shrunk before the array is frozen, not after.