<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C-Api on Shai Asher</title><link>https://slow-is-smooth.io/tags/c-api/</link><description>Recent content in C-Api on Shai Asher</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Sun, 15 Dec 2019 09:00:00 +0200</lastBuildDate><atom:link href="https://slow-is-smooth.io/tags/c-api/index.xml" rel="self" type="application/rss+xml"/><item><title>What Python 3.8 Changed Underneath</title><link>https://slow-is-smooth.io/blog/what-python-3-8-changed-underneath/</link><pubDate>Sun, 15 Dec 2019 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/what-python-3-8-changed-underneath/</guid><description>&lt;p&gt;Python 3.8&amp;rsquo;s release notes say that &lt;code&gt;LOAD_GLOBAL&lt;/code&gt; &amp;ldquo;is about 40% faster now&amp;rdquo;. On the same machine, in&#10;the same sitting, a function that reads a global eight times and is called a million times paid 0.47&#10;nanoseconds per global read over a local in 3.8.0, against 2.05 in 3.7.5. A loop that does the same&#10;reads a million times inside &lt;em&gt;one&lt;/em&gt; call paid 2.42 in 3.8.0, slightly more than in 3.7.5. Same&#10;instruction, same globals. The cache that speeds it up turns itself on only after the function has&#10;been &lt;em&gt;called&lt;/em&gt; 1,024 times, and a hot loop inside a function that&amp;rsquo;s called once never gets it.&lt;/p&gt;</description></item><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>