https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112021-01-03T03:09:51ZRuby Issue Tracking SystemRuby master - Misc #17502: C vs Rubyhttps://redmine.ruby-lang.org/issues/17502?journal_id=897242021-01-03T03:09:51Zk0kubun (Takashi Kokubun)takashikkbn@gmail.com
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/16254">Feature #16254</a>: MRI internal: Define built-in classes in Ruby with `__intrinsic__` syntax</i> added</li></ul> Ruby master - Misc #17502: C vs Rubyhttps://redmine.ruby-lang.org/issues/17502?journal_id=897252021-01-03T03:49:32Zk0kubun (Takashi Kokubun)takashikkbn@gmail.com
<ul><li><strong>Assignee</strong> set to <i>ko1 (Koichi Sasada)</i></li></ul><p>First off, you may be interested in reading [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: MRI internal: Define built-in classes in Ruby with `__intrinsic__` syntax (Closed)" href="https://redmine.ruby-lang.org/issues/16254">#16254</a>] in case you haven't. (Note: <code>__intrinsitc__</code> was renamed to <code>__builtin_</code> first, and then renamed again to <code>Primitive.</code>)</p>
<p>Let me separately discuss multiple independent topics you raised in this single ticket:</p>
<a name="Support-Primitive-for-gems-and-C-extensions"></a>
<h2 >Support <code>Primitive.</code> for gems and C extensions<a href="#Support-Primitive-for-gems-and-C-extensions" class="wiki-anchor">¶</a></h2>
<blockquote>
<p>External gems don't have access to this.</p>
<p>Is there a plan for an API that would:</p>
<ol>
<li>be accessible to C extensions</li>
</ol>
</blockquote>
<p>Apparently the current implementation was not designed for this, but only <a class="user active user-mention" href="https://redmine.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a> can answer about his future plan.</p>
<a name="Using-Primitive-to-optimize-keyword-parameters-of-C-API"></a>
<h2 >Using <code>Primitive.</code> to optimize keyword parameters of C API<a href="#Using-Primitive-to-optimize-keyword-parameters-of-C-API" class="wiki-anchor">¶</a></h2>
<blockquote>
<p>The C-API to deal with keyword parameters is also very verbose the parsing and the engine does not know it.</p>
<p>Is there a plan for an API that would:<br>
3) has an easy way to define keyword parameters?</p>
<p>I realize that RBS may give perfect signatures, but ideally parameters would be more informative for C-functions too.</p>
</blockquote>
<p>Could you talk about keyword parameters in a different ticket, either in [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: MRI internal: Define built-in classes in Ruby with `__intrinsic__` syntax (Closed)" href="https://redmine.ruby-lang.org/issues/16254">#16254</a>] (because using <code>Primitive.</code> to optimize keyword parameters of C methods was a part of its design) or another new Misc ticket? At least the PR you quoted wasn't using keyword arguments, and the purpose is fairly independent from the thing described below.</p>
<a name="Annotating-C-methods"></a>
<h2 >Annotating C methods<a href="#Annotating-C-methods" class="wiki-anchor">¶</a></h2>
<blockquote>
<p>Moreover, some optimization PRs are simply rewriting C-code into Ruby using pseudo C code.</p>
<p>I understand the intentions are great, but changes like <a href="https://github.com/ruby/ruby/pull/4018/files" class="external">https://github.com/ruby/ruby/pull/4018/files</a> seem a symptom that something needs to be improved with the C api.<br>
It seems to me that this is a way to circumvent a deeper issue. Is this the right direction?</p>
<ol start="2">
<li>can't be re-written any faster in pseuso-C in Ruby</li>
</ol>
</blockquote>
<p>My purpose of merging the PR was to annotate such C functions as they're safe to be inlined by JIT, using <code>Primitive.attr! 'inline'</code>. Even while the purpose is to optimize JIT, we should maintain VM's performance when we optimize JIT, and therefore we confirm VM performance is not degraded by benchmarking VM. Because it's very important that annotating a method doesn't make the method slower, the current situation where presudo-C in Ruby very slightly improves performance is somewhat helpful for adding annotations without thinking about its VM impact too much, although it shouldn't be the purpose of using pseudo-C in Ruby.</p>
<p>To be clear, I'm not fond of pseudo-C in Ruby itself and I'm personally fine with annotating C methods just using C. But that's what <a class="user active user-mention" href="https://redmine.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a> has objected to. So please talk to him about why.</p> Ruby master - Misc #17502: C vs Rubyhttps://redmine.ruby-lang.org/issues/17502?journal_id=923502021-06-05T09:21:03ZEregon (Benoit Daloze)
<ul></ul><p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/17">@ko1 (Koichi Sasada)</a> Could you reply to this?</p>
<p>I feel C code in the middle of Ruby code is not so great, and if moving C code to strings in Ruby is just to add <code>Primitive.attr! 'inline'</code> (like for <code>Float#zero?</code>),<br>
it seems much simpler and nicer to add <code>rb_impl_define_method_inline()</code> or so (private API, not public C API of course).</p>
<p>For instance, there is no syntax highlighting for C code in Ruby code (<code>Primitive.cexpr! 'flo_iszero(self) ? Qtrue : Qfalse'</code>), looking at callers of flo_iszero() would only reveal generated code, not the <code>Primitive.cexpr!</code>, etc.</p> Ruby master - Misc #17502: C vs Rubyhttps://redmine.ruby-lang.org/issues/17502?journal_id=950532021-12-02T19:53:23Zko1 (Koichi Sasada)
<ul></ul><blockquote>
<ol>
<li>be accessible to C extensions</li>
</ol>
</blockquote>
<p>I think it is better but not sure we can.</p>
<blockquote>
<ol start="2">
<li>can't be re-written any faster in pseuso-C in Ruby</li>
</ol>
</blockquote>
<p>Do you mean Cython for Python?</p>
<blockquote>
<ol start="3">
<li>has an easy way to define keyword parameters?</li>
</ol>
</blockquote>
<p>I believe the easiest way to write keywords is written in Ruby.</p>
<blockquote>
<p>I feel C code in the middle of Ruby code is not so great, and if moving C code to strings in Ruby is just to add Primitive.attr! 'inline' (like for Float#zero?),</p>
</blockquote>
<p>For me, for few lines I'm fine to mix Ruby and C.</p>