summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2024-01-18 10:36:49 -0500
committerPeter Zhu <peter@peterzhu.ca>2024-01-18 13:40:14 -0500
commitd8ac96efc57be460a0aad5d6ae033639439506fb (patch)
treeeff2bf059a628de82b82d46ac253be4bf28905ef
parentd3b07b984545ce156e02e9f71404b652c6cb5284 (diff)
[PRISM] Fix memory leak in case nodes
The temporary array conditions_labels is heap allocated and never freed. We can use alloca instead to stack allocate it so that we don't need to manually free it. For example: code = "case; #{100.times.map { "when #{it}; " }.join}; end" 10.times do 10_000.times do RubyVM::InstructionSequence.compile_prism(code) end puts `ps -o rss= -p #{$$}` end Before: 21376 30304 38800 47184 55456 64192 72288 80400 89040 97104 After: 13088 13632 13760 14016 14688 14992 15120 15232 15744 15744
-rw-r--r--prism_compile.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/prism_compile.c b/prism_compile.c
index e87c240638..9dd39e9b94 100644
--- a/prism_compile.c
+++ b/prism_compile.c
@@ -3502,7 +3502,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
pm_node_list_t conditions = case_node->conditions;
- LABEL **conditions_labels = (LABEL **)ALLOC_N(VALUE, conditions.size + 1);
+ LABEL **conditions_labels = (LABEL **)ALLOCA_N(VALUE, conditions.size + 1);
LABEL *label;
for (size_t i = 0; i < conditions.size; i++) {