From e84f7bb3b0f213bddf1026d5f48cf07506a4ec26 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 19 Dec 2025 22:26:36 +0900 Subject: [PATCH] mruby-compiler: remove unnecessary MOVE/APOST in simple masgn For simple parallel assignment like `a,b = 1,2` without rest or post variables, skip generating APOST instruction and unnecessary MOVE. The values are already extracted via AREF. Before (27 bytes): LOADI_1 R3 LOADI_2 R4 ARRAY R3 2 AREF R1 R3 0 AREF R2 R3 1 MOVE R4 R3 APOST R4 2 0 MOVE R4 R3 RETURN R3 After (18 bytes): LOADI_1 R3 LOADI_2 R4 ARRAY R3 2 AREF R1 R3 0 AREF R2 R3 1 RETURN R3 Co-authored-by: Claude --- mrbgems/mruby-compiler/core/codegen.c | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index 527e92de9..de52f87af 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -5086,24 +5086,24 @@ codegen_masgn(codegen_scope *s, node *varnode, node *rhs, int sp, int val) } } - gen_move(s, cursp(), rhs_reg, val); - push_n(post+1); - pop_n(post+1); - genop_3(s, OP_APOST, cursp(), n, post); - int nn = 1; - if (masgn_n->rest && (intptr_t)masgn_n->rest != -1) { /* rest */ - gen_assignment(s, masgn_n->rest, NULL, cursp(), NOVAL); - } - if (masgn_n->post) { - node *post_part = masgn_n->post; - while (post_part) { - gen_assignment(s, post_part->car, NULL, cursp()+nn, NOVAL); - post_part = post_part->cdr; - nn++; + /* Only generate APOST if there's rest or post variables */ + if ((masgn_n->rest && (intptr_t)masgn_n->rest != -1) || masgn_n->post) { + gen_move(s, cursp(), rhs_reg, val); + push_n(post+1); + pop_n(post+1); + genop_3(s, OP_APOST, cursp(), n, post); + int nn = 1; + if (masgn_n->rest && (intptr_t)masgn_n->rest != -1) { /* rest */ + gen_assignment(s, masgn_n->rest, NULL, cursp(), NOVAL); + } + if (masgn_n->post) { + node *post_part = masgn_n->post; + while (post_part) { + gen_assignment(s, post_part->car, NULL, cursp()+nn, NOVAL); + post_part = post_part->cdr; + nn++; + } } - } - if (val) { - gen_move(s, cursp(), rhs_reg, 0); } if (!val && t) {