summaryrefslogtreecommitdiff
path: root/vm_args.c
diff options
context:
space:
mode:
authorJeremy Evans <[email protected]>2024-07-12 15:33:59 -0700
committerJeremy Evans <[email protected]>2024-07-18 22:17:21 -0700
commit6428ce80f0ef85324a4d0a72ffa4c6fa2db37cdd (patch)
treeaac9d7262c4394bb937c6e4f6cd5bdabd7301fd0 /vm_args.c
parent1cc5a64dd87a6a474ec8387083d41a3c6c1c3ca5 (diff)
Avoid array allocation for f(*r2k_ary) when def f(x)
When calling a method that does not accept a positional splat parameter with a splatted array with a ruby2_keywords flagged hash, there is no need to duplicate the splatted array. Previously, Ruby would duplicate the splatted array and potentially modify it before flattening it to the VM stack Use a similar approach as the f(*ary, **hash) optimization, flattening the splatted array to the VM stack without modifying it, and make any modifications needed to the VM stack.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11161
Diffstat (limited to 'vm_args.c')
-rw-r--r--vm_args.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/vm_args.c b/vm_args.c
index abd9cf75f6..84ffd2e983 100644
--- a/vm_args.c
+++ b/vm_args.c
@@ -734,6 +734,32 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co
}
given_argc--;
}
+ else if (!ISEQ_BODY(iseq)->param.flags.has_rest) {
+ // Avoid duping rest when not necessary
+ // Copy rest elements and converted keyword hash directly to VM stack
+ const VALUE *argv = RARRAY_CONST_PTR(args->rest);
+ int j, i=args->argc, rest_len = RARRAY_LENINT(args->rest)-1;
+ args->argc += rest_len;
+ if (rest_len) {
+ CHECK_VM_STACK_OVERFLOW(ec->cfp, rest_len+1);
+ for (i, j=0; rest_len > 0; rest_len--, i++, j++) {
+ locals[i] = argv[j];
+ }
+ }
+ args->rest = Qfalse;
+ ci_flag &= ~VM_CALL_ARGS_SPLAT;
+
+ if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
+ given_argc--;
+ keyword_hash = converted_keyword_hash;
+ }
+ else {
+ args->argc += 1;
+ locals[i] = converted_keyword_hash;
+ keyword_hash = Qnil;
+ kw_flag = 0;
+ }
+ }
else {
if (rest_last != converted_keyword_hash) {
rest_last = converted_keyword_hash;