[ruby-core:88679] [Ruby trunk Feature#15010] Reduce allocation for rest parameters

From: chopraanmol1@...
Date: 2018-08-27 07:41:04 UTC
List: ruby-core #88679
Issue #15010 has been updated by chopraanmol1 (Anmol Chopra).


ko1 (Koichi Sasada) wrote:
> # Idea (as my understanding)
> 
> ~a rest parameter" is dup multiple times because of current implementation. Only 1 "dup" is needed. They should be eliminate.
> The patch try to manage "dup'ed or not" by passing `skip_rest_ary_dup`, and if it is true, then we don't need to dup the rest parameter again.

Yes, and once a rest parameter is duped it mutates the array in case if rest_index is modified (Previously, only args_setup_post_parameters used to mutate rest parameter). 
> 
> # Comment
> 
> I'm fine to introduce your idea.
> Why don't you put a new field in `args_info`?

This suggestion makes a lot of sense as it will simplify this patch, I'll update the patch soon to reflect this.


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

* 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)
Reduce-allocation-for-rest-parameters-v2.patch (3.57 KB)
bench_method_arg_v2.rb (3.15 KB)
Reduce-allocation-for-rest-parameters-v1.patch (7.71 KB)


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

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

In This Thread

Prev Next