Skip to content

Commit 6b8078c

Browse files
committed
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 <calldata!mid:hello, argc:0, FCALL|VCALL|ARGS_SIMPLE> 0005 dup 0006 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE> 0008 anytostring 0009 putobject " world" 0011 concatstrings 3 0013 leave After: 0000 putself ( 1)[Li] 0001 opt_send_without_block <calldata!mid:hello, argc:0, FCALL|VCALL|ARGS_SIMPLE> 0003 dup 0004 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE> 0006 anytostring 0007 putobject " world" 0009 concatstrings 2 0011 leave
1 parent 6370674 commit 6b8078c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

prism_compile.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,15 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const
643643
encoding = scope_node->encoding;
644644
}
645645

646-
current_string = rb_enc_str_new(NULL, 0, encoding);
646+
if (parts_size == 1) {
647+
current_string = rb_enc_str_new(NULL, 0, encoding);
648+
}
647649
}
648650

649-
{
651+
if (RTEST(current_string)) {
650652
VALUE operand = rb_fstring(current_string);
651653
PUSH_INSN1(ret, current_location, putobject, operand);
654+
stack_size++;
652655
}
653656

654657
PM_COMPILE_NOT_POPPED(part);
@@ -664,7 +667,7 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const
664667
PUSH_INSN(ret, current_location, anytostring);
665668

666669
current_string = Qnil;
667-
stack_size += 2;
670+
stack_size++;
668671
}
669672
}
670673
}

0 commit comments

Comments
 (0)