Deprecate then remove the `tp_is_gc` slot

tp_is_gc is a function pointer field in PyTypeObject.
Its purpose is to allow some objects in a class to be GC’d while others are not. This was needed for classes before we added immortal objects, as some were statically allocated and not GC’d, whereas others were heap allocated and needed to be handled by the cycle collector.

However, we now have immortal objects (and have since 3.12), so there is no need for this hook. Statically allocated objects should be immortal, and thus not touched by the GC.

Having this extra function adds overhead to the GC and complicates implementation of C extensions. We should deprecate tp_is_gc and aim to remove it when appropriate. Probably in 3.19 or thereabouts.

5 Likes

I would be happy if we can eliminate this. It adds extra performance cost and extra complexity. I’ve seen extension modules that use it incorrectly as well (an instance should either return True for tp_is_gc always or never, not switch depending on some factors that the extension module uses to decide).

3 Likes

Are there public APIs for immortalization?