summaryrefslogtreecommitdiff
path: root/ast.c
diff options
context:
space:
mode:
authoreileencodes <[email protected]>2024-06-18 14:52:18 -0400
committerAaron Patterson <[email protected]>2024-07-02 10:34:23 -0700
commitd25b74b32cbce4fcaed503f124fa8e7d721f18bf (patch)
treef83f20a0a5b0f55aa6e8bc7e9d471114f45cce9d /ast.c
parentcee62c6738c42ce774e96e180cf2d46afb8e9cbe (diff)
Resize arrays in `rb_ary_freeze` and use it for freezing arrays
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 https://github.com/ruby/ruby/pull/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]>
Diffstat (limited to 'ast.c')
-rw-r--r--ast.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/ast.c b/ast.c
index 12b3996f94..cc4bca1a1a 100644
--- a/ast.c
+++ b/ast.c
@@ -769,7 +769,7 @@ ast_node_all_tokens(rb_execution_context_t *ec, VALUE self)
token = rb_ary_new_from_args(4, INT2FIX(parser_token->id), ID2SYM(rb_intern(parser_token->type_name)), str, loc);
rb_ary_push(all_tokens, token);
}
- rb_obj_freeze(all_tokens);
+ rb_ary_freeze(all_tokens);
return all_tokens;
}