<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Extensions on Shai Asher</title><link>https://slow-is-smooth.io/tags/extensions/</link><description>Recent content in Extensions on Shai Asher</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Sun, 18 Aug 2019 09:00:00 +0300</lastBuildDate><atom:link href="https://slow-is-smooth.io/tags/extensions/index.xml" rel="self" type="application/rss+xml"/><item><title>Writing the C Side by Hand</title><link>https://slow-is-smooth.io/blog/writing-the-c-side-by-hand/</link><pubDate>Sun, 18 Aug 2019 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/writing-the-c-side-by-hand/</guid><description>&lt;p&gt;The same C function, &lt;code&gt;long add(long a, long b) { return a + b; }&lt;/code&gt;, called from Python 3.7 costs 9.7&#10;nanoseconds through a hand-written extension using the new &lt;code&gt;METH_FASTCALL&lt;/code&gt; convention, 26.5 through&#10;the classic argument-tuple convention, 46 through pybind11, 51 through cffi and 189 through ctypes.&#10;The addition itself takes about a nanosecond. Everything else is the boundary between Python and C:&#10;packing and unpacking arguments, converting Python ints to C longs and back, and the reference&#10;counting that has to be exactly right at every crossing, because a single missing &lt;code&gt;Py_INCREF&lt;/code&gt; frees&#10;an object that something else is still using, and the program goes on running with garbage.&lt;/p&gt;</description></item></channel></rss>