Feature #5000

skip string creation of when clause

Added by Yusuke Endoh almost 3 years ago. Updated almost 3 years ago.

[ruby-dev:44068]
Status:Closed
Priority:Normal
Assignee:Yusuke Endoh
Category:-
Target version:1.9.3

Description

遠藤です。

when 節に文字列リテラルをそのまま書くことは結構あると思います。

case str
when "foo"
when "bar"
when "baz"
end

現在は when 節の比較を実行するごとに、String オブジェクトが生成
されています。しかし、この場合は毎回同じオブジェクトを返せばいい
と思います。最良のケースで、このくらい高速化します。

$ cat t.rb
5000000.times do
case nil
when "foo"
when "bar"
when "baz"
end
end

# パッチ前
$ time ./ruby.org t.rb
real 0m4.561s
user 0m4.488s
sys 0m0.004s

# パッチ後
$ time ./ruby.new t.rb
real 0m2.881s
user 0m2.828s
sys 0m0.036s

when x="foo" や when "foo#{x}" のような場合には最適化しません。
make check も通ったし、ささださんの了解も得ていますので、コミット
してやる所存です。反対があればお早めにどうぞ。

diff --git a/compile.c b/compile.c
index 01c99b6..53149ca 100644
--- a/compile.c
+++ b/compile.c
@@ -2362,7 +2362,14 @@ whenvals(rbiseqt *iseq, LINKANCHOR *condseq, NODE *vals, LABEL *l1, VALUE s
special
literals = Qfalse;
}

  • COMPILE(cond_seq, "when cond", val);
  • if (ndtype(val) == NODESTR) {
  • debugpparam("ndlit", val->nd_lit);
  • OBJFREEZE(val->ndlit);
  • ADDINSN1(condseq, ndline(val), putobject, val->ndlit);
  • }
  • else {
  • COMPILE(cond_seq, "when cond", val);
  • } ADDINSN1(condseq, ndline(val), topn, INT2FIX(1)); ADDSEND(condseq, ndline(val), ID2SYM(idEqq), INT2FIX(1)); ADDINSNL(condseq, nd_line(val), branchif, l1);

Yusuke Endoh mame@tsg.ne.jp

Associated revisions

Revision 32484
Added by Yusuke Endoh almost 3 years ago

  • compile.c (when_vals): when a string literal is written on when clause, skip string creation to make it faster. [Feature #5000]

History

#1 Updated by Yusuke Endoh almost 3 years ago

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

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


  • compile.c (when_vals): when a string literal is written on when clause, skip string creation to make it faster. [Feature #5000]

Also available in: Atom PDF