← Writing

Cpython

17 posts

Importing Is Executing

Part 15 of a series on the CPython runtime: what import does on Linux, counted in system calls. Python 2.7 making 18,469 of them for import json with 100 path entries and Python 3.8 4,372, a .pyc that keeps running old …

The Other Engines

Part 14 of a series on the CPython runtime: one loop on CPython 3.8, PyPy 7.3, Cython and Numba, from 0.34 seconds to 5 milliseconds. PyPy's JIT trace read op by op, the guard that falls back to big integers, the bridge …

Profiling Below the Bytecode

Part 13 of a series on the CPython runtime: one slow program, five ways of looking at it. cProfile blames str.split, py-spy blames a list comprehension, perf finds the cycle collector taking 60% of the time and no Python …

Sharing Memory with C

Part 11 of a series on the CPython runtime: the buffer protocol, the interface that lets Python objects and C code share one block of memory without copying it. One bytearray seen as a memoryview, a NumPy array and a C …

Dicts All the Way Down

Part 8 of a series on the CPython runtime: the dict in Python 3.7.3, read from memory. The compact index table and entries array, the probing recurrence reproduced in Python and checked against the real table, the size …

Strings That Change Width

Part 7 of a series on the CPython runtime: how Python 3.7.2 stores a str. PEP 393's three widths, read from the string header, and one emoji that makes a megabyte of log text four megabytes; a UTF-8 copy that C …

Where the Memory Goes

Part 5 of a series on the CPython runtime: pymalloc, the allocator under every small object. Size classes, 4 KiB pools and 256 KiB arenas, a pool header read from Python, why 4,000 surviving floats keep 95 MiB of memory, …