Issue #15010 has been updated by chopraanmol1 (Anmol Chopra).


chopraanmol1 (Anmol Chopra) wrote:
> I'm also thinking of an alternate solution which will avoid passing the skip_dup_flag variable around, If we can ensure that args->rest is not used/assigned until args_copy is called. To do this when VM_CALL_ARGS_SPLAT flag is on instead of assigning args->rest we could expand the splat arg to locals / args->argv.
> 

Implementation: https://github.com/ruby/ruby/compare/trunk...chopraanmol1:improve_rest_parameters_setup_v2

Benchmark result

~~~

                               trunk        patch 1       patch 2
benchmark_method               0.196346     0.197841      0.196466
rest_method                    0.788287     0.539768      0.535512
lead_method                    0.792892     0.547752      0.533818
post_method                    1.133035     0.636972      0.540609
lead_post_method               0.867869     0.709440      0.370182
benchmark_method *args         0.227389     0.230066      0.227671
rest_method *args              0.826490     0.559881      0.563779
lead_method *args              0.821036     0.602590      0.565583
post_method *args              1.157621     0.649459      0.570189
lead_post_method *args         1.064632     0.687248      0.387054
rest_method *long_args         0.985696     0.766369      0.779729
lead_method *long_args         0.997824     0.870107      0.794615
post_method *long_args         1.703731     0.863923      0.813282
lead_post_method *long_args    1.707543     0.989116      0.802757
rest_with_named_parameter      1.862414     1.293406      1.255951
bench proc                     0.275176     0.263893      0.260555
lead_proc underflow_args       1.149043     0.801893      0.363017
opt_post_proc overflow_args    1.025754     0.717966      0.312920

~~~



chopraanmol1 (Anmol Chopra) wrote:
> In this solution, args_setup_post_parameters can be further modified to use args->argv instead of args->rest which makes zero allocation for the following example:

args->argv and locals are pointing to same address so it is not feasible.


Note: This second patch is not the final implementation, there are few more changes. In second patch args_check_block_arg0 function can still assign args->rest, I'll look into this later (only if the second patch seems more acceptable over first) if it can be completely avoided. In case it can be avoided most of the method handling args->rest can be cleaned after that, which will also ensure that args->rest_index is never modified. As a result, we could even avoid calling rb_ary_behead.




----------------------------------------
Feature #15010: Reduce allocation for rest parameters
https://bugs.ruby-lang.org/issues/15010#change-73646

* Author: chopraanmol1 (Anmol Chopra)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Currently multiple arrays are allocated while making a call to method with rest parameter.

E.g.
~~~
def rest_method(*args) #-> This will create 2 arrays
end

def post_method(*args,last) #-> This will create 3 arrays
end
~~~

Applying following set of changes will reduce creation of array to 1

https://github.com/ruby/ruby/pull/1935

Benchmark Result:

trunk
~~~
                                  user     system      total        real
benchmark_method              0.340000   0.000000   0.340000 (  0.337035)
rest_method                   0.964000   0.000000   0.964000 (  0.964660)
lead_method                   0.976000   0.000000   0.976000 (  0.976011)
post_method                   2.424000   0.000000   2.424000 (  2.421732)
lead_post_method              1.800000   0.000000   1.800000 (  1.799500)
rest_with_named_parameter     2.040000   0.000000   2.040000 (  2.040323)
lead_proc underflow_args      1.224000   0.000000   1.224000 (  1.225237)
opt_post_proc overflow_args   1.056000   0.000000   1.056000 (  1.057402)
~~~

modified
~~~
                                  user     system      total        real
benchmark_method              0.336000   0.000000   0.336000 (  0.336911)
rest_method                   0.708000   0.000000   0.708000 (  0.706142)
lead_method                   0.720000   0.000000   0.720000 (  0.717971)
post_method                   1.896000   0.000000   1.896000 (  1.894426)
lead_post_method              1.560000   0.000000   1.560000 (  1.560495)
rest_with_named_parameter     1.464000   0.000000   1.464000 (  1.467313)
lead_proc underflow_args      0.864000   0.000000   0.864000 (  0.863980)
opt_post_proc overflow_args   0.772000   0.000000   0.772000 (  0.770364)
~~~

---Files--------------------------------
bench_method_arg.rb (1.32 KB)
0001-Reduce-allocation-for-rest-parameters.patch (7.71 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>