Skip to content

Commit

Permalink
Merge pull request #2727 from ruby/gc-able-symbols
Browse files Browse the repository at this point in the history
Use GC-able symbols for AST
  • Loading branch information
kddnewton authored Apr 23, 2024
2 parents 7463f66 + f531e47 commit 9dded6f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.2"
ruby-version: "3.3"
bundler-cache: true
- name: Install doxygen and dependencies
run: |
Expand Down
18 changes: 8 additions & 10 deletions templates/ext/prism/api_node.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,21 @@ pm_node_stack_pop(pm_node_stack_node_t **stack) {

VALUE
pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encoding, VALUE source) {
ID *constants = xcalloc(parser->constant_pool.size, sizeof(ID));
VALUE constants = rb_ary_new_capa(parser->constant_pool.size);

for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
pm_constant_t *constant = &parser->constant_pool.constants[index];
int state = 0;

VALUE string = rb_enc_str_new((const char *) constant->start, constant->length, encoding);
ID value = rb_protect(rb_intern_str, string, &state);
VALUE value = rb_protect(rb_str_intern, string, &state);

if (state != 0) {
value = rb_intern_const("?");
value = ID2SYM(rb_intern_const("?"));
rb_set_errinfo(Qnil);
}

constants[index] = value;
rb_ary_push(constants, value);
}

pm_node_stack_node_t *node_stack = NULL;
Expand Down Expand Up @@ -197,15 +197,15 @@ pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encodi
<%- when Prism::Template::ConstantField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
assert(cast-><%= field.name %> != 0);
argv[<%= index %>] = rb_id2sym(constants[cast-><%= field.name %> - 1]);
argv[<%= index %>] = RARRAY_AREF(constants, cast-><%= field.name %> - 1);
<%- when Prism::Template::OptionalConstantField -%>
argv[<%= index %>] = cast-><%= field.name %> == 0 ? Qnil : rb_id2sym(constants[cast-><%= field.name %> - 1]);
argv[<%= index %>] = cast-><%= field.name %> == 0 ? Qnil : RARRAY_AREF(constants, cast-><%= field.name %> - 1);
<%- when Prism::Template::ConstantListField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
argv[<%= index %>] = rb_ary_new_capa(cast-><%= field.name %>.size);
for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
assert(cast-><%= field.name %>.ids[index] != 0);
rb_ary_push(argv[<%= index %>], rb_id2sym(constants[cast-><%= field.name %>.ids[index] - 1]));
rb_ary_push(argv[<%= index %>], RARRAY_AREF(constants, cast-><%= field.name %>.ids[index] - 1));
}
<%- when Prism::Template::LocationField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
Expand Down Expand Up @@ -246,9 +246,7 @@ pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encodi
}
}

VALUE result = rb_ary_pop(value_stack);
xfree(constants);
return result;
return rb_ary_pop(value_stack);
}

void
Expand Down

0 comments on commit 9dded6f

Please sign in to comment.