diff options
author | Jeremy Evans <[email protected]> | 2023-11-03 16:56:58 -0700 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2023-12-07 11:27:55 -0800 |
commit | 40a2afd08fe1b921f1052b29031abfa1869e0557 (patch) | |
tree | 6a96a06873a3120448b594c183685f898ba314bf /compile.c | |
parent | 41b299d6391261c11025e88b54743bb903606fb3 (diff) |
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.
Diffstat (limited to 'compile.c')
-rw-r--r-- | compile.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -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; } |