faster-cpython / ideas

1.67k stars 49 forks source link

Combining deferred reference counting and tagged pointers #678

Open markshannon opened 1 month ago

markshannon commented 1 month ago

CPython does a lot of reference counting operations that have a major impact on the performance of the JIT and interpreter.

CPython also boxes all integers, which means that integer operations that are extremely cheap in most languages are quite expensive in (C)Python.

We can remove of the cost of reference counting by using deferred reference counting and almost all of the cost of boxing ints by using tagged pointers.

It is worth grouping these two issues together, as changes required to support deferred reference counting and those for tagged ints have some overlap. We need to distinguish between references on the stack, on the heap and passed to the C API for both deferred references and tagged int. This means that work done for one will help the other.