summaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorJeremy Evans <[email protected]>2024-01-30 10:31:27 -0800
committerJeremy Evans <[email protected]>2024-02-20 10:47:44 -0800
commit77c1233f79a0f96a081b70da533fbbde4f3037fa (patch)
tree0f0cc52dac2f53255656dec246e9d6bfaf310762 /compile.c
parent2e2e3d89af7d16e466a12a42ef6cd6554e700f50 (diff)
Add pushtoarraykwsplat instruction to avoid unnecessary array allocation
This is designed to replace the newarraykwsplat instruction, which is no longer used in the parse.y compiler after this commit. This avoids an unnecessary array allocation in the case where ARGSCAT is followed by LIST with keyword: ```ruby a = [] kw = {} [*a, 1, **kw] ``` Previous Instructions: ``` 0000 newarray 0 ( 1)[Li] 0002 setlocal_WC_0 a@0 0004 newhash 0 ( 2)[Li] 0006 setlocal_WC_0 kw@1 0008 getlocal_WC_0 a@0 ( 3)[Li] 0010 splatarray true 0012 putobject_INT2FIX_1_ 0013 putspecialobject 1 0015 newhash 0 0017 getlocal_WC_0 kw@1 0019 opt_send_without_block <calldata!mid:core#hash_merge_kwd, argc:2, ARGS_SIMPLE> 0021 newarraykwsplat 2 0023 concattoarray 0024 leave ``` New Instructions: ``` 0000 newarray 0 ( 1)[Li] 0002 setlocal_WC_0 a@0 0004 newhash 0 ( 2)[Li] 0006 setlocal_WC_0 kw@1 0008 getlocal_WC_0 a@0 ( 3)[Li] 0010 splatarray true 0012 putobject_INT2FIX_1_ 0013 pushtoarray 1 0015 putspecialobject 1 0017 newhash 0 0019 getlocal_WC_0 kw@1 0021 opt_send_without_block <calldata!mid:core#hash_merge_kwd, argc:2, ARGS_SIMPLE> 0023 pushtoarraykwsplat 0024 leave ``` pushtoarraykwsplat is designed to be simpler than newarraykwsplat. It does not take a variable number of arguments from the stack, it pops the top of the stack, and appends it to the second from the top, unless the top of the stack is an empty hash. During this work, I found the ARGSPUSH followed by HASH with keyword did not compile correctly, as it pushed the generated hash to the array even if the hash was empty. This fixes the behavior, to use pushtoarraykwsplat instead of pushtoarray in that case: ```ruby a = [] kw = {} [*a, **kw] [{}] # Before [] # After ``` This does not remove the newarraykwsplat instruction, as it is still referenced in the prism compiler (which should be updated similar to this), YJIT (only in the bindings, it does not appear to be implemented), and RJIT (in a couple comments). After those are updated, the newarraykwsplat instruction should be removed.
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c34
1 files changed, 23 insertions, 11 deletions
diff --git a/compile.c b/compile.c
index 7694b34bf8..b2ff9c3513 100644
--- a/compile.c
+++ b/compile.c
@@ -4813,12 +4813,12 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int pop
* putobject [1,2,3,...,100] (<- hidden array); concattoarray;
* push z; pushtoarray 1;
*
- * - If the last element is a keyword, newarraykwsplat should be emitted
- * to check and remove empty keyword arguments hash from array.
+ * - If the last element is a keyword, pushtoarraykwsplat should be emitted
+ * to only push it onto the array if it is not empty
* (Note: a keyword is NODE_HASH which is not static_literal_node_p.)
*
* [1,2,3,**kw] =>
- * putobject 1; putobject 2; putobject 3; push kw; newarraykwsplat
+ * putobject 1; putobject 2; putobject 3; newarray 3; ...; pushtoarraykwsplat kw
*/
const int max_stack_len = 0x100;
@@ -4873,15 +4873,22 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int pop
EXPECT_NODE("compile_array", node, NODE_LIST, -1);
}
- NO_CHECK(COMPILE_(ret, "array element", RNODE_LIST(node)->nd_head, 0));
- stack_len++;
-
if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
- /* Reached the end, and the last element is a keyword */
- ADD_INSN1(ret, line_node, newarraykwsplat, INT2FIX(stack_len));
- if (!first_chunk) ADD_INSN(ret, line_node, concattoarray);
+ /* Create array or push existing non-keyword elements onto array */
+ if (stack_len == 0 && first_chunk) {
+ ADD_INSN1(ret, line_node, newarray, INT2FIX(0));
+ }
+ else {
+ FLUSH_CHUNK;
+ }
+ NO_CHECK(COMPILE_(ret, "array element", RNODE_LIST(node)->nd_head, 0));
+ ADD_INSN(ret, line_node, pushtoarraykwsplat);
return 1;
}
+ else {
+ NO_CHECK(COMPILE_(ret, "array element", RNODE_LIST(node)->nd_head, 0));
+ stack_len++;
+ }
/* If there are many pushed elements, flush them to avoid stack overflow */
if (stack_len >= max_stack_len) FLUSH_CHUNK;
@@ -10426,13 +10433,18 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const no
else {
CHECK(COMPILE(ret, "argspush head", RNODE_ARGSPUSH(node)->nd_head));
const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
- if (static_literal_node_p(body_node, iseq, false)) {
+ if (keyword_node_p(body_node)) {
+ CHECK(COMPILE_(ret, "array element", body_node, FALSE));
+ ADD_INSN(ret, node, pushtoarraykwsplat);
+ }
+ else if (static_literal_node_p(body_node, iseq, false)) {
ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
+ ADD_INSN1(ret, node, pushtoarray, INT2FIX(1));
}
else {
CHECK(COMPILE_(ret, "array element", body_node, FALSE));
+ ADD_INSN1(ret, node, pushtoarray, INT2FIX(1));
}
- ADD_INSN1(ret, node, pushtoarray, INT2FIX(1));
}
break;
}