https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112022-07-05T22:11:00ZRuby Issue Tracking SystemRuby master - Feature #18897: Add a new instruction for sending messages to ephemeral stack arrayshttps://bugs.ruby-lang.org/issues/18897?journal_id=982872022-07-05T22:11:00Ztenderlovemaking (Aaron Patterson)tenderlove@ruby-lang.org
<ul></ul><p>Just to be complete with regard to stack frames, <code>max</code> doesn't show up in the stack trace in the following code even though <code><=></code> is called via <code>.max</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="nb">attr_reader</span> <span class="ss">:x</span>
<span class="k">def</span> <span class="nf">initialize</span> <span class="n">x</span>
<span class="vi">@x</span> <span class="o">=</span> <span class="n">x</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf"><</span><span class="o">=></span> <span class="n">other</span>
<span class="nb">puts</span> <span class="nb">caller</span>
<span class="n">x</span> <span class="o"><=></span> <span class="n">other</span><span class="p">.</span><span class="nf">x</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">check_max</span>
<span class="p">[</span><span class="no">Foo</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="mi">2</span><span class="p">)].</span><span class="nf">max</span>
<span class="k">end</span>
<span class="n">check_max</span>
</code></pre>
<p>We could probably detect that the <code><=></code> method is implemented in "user" code and push a frame, but it seems like nobody notices (I can't find any bug reports). 😅</p> Ruby master - Feature #18897: Add a new instruction for sending messages to ephemeral stack arrayshttps://bugs.ruby-lang.org/issues/18897?journal_id=983782022-07-19T09:36:05Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/73">@tenderlovemaking (Aaron Patterson)</a> Thank you! Your patch looks good to me.</p> Ruby master - Feature #18897: Add a new instruction for sending messages to ephemeral stack arrayshttps://bugs.ruby-lang.org/issues/18897?journal_id=1028552023-04-19T00:16:34Ztenderlovemaking (Aaron Patterson)tenderlove@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Emit special instruction for array literal + .(hash|min|max) This commit introduces a new instru..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/c5fc1ce975ecdf1c6818714e47579c5d3531c4ca">git|c5fc1ce975ecdf1c6818714e47579c5d3531c4ca</a>.</p>
<hr>
<p>Emit special instruction for array literal + .(hash|min|max)</p>
<p>This commit introduces a new instruction <code>opt_newarray_send</code> which is<br>
used when there is an array literal followed by either the <code>hash</code>,<br>
<code>min</code>, or <code>max</code> method.</p>
<pre><code>[a, b, c].hash
</code></pre>
<p>Will emit an <code>opt_newarray_send</code> instruction. This instruction falls<br>
back to a method call if the "interested" method has been monkey<br>
patched.</p>
<p>Here are some examples of the instructions generated:</p>
<pre><code>$ ./miniruby --dump=insns -e '[@a, @b].max'
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
0000 getinstancevariable :@a, <is:0> ( 1)[Li]
0003 getinstancevariable :@b, <is:1>
0006 opt_newarray_send 2, :max
0009 leave
$ ./miniruby --dump=insns -e '[@a, @b].min'
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
0000 getinstancevariable :@a, <is:0> ( 1)[Li]
0003 getinstancevariable :@b, <is:1>
0006 opt_newarray_send 2, :min
0009 leave
$ ./miniruby --dump=insns -e '[@a, @b].hash'
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,13)> (catch: FALSE)
0000 getinstancevariable :@a, <is:0> ( 1)[Li]
0003 getinstancevariable :@b, <is:1>
0006 opt_newarray_send 2, :hash
0009 leave
</code></pre>
<p>[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add a new instruction for sending messages to ephemeral stack arrays (Closed)" href="https://bugs.ruby-lang.org/issues/18897">#18897</a>] <a href="/issues/18897">[ruby-core:109147]</a></p>
<p>Co-authored-by: John Hawthorn <a href="mailto:jhawthorn@github.com" class="email">jhawthorn@github.com</a></p>