Add pushtoarray VM instruction

This instruction is similar to concattoarray, but it takes the
number of arguments to push to the array, removes that number
of arguments from the stack, and adds them to the array now at
the top of the stack.

This allows `f(*a, 1)` to allocate only a single array on the
caller side (which can be reused on the callee side in the case of
`def f(*a)`). Prior to this commit, `f(*a, 1)` would generate
3 arrays:

* a dupped by splatarray true
* 1 wrapped in array by newarray
* a dupped again by concatarray

Instructions Before for `a = []; f(*a, 1)`:

```
0000 newarray                               0                         (   1)[Li]
0002 setlocal_WC_0                          a@0
0004 putself
0005 getlocal_WC_0                          a@0
0007 splatarray                             true
0009 putobject_INT2FIX_1_
0010 newarray                               1
0012 concatarray
0013 opt_send_without_block                 <calldata!mid:f, argc:1, ARGS_SPLAT|FCALL>
0015 leave
```

Instructions After for `a = []; f(*a, 1)`:

```
0000 newarray                               0                         (   1)[Li]
0002 setlocal_WC_0                          a@0
0004 putself
0005 getlocal_WC_0                          a@0
0007 splatarray                             true
0009 putobject_INT2FIX_1_
0010 pushtoarray                            1
0012 opt_send_without_block                 <calldata!mid:f, argc:1, ARGS_SPLAT|ARGS_SPLAT_MUT|FCALL>
0014 leave
```

With these changes, method calls to Ruby methods should
implicitly allocate at most one array.

Ignore typeprof bundled gem failure due to unrecognized instruction.
This commit is contained in:
Jeremy Evans 2023-11-28 12:14:45 -08:00
parent 6e06d0d180
commit b8516d6d01
5 changed files with 197 additions and 181 deletions

View file

@ -532,6 +532,18 @@ concattoarray
ary = vm_concat_to_array(ary1, ary2);
}
/* push given number of objects to array directly before. */
DEFINE_INSN
pushtoarray
(rb_num_t num)
(...)
(VALUE val)
// attr rb_snum_t sp_inc = -(rb_snum_t)num;
{
const VALUE *objp = STACK_ADDR_FROM_TOP(num);
val = rb_ary_cat(*(objp-1), objp, num);
}
/* call to_a on array ary to splat */
DEFINE_INSN
splatarray