Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Feature #20589] Resize arrays in rb_ary_freeze and use it for freezing arrays #11030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2024

Conversation

eileencodes
Copy link
Member

@eileencodes eileencodes commented Jun 20, 2024

[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:

  1. Updates 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.
  2. Replaces rb_obj_freeze with rb_ary_freeze when the object is always an array.
  3. In ary_heap_realloc, ensure the new capa is set with ARY_SET_CAPA. Previously the change in capa was not set. 4) Adds an assertion to ary_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.

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:

1) Updates `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.
2) Replaces `rb_obj_freeze` with `rb_ary_freeze` when the object is
always an array.
3) In `ary_heap_realloc`, ensure the new capa is set with
`ARY_SET_CAPA`. Previously the change in capa was not set.
4) Adds an assertion to `ary_heap_realloc` that the array is not frozen.

Some of this work was originally done in
ruby#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.

Co-authored-by: Aaron Patterson <[email protected]>
Co-Authored-By: methodmissing <[email protected]>
@eileencodes eileencodes changed the title Resize arrays in rb_ary_freeze and use it for freezing arrays [Feature #20589] Resize arrays in rb_ary_freeze and use it for freezing arrays Jun 20, 2024
Copy link

launchable-app bot commented Jun 20, 2024

All Tests passed!

✖️no tests failed ✔️32333 tests passed(2 flakes)

@tenderlove tenderlove merged commit d25b74b into ruby:master Jul 2, 2024
107 checks passed
@tenderlove tenderlove deleted the use-rb_ary_freeze-for-arrays branch July 2, 2024 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants