Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created October 21, 2018 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/010d4336dbb4009fbca9a08f223291f1 to your computer and use it in GitHub Desktop.
Save tenderlove/010d4336dbb4009fbca9a08f223291f1 to your computer and use it in GitHub Desktop.
From 8f74c91274a7e1072685aded111e89d55ced0f96 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <aaron.patterson@gmail.com>
Date: Sun, 21 Oct 2018 16:12:16 -0700
Subject: [PATCH] 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]
```
---
compile.c | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/compile.c b/compile.c
index 272b9ddacc..ed21cbb1d1 100644
--- a/compile.c
+++ b/compile.c
@@ -2796,6 +2796,44 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal
}
}
+ 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);
}
--
2.17.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment