Skip to content

Commit bbcb569

Browse files
committed
Use an arena for building the Prism AST
1 parent 3cffc44 commit bbcb569

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

templates/ext/prism/api_node.c.erb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#line <%= __LINE__ + 1 %> "prism/templates/ext/prism/<%= File.basename(__FILE__) %>"
22
#include "prism/extension.h"
33
#include "prism/internal/allocator.h"
4+
#include "prism/internal/arena.h"
45

56
#include <assert.h>
67

@@ -101,8 +102,8 @@ typedef struct pm_node_stack_node {
101102
} pm_node_stack_node_t;
102103

103104
static void
104-
pm_node_stack_push(pm_node_stack_node_t **stack, const pm_node_t *visit) {
105-
pm_node_stack_node_t *node = xmalloc(sizeof(pm_node_stack_node_t));
105+
pm_node_stack_push(pm_arena_t *arena, pm_node_stack_node_t **stack, const pm_node_t *visit) {
106+
pm_node_stack_node_t *node = (pm_node_stack_node_t *) pm_arena_alloc(arena, sizeof(pm_node_stack_node_t), PRISM_ALIGNOF(pm_node_stack_node_t));
106107
node->prev = *stack;
107108
node->visit = visit;
108109
node->visited = false;
@@ -115,7 +116,6 @@ pm_node_stack_pop(pm_node_stack_node_t **stack) {
115116
const pm_node_t *visit = current->visit;
116117

117118
*stack = current->prev;
118-
xfree_sized(current, sizeof(pm_node_stack_node_t));
119119

120120
return visit;
121121
}
@@ -147,8 +147,9 @@ pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encodi
147147
pm_ast_constants_each_data_t constants_data = { .constants = constants, .encoding = encoding };
148148
pm_parser_constants_each(parser, pm_ast_constants_each, &constants_data);
149149

150+
pm_arena_t *node_arena = pm_arena_new();
150151
pm_node_stack_node_t *node_stack = NULL;
151-
pm_node_stack_push(&node_stack, node);
152+
pm_node_stack_push(node_arena, &node_stack, node);
152153
VALUE value_stack = rb_ary_new();
153154

154155
while (node_stack != NULL) {
@@ -171,10 +172,10 @@ pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encodi
171172
<%- node.fields.each do |field| -%>
172173
<%- case field -%>
173174
<%- when Prism::Template::NodeField, Prism::Template::OptionalNodeField -%>
174-
pm_node_stack_push(&node_stack, (pm_node_t *) cast-><%= field.name %>);
175+
pm_node_stack_push(node_arena, &node_stack, (pm_node_t *) cast-><%= field.name %>);
175176
<%- when Prism::Template::NodeListField -%>
176177
for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
177-
pm_node_stack_push(&node_stack, (pm_node_t *) cast-><%= field.name %>.nodes[index]);
178+
pm_node_stack_push(node_arena, &node_stack, (pm_node_t *) cast-><%= field.name %>.nodes[index]);
178179
}
179180
<%- end -%>
180181
<%- end -%>
@@ -276,6 +277,7 @@ pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encodi
276277
}
277278
}
278279

280+
pm_arena_free(node_arena);
279281
return rb_ary_pop(value_stack);
280282
}
281283

0 commit comments

Comments
 (0)