https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17097754782018-08-20T05:13:57ZRuby Issue Tracking SystemRuby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736182018-08-20T05:13:57Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Looks good to me. Though destructive operation to the rest array may make the source code unclear, performance is more important in this case, I think.</p>
<p>Some other functions in vm_args.c also use rb_ary_dup. There may be more room to optimize.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736202018-08-20T06:29:28Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>mame (Yusuke Endoh) wrote:</p>
<blockquote>
<p>Some other functions in vm_args.c also use rb_ary_dup. There may be more room to optimize.</p>
</blockquote>
<p>Yes, it can be further optimized for keyword argument and argument setup for the block. I'll modify the patch in a day or two.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736212018-08-20T06:42:23Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:chopraanmol1@gmail.com" class="email">chopraanmol1@gmail.com</a> wrote:</p>
<blockquote>
<p>Yes, it can be further optimized for keyword argument and argument setup for the block. I'll modify the patch in a day or two.</p>
</blockquote>
<p>Cool! It's probably worth implementing something like<br>
rb_ary_shift_m (but without the return value) to avoid looping<br>
on rb_ary_shift.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736232018-08-20T10:22:51Zchopraanmol1 (Anmol Chopra)
<ul><li><strong>File</strong> <a href="/attachments/7327">bench_method_arg.rb</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/7327/bench_method_arg.rb">bench_method_arg.rb</a> added</li><li><strong>File</strong> <i>improve_rest_parameters_setup_20_8_2018.patch</i> added</li><li><strong>File</strong> deleted (<del><i>bench_method_arg.rb</i></del>)</li><li><strong>Description</strong> updated (<a title="View differences" href="/journals/73623/diff?detail_id=49745">diff</a>)</li></ul> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736242018-08-20T10:27:30Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>normalperson (Eric Wong) wrote:</p>
<blockquote>
<p>Cool! It's probably worth implementing something like<br>
rb_ary_shift_m (but without the return value) to avoid looping<br>
on rb_ary_shift.</p>
</blockquote>
<p>Added rb_ary_clear_m (suggestion for a better name will be appreciated) with suggested changes.</p>
<p>mame (Yusuke Endoh) wrote:</p>
<blockquote>
<p>Some other functions in vm_args.c also use rb_ary_dup. There may be more room to optimize.</p>
</blockquote>
<p>Modified patch to ensure rb_ary_dup is called at most once.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736252018-08-20T11:22:04Zchopraanmol1 (Anmol Chopra)
<ul><li><strong>File</strong> <i>0001-Reduce-allocation-for-rest-parameters.patch</i> added</li><li><strong>File</strong> deleted (<del><i>improve_rest_parameters_setup.patch</i></del>)</li><li><strong>File</strong> deleted (<del><i>improve_rest_parameters_setup_20_8_2018.patch</i></del>)</li></ul> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736312018-08-20T19:04:15Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:chopraanmol1@gmail.com" class="email">chopraanmol1@gmail.com</a> wrote:</p>
<blockquote>
<p>normalperson (Eric Wong) wrote:</p>
<blockquote>
<p>Cool! It's probably worth implementing something like<br>
rb_ary_shift_m (but without the return value) to avoid looping<br>
on rb_ary_shift.</p>
</blockquote>
</blockquote>
<blockquote>
<p>Added rb_ary_clear_m (suggestion for a better name will be<br>
appreciated) with suggested changes.</p>
</blockquote>
<p>Thanks; for internal functions the name isn't as important :)</p>
<p>New functions prototypes should go into internal.h, though.<br>
ruby/intern.h ended up being part of the public API and external<br>
libraries depend on it :<</p>
<p>There's no reason for arg_rest_dup to be a macro instead of a<br>
static inline function. Static inlines are preferred because<br>
they make life easier for the compiler and debugger.</p>
<p>Also, multi-line macros without "do {} while (0)" is dangerous<br>
to control flow.</p>
<p>Thanks again.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736352018-08-20T22:15:11Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Thank you, too. Two points:</p>
<p>First, the prefix <code>_m</code> is often used for an entry function of Ruby-level method that is passed to <code>rb_define_method</code>. Though it is just an internal function, it would be better to avoid the prefix. How about <code>rb_ary_remove_first</code>?</p>
<p>Second, I agree with ensuring rb_ary_dup is called at most once. But I'm afraid if rewriting the array without dup may cause obscure incompatibility. It is difficult for me to review your patch.</p>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a>, could you review this?</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736382018-08-20T23:14:31Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>normalperson (Eric Wong) wrote:</p>
<blockquote>
<blockquote>
<p>Added rb_ary_clear_m (suggestion for a better name will be<br>
appreciated) with suggested changes.</p>
</blockquote>
<p>Thanks; for internal functions the name isn't as important :)</p>
</blockquote>
<p>What about rb_ary_clear_head? (or rb_ary_behead :)</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736412018-08-21T04:41:55Zchopraanmol1 (Anmol Chopra)
<ul><li><strong>File</strong> <i>0001-Reduce-allocation-for-rest-parameters.patch</i> added</li><li><strong>File</strong> deleted (<del><i>0001-Reduce-allocation-for-rest-parameters.patch</i></del>)</li></ul> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736422018-08-21T04:46:02Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>normalperson (Eric Wong) wrote:</p>
<blockquote>
<p>New functions prototypes should go into internal.h, though.<br>
ruby/intern.h ended up being part of the public API and external<br>
libraries depend on it :<</p>
<p>There's no reason for arg_rest_dup to be a macro instead of a<br>
static inline function. Static inlines are preferred because<br>
they make life easier for the compiler and debugger.</p>
</blockquote>
<p>Updated.</p>
<p>mame (Yusuke Endoh) wrote:</p>
<blockquote>
<p>First, the prefix <code>_m</code> is often used for an entry function of Ruby-level method that is passed to <code>rb_define_method</code>. Though it is just an internal function, it would be better to avoid the prefix. How about <code>rb_ary_remove_first</code>?</p>
</blockquote>
<p>For now, I'm renaming the method to rb_ary_behead (suggested by nobu)</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736432018-08-21T05:19:59Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>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.</p>
<p>Unless it breaks test beyond repair, I'll add this alternate patch with the respective benchmark(It probably will be slower for the large array), so it can be compared side by side. 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:</p>
<pre><code>def opt_post(a,b,c=1,d=2,e,f); end
</code></pre> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736442018-08-21T05:36:19Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>normalperson (Eric Wong) wrote:</p>
<blockquote>
<p>New functions prototypes should go into internal.h, though.<br>
ruby/intern.h ended up being part of the public API and external<br>
libraries depend on it :<</p>
</blockquote>
<p>Moving method to internal.h breaks jit <a href="https://travis-ci.org/ruby/ruby/builds/418529271" class="external">https://travis-ci.org/ruby/ruby/builds/418529271</a> , I'm not sure how to fix this failure.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736452018-08-21T05:59:47Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>chopraanmol1 (Anmol Chopra) wrote:</p>
<blockquote>
<p>Moving method to internal.h breaks jit <a href="https://travis-ci.org/ruby/ruby/builds/418529271" class="external">https://travis-ci.org/ruby/ruby/builds/418529271</a> , I'm not sure how to fix this failure.</p>
</blockquote>
<p>Define the function with <code>MJIT_FUNC_EXPORTED</code>.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736462018-08-21T07:26:46Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>chopraanmol1 (Anmol Chopra) wrote:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>Implementation: <a href="https://github.com/ruby/ruby/compare/trunk...chopraanmol1:improve_rest_parameters_setup_v2" class="external">https://github.com/ruby/ruby/compare/trunk...chopraanmol1:improve_rest_parameters_setup_v2</a></p>
<p>Benchmark result</p>
<pre><code>
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
</code></pre>
<p>chopraanmol1 (Anmol Chopra) wrote:</p>
<blockquote>
<p>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:</p>
</blockquote>
<p>args->argv and locals are pointing to same address so it is not feasible.</p>
<p>Note: This second patch is not the final implementation, there are few more changes. In second patch args_check_block_arg0/args_setup_opt_parameters function can still assign args->rest / modify args->rest_index, 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.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736472018-08-21T07:35:05Zchopraanmol1 (Anmol Chopra)
<ul><li><strong>File</strong> <a href="/attachments/7334">Reduce-allocation-for-rest-parameters-v2.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/7334/Reduce-allocation-for-rest-parameters-v2.patch">Reduce-allocation-for-rest-parameters-v2.patch</a> added</li><li><strong>File</strong> <i>Reduce-allocation-for-rest-parameters-v1.patch</i> added</li><li><strong>File</strong> <a href="/attachments/7336">bench_method_arg_v2.rb</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/7336/bench_method_arg_v2.rb">bench_method_arg_v2.rb</a> added</li><li><strong>File</strong> deleted (<del><i>0001-Reduce-allocation-for-rest-parameters.patch</i></del>)</li></ul> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736502018-08-21T12:45:21Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>Limitation of patch 2.</p>
<ol>
<li>
<p>Patch 2 gets slower than Patch 1 for a large array. Array with length 100 - 200 have similar performance but beyond that patch 1 is faster in most of the case.</p>
</li>
<li>
<p>Patch 2 results in segmentation fault for following: <a href="https://github.com/ruby/ruby/blob/8e66ffc1d756c42ee025a56672ad71f2200ca6be/test/ruby/test_method.rb#L951" class="external">https://github.com/ruby/ruby/blob/8e66ffc1d756c42ee025a56672ad71f2200ca6be/test/ruby/test_method.rb#L951</a></p>
</li>
</ol>
<p><del>Ignoring above limitation patch 2 do perform better for the small array. One Hack Solution can be to check the length of splat arg against some arbitrary number to decide if splat arg should be expanded to args->argv or should be assigned to args->rest. But it doesn't sound like a nice solution.</del></p>
<p>I'm not able to reproduce benchmark result for Patch 2. Even for splat args with size under 100 patch 2 has similar performance to patch 1. Patch 2 is only significantly faster on <strong>lead_proc underflow_args</strong> benchmark. Given the above limitation patch 2 is not worth it.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736722018-08-23T07:22:29Zko1 (Koichi Sasada)
<ul></ul><p>sorry, which patch should I review?</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=736752018-08-23T07:41:14Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>ko1 (Koichi Sasada) wrote:</p>
<blockquote>
<p>sorry, which patch should I review?</p>
</blockquote>
<p>Reduce-allocation-for-rest-parameters-v1.patch</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737232018-08-27T05:22:02Zchopraanmol1 (Anmol Chopra)
<ul><li><strong>File</strong> <i>Reduce-allocation-for-rest-parameters-v1.patch</i> added</li></ul> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737242018-08-27T05:22:14Zchopraanmol1 (Anmol Chopra)
<ul><li><strong>File</strong> deleted (<del><i>Reduce-allocation-for-rest-parameters-v1.patch</i></del>)</li></ul> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737252018-08-27T05:26:46Zchopraanmol1 (Anmol Chopra)
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a>, It would be great if you could review <a href="https://bugs.ruby-lang.org/attachments/7343/Reduce-allocation-for-rest-parameters-v1.patch" class="external">https://bugs.ruby-lang.org/attachments/7343/Reduce-allocation-for-rest-parameters-v1.patch</a></p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737322018-08-27T06:33:20Zko1 (Koichi Sasada)
<ul></ul><p>Sorry for late response.</p>
<a name="Idea-as-my-understanding"></a>
<h1 >Idea (as my understanding)<a href="#Idea-as-my-understanding" class="wiki-anchor">¶</a></h1>
<p>~a rest parameter" is dup multiple times because of current implementation. Only 1 "dup" is needed. They should be eliminate.<br>
The patch try to manage "dup'ed or not" by passing <code>skip_rest_ary_dup</code>, and if it is true, then we don't need to dup the rest parameter again.</p>
<a name="Comment"></a>
<h1 >Comment<a href="#Comment" class="wiki-anchor">¶</a></h1>
<p>I'm fine to introduce your idea.<br>
Why don't you put a new field in <code>args_info</code>?</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737352018-08-27T07:41:03Zchopraanmol1 (Anmol Chopra)
<ul></ul><p>ko1 (Koichi Sasada) wrote:</p>
<blockquote>
<a name="Idea-as-my-understanding"></a>
<h1 >Idea (as my understanding)<a href="#Idea-as-my-understanding" class="wiki-anchor">¶</a></h1>
<p>~a rest parameter" is dup multiple times because of current implementation. Only 1 "dup" is needed. They should be eliminate.<br>
The patch try to manage "dup'ed or not" by passing <code>skip_rest_ary_dup</code>, and if it is true, then we don't need to dup the rest parameter again.</p>
</blockquote>
<p>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).</p>
<blockquote>
<a name="Comment"></a>
<h1 >Comment<a href="#Comment" class="wiki-anchor">¶</a></h1>
<p>I'm fine to introduce your idea.<br>
Why don't you put a new field in <code>args_info</code>?</p>
</blockquote>
<p>This suggestion makes a lot of sense as it will simplify this patch, I'll update the patch soon to reflect this.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737382018-08-27T09:37:03Zchopraanmol1 (Anmol Chopra)
<ul><li><strong>File</strong> <a href="/attachments/7344">Reduce-allocation-for-rest-parameters-v1.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/7344/Reduce-allocation-for-rest-parameters-v1.patch">Reduce-allocation-for-rest-parameters-v1.patch</a> added</li><li><strong>File</strong> deleted (<del><i>Reduce-allocation-for-rest-parameters-v1.patch</i></del>)</li></ul> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737412018-08-27T10:08:58Zchopraanmol1 (Anmol Chopra)
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a>, I've added new field rest_dupped to args_info.</p>
<p>Updated patch <a href="https://bugs.ruby-lang.org/attachments/7344/Reduce-allocation-for-rest-parameters-v1.patch" class="external">https://bugs.ruby-lang.org/attachments/7344/Reduce-allocation-for-rest-parameters-v1.patch</a></p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737642018-08-28T06:53:35Zko1 (Koichi Sasada)
<ul></ul><p>It seems fine.<br>
I'll commit it.</p> Ruby master - Feature #15010: Reduce allocation for rest parametershttps://bugs.ruby-lang.org/issues/15010?journal_id=737652018-08-28T07:06:13Zko1 (Koichi Sasada)
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r64583.</p>
<hr>
<p>rest parameter optimization [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Reduce allocation for rest parameters (Closed)" href="https://bugs.ruby-lang.org/issues/15010">#15010</a>]</p>
<ul>
<li>
<p>vm_args.c: rb_ary_dup(args->rest) to be used at most once during<br>
parameter setup. [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Reduce allocation for rest parameters (Closed)" href="https://bugs.ruby-lang.org/issues/15010">#15010</a>]<br>
A patch by chopraanmol1 (Anmol Chopra) <a href="mailto:chopraanmol1@gmail.com" class="email">chopraanmol1@gmail.com</a>.</p>
</li>
<li>
<p>array.c (rb_ary_behead): added to remove first n elements.</p>
</li>
</ul>