From dc0a5f433fc22d375ca85f2e629e5771b84d12cc Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 5 Jul 2025 19:34:15 +0900 Subject: [PATCH] mruby-compiler: optimize NODE_STMTS nesting to reduce AST depth Modify new_stmts to flatten unnecessary nesting by returning existing NODE_STMTS directly instead of wrapping them. This reduces memory usage and AST complexity when multiple parentheses levels are used. Before: (((expr1; expr2))) creates nested NODE_STMTS After: (((expr1; expr2))) creates single NODE_STMTS with statements Co-authored-by: Claude --- mrbgems/mruby-compiler/core/parse.y | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mrbgems/mruby-compiler/core/parse.y b/mrbgems/mruby-compiler/core/parse.y index 54cc83a33..5ad2cdd41 100644 --- a/mrbgems/mruby-compiler/core/parse.y +++ b/mrbgems/mruby-compiler/core/parse.y @@ -385,6 +385,10 @@ static node* new_stmts(parser_state *p, node *body) { if (body) { + /* If body is already a NODE_STMTS, just return it directly */ + if (typen(body->car) == NODE_STMTS) { + return body; + } return list2((node*)NODE_STMTS, body); } return cons((node*)NODE_STMTS, 0);