Bug #9159


[patch] use rb_fstring for internal strings

Added by tmm1 (Aman Karmani) over 10 years ago. Updated over 10 years ago.

Target version:
ruby -v:
ruby 2.1.0dev (2013-11-26 trunk 43852)


I added rb_fstring wrappers around internal strings generated by iseqs, regexps and eval:

In our rails app, this reduces the number of long-lived strings on the heap by 30%.

  $ ruby -rconfig/environment -e' GC.start; p ObjectSpace.count_objects[:T_STRING] '

  $ ruby -rconfig/environment -e' GC.start; p ObjectSpace.count_objects[:T_STRING] '

If this patch is acceptable, I can commit it. make test and test-all pass.

Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #8148: [patch] reduce allocations due to __FILE__ and {class,module}_evalRejectedtmm1 (Aman Karmani)03/22/2013Actions

Updated by tmm1 (Aman Karmani) over 10 years ago

One example of strings de-duplicated by this patch are iseq labels. Before:

irb(main):001:0> mm = "method_missing"
irb(main):002:0> GC.start; ObjectSpace.each_object(String).select{ |o| o == mm }.size
=> 77


irb(main):002:0> GC.start; ObjectSpace.each_object(String).select{ |o| o == mm }.size
=> 3

Updated by tmm1 (Aman Karmani) over 10 years ago

if (nd_type(node) == NODE_STR) {

  • hide_obj(node->nd_lit);
  • node->nd_lit = rb_fstring(node->nd_lit);
    ADD_INSN1(ret, nd_line(node), putobject, node->nd_lit);

Some internal strings are no longer hidden with this patch, since the underlying T_STRING object can be shared via the fstr table.
Since the object is already marked immutable (via the frozen flag), hiding it is unnecessary.

Actions #4

Updated by tmm1 (Aman Karmani) over 10 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r43866.
Aman, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

  • compile.c: Use rb_fstring() to de-duplicate string literals in code. [ruby-core:58599] [Bug #9159] [ruby-core:54405]
  • iseq.c (prepare_iseq_build): De-duplicate iseq labels and source locations.
  • re.c (rb_reg_initialize): Use rb_fstring() for regex string.
  • string.c (rb_fstring): Handle non-string and already-fstr arguments.
  • vm_eval.c (eval_string_with_cref): De-duplicate eval source filename.

Updated by normalperson (Eric Wong) over 10 years ago

Can somebody following this thread check out my patch for

Theoretically it should be better than nothing (but not ideal), but I
don't think any of my code uses string hash keys enough to matter...


Also available in: Atom PDF