ruby-changes:53887
From: tenderlove <ko1@a...>
Date: Sat, 1 Dec 2018 08:58:19 +0900 (JST)
Subject: [ruby-changes:53887] tenderlove:r66106 (trunk): Collapse putobject, putobject, newarray
tenderlove 2018-12-01 08:58:13 +0900 (Sat, 01 Dec 2018) New Revision: 66106 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66106 Log: Collapse putobject, putobject, newarray This collapses: ``` == disasm: #<ISeq:bar@b...:3 (3,0)-(5,3)> (catch: FALSE) 0000 putobject "a" ( 4)[LiCa] 0002 putobject "b" 0004 putobject "c" 0006 putobject "d" 0008 putobject "e" 0010 putobject "f" 0012 putobject "g" 0014 putobject "h" 0016 putobject "i" 0018 putobject "j" 0020 putobject "k" 0022 newarray 11 0024 leave ( 5)[Re] ``` In to this: ``` == disasm: #<ISeq:bar@b...:3 (3,0)-(5,3)> (catch: FALSE) 0000 duparray ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]( 4)[LiCa] 0002 leave ( 5)[Re] ``` Modified files: trunk/compile.c Index: compile.c =================================================================== --- compile.c (revision 66105) +++ compile.c (revision 66106) @@ -2823,6 +2823,53 @@ iseq_peephole_optimize(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/compile.c#L2823 } } + /* + * putobject "foo" + * putobject "bar" + * newarray 2 + * + * ==> + * + * duparray ["foo", "bar"] + */ + if (IS_INSN_ID(iobj, newarray)) { + int len; + + len = NUM2INT(OPERAND_AT(iobj, 0)); + + if (len > 0) { + INSN *link; + INSN *cur; + int i; + + link = iobj; + i = len; + while(i > 0) { + link = (INSN *)get_prev_insn(link); + if (!IS_INSN_ID(link, putobject)) + break; + + i--; + } + + /* All previous instructions were `putobject` */ + if (i == 0) { + VALUE list = rb_ary_new_capa(len); + iseq_add_mark_object_compile_time(iseq, list); + + while(i < len) { + cur = link; + rb_ary_push(list, OPERAND_AT(cur, 0)); + link = (INSN *)get_next_insn(link); + ELEM_REMOVE(&cur->link); + i++; + } + iobj->insn_id = BIN(duparray); + OPERAND_AT(iobj, 0) = list; + } + } + } + if (IS_INSN_ID(iobj, leave)) { remove_unreachable_chunk(iseq, iobj->link.next); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/