From 6b8078cc038e926969ec351bceda26182c04654d Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Sun, 29 Sep 2024 21:03:08 -0400 Subject: [PATCH] Don't create empty string for interpolation We don't need to create an empty string for interpolation unless it is the only element. For example: "#{hello} world" Before: 0000 putobject "" ( 1)[Li] 0002 putself 0003 opt_send_without_block 0005 dup 0006 objtostring 0008 anytostring 0009 putobject " world" 0011 concatstrings 3 0013 leave After: 0000 putself ( 1)[Li] 0001 opt_send_without_block 0003 dup 0004 objtostring 0006 anytostring 0007 putobject " world" 0009 concatstrings 2 0011 leave --- prism_compile.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/prism_compile.c b/prism_compile.c index ff487ddb23..3e4de89f22 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -643,12 +643,15 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const encoding = scope_node->encoding; } - current_string = rb_enc_str_new(NULL, 0, encoding); + if (parts_size == 1) { + current_string = rb_enc_str_new(NULL, 0, encoding); + } } - { + if (RTEST(current_string)) { VALUE operand = rb_fstring(current_string); PUSH_INSN1(ret, current_location, putobject, operand); + stack_size++; } PM_COMPILE_NOT_POPPED(part); @@ -664,7 +667,7 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const PUSH_INSN(ret, current_location, anytostring); current_string = Qnil; - stack_size += 2; + stack_size++; } } }