Bug #9079

Interpolated Symbol creates unnecessary intermediate String on the heap

Added by Yehuda Katz 6 months ago. Updated 4 months ago.

[ruby-core:58159]
Status:Open
Priority:Normal
Assignee:Koichi Sasada
Category:-
Target version:next minor
ruby -v:ruby 2.0.0p343 (2013-10-31) [x86_64-linux] Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN

Description

This case:

x = "world"
y = :"hello#{world}!"

allocates one TSTRING according to ObjectSpace.countobjects. Since all of the concatenation happens as part of the symbol generation, it shouldn't be necessary to create a Ruby string.

History

#1 Updated by Charlie Somerville 6 months ago

To shed a bit of light on why this happens, it turns out :"" syntax is just sugar for "".intern:

~ trunk λ ruby --dump=insns -e ':"hello #{123}"'
== disasm: <RubyVM::InstructionSequence:<main>@-e>======================
0000 trace            1                                               (   1)
0002 putobject        "hello "
0004 putobject        123
0006 tostring
0007 concatstrings    2
0009 opt_send_simple  <callinfo!mid:intern, argc:0, ARGS_SKIP>
0011 leave

#2 Updated by Koichi Sasada 4 months ago

  • Target version set to next minor

Does it has huge impact?
It is easy to add an instruction (replace concatstrings to concatstringsandmakeitsymbol) or add a method like that.
However, it has a bit problem about encoding.
Current implementation is easy and safe, because we can catch up encoding related change to String.
I want to remain this issue, if this does not have huge impact for performance.

Also available in: Atom PDF