8000 Collapse putobject, putobject, newarray · github/ruby@cbdf5a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbdf5a1

Browse files
committed
Collapse putobject, putobject, newarray
This collapses: ``` == disasm: #<ISeq:bar@bench.rb: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@bench.rb: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] ``` git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66106 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 8f23f69 commit cbdf5a1

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

compile.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,53 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal
28232823
}
28242824
}
28252825

2826+
/*
2827+
* putobject "foo"
2828+
* putobject "bar"
2829+
* newarray 2
2830+
*
2831+
* ==>
2832+
*
2833+
* duparray ["foo", "bar"]
2834+
*/
2835+
if (IS_INSN_ID(iobj, newarray)) {
2836+
int len;
2837+
2838+
len = NUM2INT(OPERAND_AT(iobj, 0));
2839+
2840+
if (len > 0) {
2841+
INSN *link;
2842+
INSN *cur;
2843+
int i;
2844+
2845+
link = iobj;
2846+
i = len;
2847+
while(i > 0) {
2848+
link = (INSN *)get_prev_insn(link);
2849+
if (!IS_INSN_ID(link, putobject))
2850+
break;
2851+
2852+
i--;
2853+
}
2854+
2855+
/* All previous instructions were `putobject` */
2856+
if (i == 0) {
2857+
VALUE list = rb_ary_new_capa(len);
2858+
iseq_add_mark_object_compile_time(iseq, list);
2859+
2860+
while(i < len) {
2861+
cur = link;
2862+
rb_ary_push(list, OPERAND_AT(cur, 0));
2863+
link = (INSN *)get_next_insn(link);
2864+
ELEM_REMOVE(&cur->link);
2865+
i++;
2866+
}
2867+
iobj->insn_id = BIN(duparray);
2868+
OPERAND_AT(iobj, 0) = list;
2869+
}
2870+
}
2871+
}
2872+
28262873
if (IS_INSN_ID(iobj, leave)) {
28272874
remove_unreachable_chunk(iseq, iobj->link.next);
28282875
}

0 commit comments

Comments
 (0)
0