
On 2021-12-15 2:57 p.m., Guido van Rossum wrote:
But as long as the imbalance is less than 0x_2000_0000, the refcount will remain in the inclusive range [ 0x_4000_0000 , 0x_7FFF_FFFF ] and we can test for immortality by testing a single bit:
if (o->ob_refcnt & 0x_4000_0000)
Could we have a full GC pass reset those counts to make it even more unlikely to get out of bounds? Allocating immortal objects from a specific memory region seems like another idea worth pursuing. It seems mimalloc has the ability to allocate pools aligned to certain large boundaries. That takes some platform specific magic. If we can do that, the test for immortality is pretty cheap. However, if you can't allocate them at a fixed region determined at compile time, I don't think you can match the performance of the code above. Maybe it helps that you could determine immortality by looking at the PyObject pointer and without loading the ob_refcnt value from memory? You would do something like: if (((uintptr_t)o) & _Py_immortal_mask) The _Py_immortal_mask value would not be known at compile time but would be a global constant. So, it would be cached by the CPU.