<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Cpython on Shai Asher</title><link>https://slow-is-smooth.io/tags/cpython/</link><description>Recent content in Cpython on Shai Asher</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Sun, 13 Dec 2020 09:00:00 +0200</lastBuildDate><atom:link href="https://slow-is-smooth.io/tags/cpython/index.xml" rel="self" type="application/rss+xml"/><item><title>Python 3.9 and the Road to a Faster CPython</title><link>https://slow-is-smooth.io/blog/python-3-9-and-the-road-to-a-faster-cpython/</link><pubDate>Sun, 13 Dec 2020 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/python-3-9-and-the-road-to-a-faster-cpython/</guid><description>&lt;p&gt;Python 3.9.1 came out last week. Built from their tags with the same compiler on the same machine,&#10;four releases of CPython, 3.6.5 to 3.9.1, run the same loop of integer arithmetic in the same&#10;0.36 seconds, give or take a hundredth. Calling a method got 24% cheaper over those releases, and&#10;calling &lt;code&gt;range(10)&lt;/code&gt; got 24% cheaper in 3.9 alone. And the slow program from Part 13, the one whose&#10;time went to the cycle collector, runs 44% slower on 3.8 than on 3.7, with the same number of&#10;collections, because each collection got slower.&lt;/p&gt;</description></item><item><title>Fork, Threads and Other Interpreters</title><link>https://slow-is-smooth.io/blog/fork-threads-and-other-interpreters/</link><pubDate>Sun, 13 Sep 2020 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/fork-threads-and-other-interpreters/</guid><description>&lt;p&gt;A Python program starts a thread, the thread takes a lock, and the main thread calls &lt;code&gt;os.fork()&lt;/code&gt;.&#10;In the child, the lock is still locked, and the thread that holds it doesn&amp;rsquo;t exist: &lt;code&gt;fork()&lt;/code&gt; copies&#10;the whole address space and exactly one thread, the one that called it. Nothing in the child will ever&#10;release that lock. And if the same program has ever created a subinterpreter and not destroyed it, it&#10;doesn&amp;rsquo;t get that far: on CPython 3.8.5, and on the 3.9 release candidate, the child hangs inside&#10;&lt;code&gt;fork()&lt;/code&gt; itself, on a lock that CPython&amp;rsquo;s own clean-up code takes twice.&lt;/p&gt;</description></item><item><title>Importing Is Executing</title><link>https://slow-is-smooth.io/blog/importing-is-executing/</link><pubDate>Sun, 21 Jun 2020 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/importing-is-executing/</guid><description>&lt;p&gt;Python 3.8 makes 479 system calls to run a program that does nothing, and imports 34 modules before&#10;the program&amp;rsquo;s first line. &lt;code&gt;import json&lt;/code&gt; adds about 270 more calls. Put 100 directories on&#10;&lt;code&gt;PYTHONPATH&lt;/code&gt; and the same &lt;code&gt;import json&lt;/code&gt; makes 4,372, and on Python 2.7 18,469, of which 17,705 fail,&#10;because Python 2 asks the file system for every name it might be looking for in every directory it&#10;might be in. And the bytecode it finds at the end of that search is trusted on the strength of two&#10;numbers, the source file&amp;rsquo;s modification time and its size: change a file without changing either and&#10;Python keeps running the old code.&lt;/p&gt;</description></item><item><title>The Other Engines</title><link>https://slow-is-smooth.io/blog/the-other-engines/</link><pubDate>Sun, 26 Apr 2020 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/the-other-engines/</guid><description>&lt;p&gt;The same five-line function takes 0.34 seconds on CPython 3.8, 7 milliseconds on PyPy, 6 on Numba&#10;and 5 as typed Cython. Run it on PyPy with the JIT switched off and it takes 0.56 seconds, slower&#10;than CPython. Then hand a list of a million integers to a small C extension, once, and PyPy&amp;rsquo;s&#10;own built-in &lt;code&gt;sum()&lt;/code&gt; over that list goes from 1.9 milliseconds to 52 and stays there, because the C&#10;extension asked for something PyPy only has to fake: a C array of &lt;code&gt;PyObject*&lt;/code&gt; pointers, which is&#10;what a Python list is in CPython and not in PyPy.&lt;/p&gt;</description></item><item><title>Profiling Below the Bytecode</title><link>https://slow-is-smooth.io/blog/profiling-below-the-bytecode/</link><pubDate>Sun, 16 Feb 2020 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/profiling-below-the-bytecode/</guid><description>&lt;p&gt;A Python 3.8 program that splits strings, adds up their lengths and writes JSON takes 4.5 seconds.&#10;&lt;code&gt;cProfile&lt;/code&gt; says 3.1 of them are in &lt;code&gt;str.split&lt;/code&gt;. &lt;code&gt;py-spy&lt;/code&gt; says two thirds of the samples are in the list&#10;comprehension that calls it. &lt;code&gt;perf&lt;/code&gt;, the Linux profiler, says a third of the time is in a C function&#10;called &lt;code&gt;collect&lt;/code&gt;, and another quarter in functions it calls, and it doesn&amp;rsquo;t name a single Python&#10;function. All three are right about what they measure, and only one of them found the real problem:&#10;the cycle collector from Part 4, walking three million lists that will never be garbage. With the&#10;collector turned off, the same program runs in 1.9 seconds.&lt;/p&gt;</description></item><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>Sharing Memory with C</title><link>https://slow-is-smooth.io/blog/sharing-memory-with-c/</link><pubDate>Sun, 20 Oct 2019 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/sharing-memory-with-c/</guid><description>&lt;p&gt;Python 3.8, released last week, adds a pickle protocol that can serialise a 100 MB NumPy array into&#10;122 bytes. The other 100,000,000 bytes aren&amp;rsquo;t copied into the pickle at all. They&amp;rsquo;re handed to the&#10;caller as a buffer, the array&amp;rsquo;s own memory, to send however it likes. That works because of an&#10;interface that has been in Python 3 from the start and that most Python programmers never see: the&#10;buffer protocol, through which one object lends the raw memory behind it to another, with no copy, as&#10;long as the lender agrees to keep it still.&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><item><title>Numbers Are C Numbers Until They Aren't</title><link>https://slow-is-smooth.io/blog/numbers-are-c-numbers-until-they-arent/</link><pubDate>Sun, 16 Jun 2019 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/numbers-are-c-numbers-until-they-arent/</guid><description>&lt;p&gt;In Python, &lt;code&gt;2**62 * 4&lt;/code&gt; is 18446744073709551616. In a NumPy array of 64-bit integers it is 0, and&#10;NumPy 1.16 doesn&amp;rsquo;t say a word. Multiply the same value as a single NumPy scalar and you get a&#10;warning and the same 0. Python&amp;rsquo;s integers have no fixed size, but almost every library that makes&#10;Python fast stores numbers as C integers and C doubles, and at that boundary Python&amp;rsquo;s arithmetic&#10;quietly becomes C&amp;rsquo;s. CPython itself sits on both sides: its integers are arbitrary-precision, and its&#10;fastest paths are the ones that can do the work in a C &lt;code&gt;long&lt;/code&gt; or &lt;code&gt;double&lt;/code&gt; and never build a Python&#10;object at all.&lt;/p&gt;</description></item><item><title>Dicts All the Way Down</title><link>https://slow-is-smooth.io/blog/dicts-all-the-way-down/</link><pubDate>Sun, 14 Apr 2019 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/dicts-all-the-way-down/</guid><description>&lt;p&gt;A Python 3.7 dict holding a million integers is 41,943,144 bytes. Delete every key and it is still&#10;41,943,144 bytes. Add one key back and it&amp;rsquo;s the same. The dict has no entries left, but it keeps&#10;the table it grew for a million, and the only way to get the memory back is to copy it: &lt;code&gt;dict(d)&lt;/code&gt;&#10;is 240 bytes. That is one of several things about dicts that the language doesn&amp;rsquo;t say and the C&#10;code decides, and dicts matter more than any other structure in CPython, because the interpreter is&#10;built out of them: module globals, class namespaces, instance attributes and keyword arguments are&#10;all dicts.&lt;/p&gt;</description></item><item><title>Strings That Change Width</title><link>https://slow-is-smooth.io/blog/strings-that-change-width/</link><pubDate>Sun, 17 Feb 2019 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/strings-that-change-width/</guid><description>&lt;p&gt;A megabyte of ASCII log text in Python 3.7 is a 1,000,049-byte object. Append one snake emoji to it&#10;and the result is 4,000,080 bytes, nearly four times as much memory for one more character, because&#10;every character in the new string, all million of them, is now stored in four bytes. That is not a&#10;quirk of a particular operation. It&amp;rsquo;s how CPython has stored strings since Python 3.3: each string&#10;picks the narrowest fixed width that fits its widest character, and one character is enough to widen&#10;all the others.&lt;/p&gt;</description></item><item><title>One Lock for the Whole Interpreter</title><link>https://slow-is-smooth.io/blog/one-lock-for-the-whole-interpreter/</link><pubDate>Sun, 16 Dec 2018 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/one-lock-for-the-whole-interpreter/</guid><description>&lt;p&gt;On a virtual machine with four CPUs, a Python 3.7 program counted down from forty million in 0.43&#10;seconds with one thread. With four threads, each counting a quarter, it took 0.47. The machine had&#10;four times the hardware and the program got slightly slower, because only one thread at a time was&#10;ever allowed to run Python code. The same program hashing 64 KiB buffers with four threads ran 3.7&#10;times faster than with one. Hashing 1 KiB buffers, it ran at the same speed as one thread. Nothing&#10;in the Python code says which of those you&amp;rsquo;ll get.&lt;/p&gt;</description></item><item><title>Where the Memory Goes</title><link>https://slow-is-smooth.io/blog/where-the-memory-goes/</link><pubDate>Sun, 11 Nov 2018 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/where-the-memory-goes/</guid><description>&lt;p&gt;A Python 3.7 process makes four million floats, 130 MB of resident memory, and then deletes all but&#10;one in every thousand. Four thousand floats are left, about 96 kilobytes of actual data. The process&#10;still uses 100 MB. Delete those last four thousand and it drops to 7 MB at once. Nothing leaked: the&#10;memory was in use, by the allocator&amp;rsquo;s bookkeeping rather than by the program, because CPython hands&#10;memory back to the operating system only in 256 KiB pieces, and only when every object in a piece&#10;is gone.&lt;/p&gt;</description></item><item><title>Counting References</title><link>https://slow-is-smooth.io/blog/counting-references/</link><pubDate>Sun, 16 Sep 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/counting-references/</guid><description>&lt;p&gt;A parent process builds a list of a million small lists, 184 MB of objects, and forks. The child&#10;never assigns to the list. It only loops over it and reads one number from each element, and when&#10;it&amp;rsquo;s done, 93 MB of the parent&amp;rsquo;s memory has been copied into the child. Reading a Python object&#10;writes to it, because every reference taken and dropped changes the object&amp;rsquo;s reference count, and&#10;the reference count lives in the object. The kernel can&amp;rsquo;t tell a reference count from data. It sees&#10;a write to a shared page and copies the page.&lt;/p&gt;</description></item><item><title>Every Python Call Is a C Call</title><link>https://slow-is-smooth.io/blog/every-python-call-is-a-c-call/</link><pubDate>Sun, 22 Jul 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/every-python-call-is-a-c-call/</guid><description>&lt;p&gt;In Python 3.7, a function that calls itself 22,000 times crashes the interpreter outright: no&#10;exception, no traceback, just &lt;code&gt;Segmentation fault&lt;/code&gt; and exit status 139. Python&amp;rsquo;s own recursion limit&#10;stops that from happening, by default, at a depth of 1,000. That limit is not a rule of the language.&#10;It&amp;rsquo;s a guess about the C stack, which CPython uses up at 384 bytes per Python call on this&#10;build. Raise the limit far enough, or reach the C stack through a path the limit never counts, and&#10;the guess runs out.&lt;/p&gt;</description></item><item><title>The Loop That Runs Your Bytecode</title><link>https://slow-is-smooth.io/blog/the-loop-that-runs-your-bytecode/</link><pubDate>Sun, 10 Jun 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/the-loop-that-runs-your-bytecode/</guid><description>&lt;p&gt;On Ubuntu 18.04, a five-line Python function that adds up a list compiles to 30 bytes: fifteen&#10;instructions of two bytes each. Running it means one C function, &lt;code&gt;_PyEval_EvalFrameDefault&lt;/code&gt;,&#10;reading those bytes two at a time and jumping, through a table of 256 addresses inside itself, to&#10;the C code for each instruction. On the machine I measured, reading a local variable added 0.6&#10;nanoseconds to each pass of a loop. Reading a global cost five times as much, and fetching a method&#10;cost seventeen times as much. A debugger&amp;rsquo;s trace hook made the whole loop nine times slower.&lt;/p&gt;</description></item><item><title>Every Value Is a Pointer to a Struct</title><link>https://slow-is-smooth.io/blog/every-value-is-a-pointer-to-a-struct/</link><pubDate>Sun, 13 May 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/every-value-is-a-pointer-to-a-struct/</guid><description>&lt;p&gt;On a freshly installed Ubuntu 18.04, the Python integer 1000 is 28 bytes of memory at an address&#10;you can print. The first eight bytes count how many things refer to it. The next eight are the&#10;address of a C structure that describes what an integer is. Eight more say how many 30-bit digits&#10;follow, and the last four hold the number itself, as the bytes &lt;code&gt;e8 03 00 00&lt;/code&gt;. Python goes to some&#10;lengths so that you never have to know any of this. It does not always succeed, and the places&#10;where it fails are the subject of this series.&lt;/p&gt;</description></item></channel></rss>