From 40a2afd08fe1b921f1052b29031abfa1869e0557 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 3 Nov 2023 16:56:58 -0700 Subject: Eliminate array allocation for f(1, *a) 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. --- compile.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'compile.c') diff --git a/compile.c b/compile.c index 31783feeaf..fe0bd5e6d2 100644 --- a/compile.c +++ b/compile.c @@ -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; } -- cgit v1.2.3