Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary array allocation for ARGSCAT with LIST body #9752

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4786,7 +4786,7 @@ static_literal_value(const NODE *node, rb_iseq_t *iseq)
}

static int
compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int popped)
compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int popped, bool first_chunk)
{
const NODE *line_node = node;

Expand Down Expand Up @@ -4846,14 +4846,14 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int pop
const int max_stack_len = 0x100;
const int min_tmp_ary_len = 0x40;
int stack_len = 0;
int first_chunk = 1;

/* Either create a new array, or push to the existing array */
#define FLUSH_CHUNK \
if (stack_len) { \
if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
first_chunk = stack_len = 0; \
first_chunk = FALSE; \
stack_len = 0; \
}

while (node) {
Expand All @@ -4879,7 +4879,7 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int pop
FLUSH_CHUNK;
if (first_chunk) {
ADD_INSN1(ret, line_node, duparray, ary);
first_chunk = 0;
first_chunk = FALSE;
}
else {
ADD_INSN1(ret, line_node, putobject, ary);
Expand Down Expand Up @@ -10174,7 +10174,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const no
CHECK(compile_super(iseq, ret, node, popped, type));
break;
case NODE_LIST:{
CHECK(compile_array(iseq, ret, node, popped) >= 0);
CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
break;
}
case NODE_ZLIST:{
Expand Down Expand Up @@ -10427,8 +10427,14 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const no
}
else {
CHECK(COMPILE(ret, "argscat head", RNODE_ARGSCAT(node)->nd_head));
CHECK(COMPILE(ret, "argscat body", RNODE_ARGSCAT(node)->nd_body));
ADD_INSN(ret, node, concattoarray);
const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
if (nd_type_p(body_node, NODE_LIST)) {
CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
}
else {
CHECK(COMPILE(ret, "argscat body", body_node));
ADD_INSN(ret, node, concattoarray);
}
}
break;
}
Expand Down