Skip to content

Commit

Permalink
Eliminate array allocation for f(1, *a)
Browse files Browse the repository at this point in the history
Due to how the compiler works, while f(*a) does not allocate an
array f(1, *a) does.  This is possible to fix in the compiler, but
the change is much more complex.  This attempts to fix the issue
in a simpler way using the peephole optimizer.

Eliminating this array allocation is safe, since just as in the
f(*a) case, nothing else on the caller side can modify the array.
  • Loading branch information
jeremyevans committed Dec 7, 2023
1 parent 41b299d commit 40a2afd
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions compile.c
Expand Up @@ -3836,6 +3836,27 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal
}
}

if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) == Qtrue) {
LINK_ELEMENT *niobj = &iobj->link;

/*
* Eliminate array allocation for f(1, *a)
*
* splatarray true
* send ARGS_SPLAT and not KW_SPLAT|ARGS_BLOCKARG
* =>
* splatarray false
* send
*/
if (IS_NEXT_INSN_ID(niobj, send)) {
niobj = niobj->next;
unsigned int flag = vm_ci_flag((const struct rb_callinfo *)OPERAND_AT(niobj, 0));
if ((flag & VM_CALL_ARGS_SPLAT) && !(flag & (VM_CALL_KW_SPLAT|VM_CALL_ARGS_BLOCKARG))) {
OPERAND_AT(iobj, 0) = Qfalse;
}
}
}

return COMPILE_OK;
}

Expand Down

0 comments on commit 40a2afd

Please sign in to comment.