]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-124218: Refactor per-thread reference counting (#124844)
authorSam Gross <colesbury@gmail.com>
Tue, 1 Oct 2024 17:05:42 +0000 (13:05 -0400)
committerGitHub <noreply@github.com>
Tue, 1 Oct 2024 17:05:42 +0000 (17:05 +0000)
Currently, we only use per-thread reference counting for heap type objects and
the naming reflects that. We will extend it to a few additional types in an
upcoming change to avoid scaling bottlenecks when creating nested functions.

Rename some of the files and functions in preparation for this change.

15 files changed:
Include/internal/pycore_interp.h
Include/internal/pycore_object.h
Include/internal/pycore_tstate.h
Include/internal/pycore_typeid.h [deleted file]
Include/internal/pycore_uniqueid.h [new file with mode: 0644]
Makefile.pre.in
Objects/typeobject.c
PCbuild/_freeze_module.vcxproj
PCbuild/_freeze_module.vcxproj.filters
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters
Python/gc_free_threading.c
Python/pylifecycle.c
Python/pystate.c
Python/uniqueid.c [moved from Python/typeid.c with 51% similarity]

index a1898d926ac39f3aac96b11a062a0c52cd4b658a..ade69be1cf2eb2415bd55f0c94e8bfb773ce5553 100644 (file)
@@ -35,7 +35,7 @@ extern "C" {
 #include "pycore_qsbr.h"          // struct _qsbr_state
 #include "pycore_tstate.h"        // _PyThreadStateImpl
 #include "pycore_tuple.h"         // struct _Py_tuple_state
-#include "pycore_typeid.h"        // struct _Py_type_id_pool
+#include "pycore_uniqueid.h"      // struct _Py_unique_id_pool
 #include "pycore_typeobject.h"    // struct types_state
 #include "pycore_unicodeobject.h" // struct _Py_unicode_state
 #include "pycore_warnings.h"      // struct _warnings_runtime_state
@@ -221,7 +221,7 @@ struct _is {
 #if defined(Py_GIL_DISABLED)
     struct _mimalloc_interp_state mimalloc;
     struct _brc_state brc;  // biased reference counting state
-    struct _Py_type_id_pool type_ids;
+    struct _Py_unique_id_pool unique_ids;  // object ids for per-thread refcounts
     PyMutex weakref_locks[NUM_WEAKREF_LIST_LOCKS];
 #endif
 
index 80b588815bc9cfbbb7456e88a1f018cff05eb1b4..0af13b1bcda20b76c22dad8ac82dabae8b574827 100644 (file)
@@ -14,7 +14,7 @@ extern "C" {
 #include "pycore_interp.h"        // PyInterpreterState.gc
 #include "pycore_pyatomic_ft_wrappers.h"  // FT_ATOMIC_STORE_PTR_RELAXED
 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
-#include "pycore_typeid.h"        // _PyType_IncrefSlow
+#include "pycore_uniqueid.h"      // _PyType_IncrefSlow
 
 
 #define _Py_IMMORTAL_REFCNT_LOOSE ((_Py_IMMORTAL_REFCNT >> 1) + 1)
@@ -335,12 +335,12 @@ _Py_INCREF_TYPE(PyTypeObject *type)
     // Unsigned comparison so that `unique_id=-1`, which indicates that
     // per-thread refcounting has been disabled on this type, is handled by
     // the "else".
-    if ((size_t)ht->unique_id < (size_t)tstate->types.size) {
+    if ((size_t)ht->unique_id < (size_t)tstate->refcounts.size) {
 #  ifdef Py_REF_DEBUG
         _Py_INCREF_IncRefTotal();
 #  endif
         _Py_INCREF_STAT_INC();
-        tstate->types.refcounts[ht->unique_id]++;
+        tstate->refcounts.values[ht->unique_id]++;
     }
     else {
         // The slow path resizes the thread-local refcount array if necessary.
@@ -368,12 +368,12 @@ _Py_DECREF_TYPE(PyTypeObject *type)
     // Unsigned comparison so that `unique_id=-1`, which indicates that
     // per-thread refcounting has been disabled on this type, is handled by
     // the "else".
-    if ((size_t)ht->unique_id < (size_t)tstate->types.size) {
+    if ((size_t)ht->unique_id < (size_t)tstate->refcounts.size) {
 #  ifdef Py_REF_DEBUG
         _Py_DECREF_DecRefTotal();
 #  endif
         _Py_DECREF_STAT_INC();
-        tstate->types.refcounts[ht->unique_id]--;
+        tstate->refcounts.values[ht->unique_id]--;
     }
     else {
         // Directly decref the type if the type id is not assigned or if
index f681b644c9ad5d948e788269f4b818c7301a1a1f..a72ef4493b77ca05fb44957a2b8cee26634b23b8 100644 (file)
@@ -32,15 +32,15 @@ typedef struct _PyThreadStateImpl {
     struct _Py_freelists freelists;
     struct _brc_thread_state brc;
     struct {
-        // The thread-local refcounts for heap type objects
-        Py_ssize_t *refcounts;
+        // The per-thread refcounts
+        Py_ssize_t *values;
 
         // Size of the refcounts array.
         Py_ssize_t size;
 
-        // If set, don't use thread-local refcounts
+        // If set, don't use per-thread refcounts
         int is_finalized;
-    } types;
+    } refcounts;
 #endif
 
 #if defined(Py_REF_DEBUG) && defined(Py_GIL_DISABLED)
diff --git a/Include/internal/pycore_typeid.h b/Include/internal/pycore_typeid.h
deleted file mode 100644 (file)
index e64d144..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-#ifndef Py_INTERNAL_TYPEID_H
-#define Py_INTERNAL_TYPEID_H
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifndef Py_BUILD_CORE
-#  error "this header requires Py_BUILD_CORE define"
-#endif
-
-#ifdef Py_GIL_DISABLED
-
-// This contains code for allocating unique ids to heap type objects
-// and re-using those ids when the type is deallocated.
-//
-// The type ids are used to implement per-thread reference counts of
-// heap type objects to avoid contention on the reference count fields
-// of heap type objects. Static type objects are immortal, so contention
-// is not an issue for those types.
-//
-// Type id of -1 is used to indicate a type doesn't use thread-local
-// refcounting. This value is used when a type object is finalized by the GC
-// and during interpreter shutdown to allow the type object to be
-// deallocated promptly when the object's refcount reaches zero.
-//
-// Each entry implicitly represents a type id based on it's offset in the
-// table. Non-allocated entries form a free-list via the 'next' pointer.
-// Allocated entries store the corresponding PyTypeObject.
-typedef union _Py_type_id_entry {
-    // Points to the next free type id, when part of the freelist
-    union _Py_type_id_entry *next;
-
-    // Stores the type object when the id is assigned
-    PyHeapTypeObject *type;
-} _Py_type_id_entry;
-
-struct _Py_type_id_pool {
-    PyMutex mutex;
-
-    // combined table of types with allocated type ids and unallocated
-    // type ids.
-    _Py_type_id_entry *table;
-
-    // Next entry to allocate inside 'table' or NULL
-    _Py_type_id_entry *freelist;
-
-    // size of 'table'
-    Py_ssize_t size;
-};
-
-// Assigns the next id from the pool of type ids.
-extern void _PyType_AssignId(PyHeapTypeObject *type);
-
-// Releases the allocated type id back to the pool.
-extern void _PyType_ReleaseId(PyHeapTypeObject *type);
-
-// Merges the thread-local reference counts into the corresponding types.
-extern void _PyType_MergeThreadLocalRefcounts(_PyThreadStateImpl *tstate);
-
-// Like _PyType_MergeThreadLocalRefcounts, but also frees the thread-local
-// array of refcounts.
-extern void _PyType_FinalizeThreadLocalRefcounts(_PyThreadStateImpl *tstate);
-
-// Frees the interpreter's pool of type ids.
-extern void _PyType_FinalizeIdPool(PyInterpreterState *interp);
-
-// Increfs the type, resizing the thread-local refcount array if necessary.
-PyAPI_FUNC(void) _PyType_IncrefSlow(PyHeapTypeObject *type);
-
-#endif   /* Py_GIL_DISABLED */
-
-#ifdef __cplusplus
-}
-#endif
-#endif   /* !Py_INTERNAL_TYPEID_H */
diff --git a/Include/internal/pycore_uniqueid.h b/Include/internal/pycore_uniqueid.h
new file mode 100644 (file)
index 0000000..8f3b441
--- /dev/null
@@ -0,0 +1,72 @@
+#ifndef Py_INTERNAL_UNIQUEID_H
+#define Py_INTERNAL_UNIQUEID_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
+#ifdef Py_GIL_DISABLED
+
+// This contains code for allocating unique ids to objects for per-thread
+// reference counting.
+//
+// Per-thread reference counting is used along with deferred reference
+// counting to avoid scaling bottlenecks due to reference count contention.
+//
+// An id of -1 is used to indicate that an object doesn't use per-thread
+// refcounting. This value is used when the object is finalized by the GC
+// and during interpreter shutdown to allow the object to be
+// deallocated promptly when the object's refcount reaches zero.
+//
+// Each entry implicitly represents a unique id based on its offset in the
+// table. Non-allocated entries form a free-list via the 'next' pointer.
+// Allocated entries store the corresponding PyObject.
+typedef union _Py_unique_id_entry {
+    // Points to the next free type id, when part of the freelist
+    union _Py_unique_id_entry *next;
+
+    // Stores the object when the id is assigned
+    PyObject *obj;
+} _Py_unique_id_entry;
+
+struct _Py_unique_id_pool {
+    PyMutex mutex;
+
+    // combined table of object with allocated unique ids and unallocated ids.
+    _Py_unique_id_entry *table;
+
+    // Next entry to allocate inside 'table' or NULL
+    _Py_unique_id_entry *freelist;
+
+    // size of 'table'
+    Py_ssize_t size;
+};
+
+// Assigns the next id from the pool of ids.
+extern Py_ssize_t _PyObject_AssignUniqueId(PyObject *obj);
+
+// Releases the allocated id back to the pool.
+extern void _PyObject_ReleaseUniqueId(Py_ssize_t unique_id);
+
+// Merges the per-thread reference counts into the corresponding objects.
+extern void _PyObject_MergePerThreadRefcounts(_PyThreadStateImpl *tstate);
+
+// Like _PyObject_MergePerThreadRefcounts, but also frees the per-thread
+// array of refcounts.
+extern void _PyObject_FinalizePerThreadRefcounts(_PyThreadStateImpl *tstate);
+
+// Frees the interpreter's pool of type ids.
+extern void _PyObject_FinalizeUniqueIdPool(PyInterpreterState *interp);
+
+// Increfs the type, resizing the per-thread refcount array if necessary.
+PyAPI_FUNC(void) _PyType_IncrefSlow(PyHeapTypeObject *type);
+
+#endif   /* Py_GIL_DISABLED */
+
+#ifdef __cplusplus
+}
+#endif
+#endif   /* !Py_INTERNAL_UNIQUEID_H */
index a4d99262702a1769ec8d1e3cf067a3cc2600f62e..07c8a4d20142db9acc5908060598244d92c8dd8a 100644 (file)
@@ -490,7 +490,7 @@ PYTHON_OBJS=        \
                Python/thread.o \
                Python/traceback.o \
                Python/tracemalloc.o \
-               Python/typeid.o \
+               Python/uniqueid.o \
                Python/getopt.o \
                Python/pystrcmp.o \
                Python/pystrtod.o \
@@ -1279,7 +1279,7 @@ PYTHON_HEADERS= \
                $(srcdir)/Include/internal/pycore_tracemalloc.h \
                $(srcdir)/Include/internal/pycore_tstate.h \
                $(srcdir)/Include/internal/pycore_tuple.h \
-               $(srcdir)/Include/internal/pycore_typeid.h \
+               $(srcdir)/Include/internal/pycore_uniqueid.h \
                $(srcdir)/Include/internal/pycore_typeobject.h \
                $(srcdir)/Include/internal/pycore_typevarobject.h \
                $(srcdir)/Include/internal/pycore_ucnhash.h \
index 0e2d9758a5ffaeb5da9a534495e411e3a600222e..6484e8921f8122aabe893f1f952a1ced810574db 100644 (file)
@@ -3932,7 +3932,7 @@ type_new_alloc(type_new_ctx *ctx)
     et->ht_token = NULL;
 
 #ifdef Py_GIL_DISABLED
-    _PyType_AssignId(et);
+    et->unique_id = _PyObject_AssignUniqueId((PyObject *)et);
 #endif
 
     return type;
@@ -5026,7 +5026,7 @@ PyType_FromMetaclass(
 
 #ifdef Py_GIL_DISABLED
     // Assign a type id to enable thread-local refcounting
-    _PyType_AssignId(res);
+    res->unique_id = _PyObject_AssignUniqueId((PyObject *)res);
 #endif
 
     /* Ready the type (which includes inheritance).
@@ -6080,7 +6080,7 @@ type_dealloc(PyObject *self)
     Py_XDECREF(et->ht_module);
     PyMem_Free(et->_ht_tpname);
 #ifdef Py_GIL_DISABLED
-    _PyType_ReleaseId(et);
+    _PyObject_ReleaseUniqueId(et->unique_id);
 #endif
     et->ht_token = NULL;
     Py_TYPE(type)->tp_free((PyObject *)type);
index 743e6e2a66a8f1e3470be6f5ddb4d7a8f85576eb..a3c2d32c454e049b265c928a1d7c8e028927b1ba 100644 (file)
     <ClCompile Include="..\Python\thread.c" />
     <ClCompile Include="..\Python\traceback.c" />
     <ClCompile Include="..\Python\tracemalloc.c" />
-    <ClCompile Include="..\Python\typeid.c" />
+    <ClCompile Include="..\Python\uniqueid.c" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\PC\pyconfig.h.in" />
index 0887a47917a931899699f251181c76924ec594a7..91b1d75fb8df5ee9edd440013a7213f3d298fa8f 100644 (file)
     <ClCompile Include="..\Python\tracemalloc.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="..\Python\typeid.c">
+    <ClCompile Include="..\Python\uniqueid.c">
       <Filter>Source Files</Filter>
     </ClCompile>
     <ClCompile Include="..\Objects\tupleobject.c">
index 19b982db7f5b8775a89c57bc904d7c423cb10f8b..3b33c6bf6bb91d7327b275f09cbe5d02db2a037d 100644 (file)
     <ClInclude Include="..\Include\internal\pycore_tracemalloc.h" />
     <ClInclude Include="..\Include\internal\pycore_tstate.h" />
     <ClInclude Include="..\Include\internal\pycore_tuple.h" />
-    <ClInclude Include="..\Include\internal\pycore_typeid.h" />
     <ClInclude Include="..\Include\internal\pycore_typeobject.h" />
     <ClInclude Include="..\Include\internal\pycore_typevarobject.h" />
     <ClInclude Include="..\Include\internal\pycore_ucnhash.h" />
     <ClInclude Include="..\Include\internal\pycore_unionobject.h" />
     <ClInclude Include="..\Include\internal\pycore_unicodeobject.h" />
     <ClInclude Include="..\Include\internal\pycore_unicodeobject_generated.h" />
+    <ClInclude Include="..\Include\internal\pycore_uniqueid.h" />
     <ClInclude Include="..\Include\internal\pycore_warnings.h" />
     <ClInclude Include="..\Include\internal\pycore_weakref.h" />
     <ClInclude Include="..\Include\intrcheck.h" />
     <ClCompile Include="..\Python\thread.c" />
     <ClCompile Include="..\Python\traceback.c" />
     <ClCompile Include="..\Python\tracemalloc.c" />
-    <ClCompile Include="..\Python\typeid.c" />
+    <ClCompile Include="..\Python\uniqueid.c" />
   </ItemGroup>
   <ItemGroup Condition="$(IncludeExternals)">
     <ClCompile Include="..\Modules\zlibmodule.c" />
index 23f2e9c8bc0eb70e7ed31c0d31d55b85cf7102a9..ee2930b10439a983fc0ed36eb1077cd1cf692dbd 100644 (file)
     <ClInclude Include="..\Include\internal\pycore_tuple.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
-    <ClInclude Include="..\Include\internal\pycore_typeid.h">
-      <Filter>Include\internal</Filter>
-    </ClInclude>
     <ClInclude Include="..\Include\internal\pycore_typeobject.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
     <ClInclude Include="..\Include\internal\pycore_unionobject.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
+    <ClInclude Include="..\Include\internal\pycore_uniqueid.h">
+      <Filter>Include\internal</Filter>
+    </ClInclude>
     <ClInclude Include="..\Include\internal\mimalloc\mimalloc.h">
       <Filter>Include\internal\mimalloc</Filter>
     </ClInclude>
     <ClCompile Include="..\Python\tracemalloc.c">
       <Filter>Python</Filter>
     </ClCompile>
-    <ClCompile Include="..\Python\typeid.c">
+    <ClCompile Include="..\Python\uniqueid.c">
       <Filter>Python</Filter>
     </ClCompile>
     <ClCompile Include="..\Python\bootstrap_hash.c">
index a5bc9b9b5782b2bf23a8ec24e749e56cc69675af..38564d9d9b0058a491938fea64170968a70263d7 100644 (file)
@@ -15,7 +15,7 @@
 #include "pycore_tstate.h"        // _PyThreadStateImpl
 #include "pycore_weakref.h"       // _PyWeakref_ClearRef()
 #include "pydtrace.h"
-#include "pycore_typeid.h"        // _PyType_MergeThreadLocalRefcounts
+#include "pycore_uniqueid.h"      // _PyType_MergeThreadLocalRefcounts
 
 #ifdef Py_GIL_DISABLED
 
@@ -217,12 +217,12 @@ disable_deferred_refcounting(PyObject *op)
         merge_refcount(op, 0);
     }
 
-    // Heap types also use thread-local refcounting -- disable it here.
+    // Heap types also use per-thread refcounting -- disable it here.
     if (PyType_Check(op)) {
-        // Disable thread-local refcounting for heap types
-        PyTypeObject *type = (PyTypeObject *)op;
-        if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
-            _PyType_ReleaseId((PyHeapTypeObject *)op);
+        if (PyType_HasFeature((PyTypeObject *)op, Py_TPFLAGS_HEAPTYPE)) {
+            PyHeapTypeObject *ht = (PyHeapTypeObject *)op;
+            _PyObject_ReleaseUniqueId(ht->unique_id);
+            ht->unique_id = -1;
         }
     }
 
@@ -1221,7 +1221,7 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,
         _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)p;
 
         // merge per-thread refcount for types into the type's actual refcount
-        _PyType_MergeThreadLocalRefcounts(tstate);
+        _PyObject_MergePerThreadRefcounts(tstate);
 
         // merge refcounts for all queued objects
         merge_queued_objects(tstate, state);
index 8aebbe5c405ffe0d2dbd2cdd980794a8fc3521ca..d9e89edf5ddc9e04c677c60b4813a1819ac06997 100644 (file)
@@ -28,7 +28,7 @@
 #include "pycore_sliceobject.h"   // _PySlice_Fini()
 #include "pycore_sysmodule.h"     // _PySys_ClearAuditHooks()
 #include "pycore_traceback.h"     // _Py_DumpTracebackThreads()
-#include "pycore_typeid.h"        // _PyType_FinalizeIdPool()
+#include "pycore_uniqueid.h"      // _PyType_FinalizeIdPool()
 #include "pycore_typeobject.h"    // _PyTypes_InitTypes()
 #include "pycore_typevarobject.h" // _Py_clear_generic_types()
 #include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
@@ -1834,7 +1834,7 @@ finalize_interp_types(PyInterpreterState *interp)
 
     _PyTypes_Fini(interp);
 #ifdef Py_GIL_DISABLED
-    _PyType_FinalizeIdPool(interp);
+    _PyObject_FinalizeUniqueIdPool(interp);
 #endif
 
     _PyCode_Fini(interp);
index 6b85e5a64fefcfb032a62ab278ada4cca23fbd44..9d11e2d25493146c8fe0472b53dd81d7f065c81c 100644 (file)
@@ -20,7 +20,7 @@
 #include "pycore_runtime_init.h"  // _PyRuntimeState_INIT
 #include "pycore_sysmodule.h"     // _PySys_Audit()
 #include "pycore_obmalloc.h"      // _PyMem_obmalloc_state_on_heap()
-#include "pycore_typeid.h"        // _PyType_FinalizeThreadLocalRefcounts()
+#include "pycore_uniqueid.h"      // _PyType_FinalizeThreadLocalRefcounts()
 
 /* --------------------------------------------------------------------------
 CAUTION
@@ -1745,7 +1745,7 @@ PyThreadState_Clear(PyThreadState *tstate)
 
     // Merge our thread-local refcounts into the type's own refcount and
     // free our local refcount array.
-    _PyType_FinalizeThreadLocalRefcounts((_PyThreadStateImpl *)tstate);
+    _PyObject_FinalizePerThreadRefcounts((_PyThreadStateImpl *)tstate);
 
     // Remove ourself from the biased reference counting table of threads.
     _Py_brc_remove_thread(tstate);
@@ -1805,7 +1805,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil)
     _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
     tstate->interp->object_state.reftotal += tstate_impl->reftotal;
     tstate_impl->reftotal = 0;
-    assert(tstate_impl->types.refcounts == NULL);
+    assert(tstate_impl->refcounts.values == NULL);
 #endif
 
     HEAD_UNLOCK(runtime);
similarity index 51%
rename from Python/typeid.c
rename to Python/uniqueid.c
index 83a68723ded61b66d66c9b38a1cdf58b0ee7fce0..9a9ee2f39467b0773450eddd410d105688b32a5a 100644 (file)
@@ -3,12 +3,14 @@
 #include "pycore_lock.h"        // PyMutex_LockFlags()
 #include "pycore_pystate.h"     // _PyThreadState_GET()
 #include "pycore_object.h"      // _Py_IncRefTotal
-#include "pycore_typeid.h"
+#include "pycore_uniqueid.h"
 
-// This contains code for allocating unique ids to heap type objects
-// and re-using those ids when the type is deallocated.
+// This contains code for allocating unique ids for per-thread reference
+// counting and re-using those ids when an object is deallocated.
 //
-// See Include/internal/pycore_typeid.h for more details.
+// Currently, per-thread reference counting is only used for heap types.
+//
+// See Include/internal/pycore_uniqueid.h for more details.
 
 #ifdef Py_GIL_DISABLED
 
@@ -18,7 +20,7 @@
 #define UNLOCK_POOL(pool) PyMutex_Unlock(&pool->mutex)
 
 static int
-resize_interp_type_id_pool(struct _Py_type_id_pool *pool)
+resize_interp_type_id_pool(struct _Py_unique_id_pool *pool)
 {
     if ((size_t)pool->size > PY_SSIZE_T_MAX / (2 * sizeof(*pool->table))) {
         return -1;
@@ -29,8 +31,8 @@ resize_interp_type_id_pool(struct _Py_type_id_pool *pool)
         new_size = POOL_MIN_SIZE;
     }
 
-    _Py_type_id_entry *table = PyMem_Realloc(pool->table,
-                                             new_size * sizeof(*pool->table));
+    _Py_unique_id_entry *table = PyMem_Realloc(pool->table,
+                                               new_size * sizeof(*pool->table));
     if (table == NULL) {
         return -1;
     }
@@ -50,70 +52,67 @@ resize_interp_type_id_pool(struct _Py_type_id_pool *pool)
 static int
 resize_local_refcounts(_PyThreadStateImpl *tstate)
 {
-    if (tstate->types.is_finalized) {
+    if (tstate->refcounts.is_finalized) {
         return -1;
     }
 
-    struct _Py_type_id_pool *pool = &tstate->base.interp->type_ids;
+    struct _Py_unique_id_pool *pool = &tstate->base.interp->unique_ids;
     Py_ssize_t size = _Py_atomic_load_ssize(&pool->size);
 
-    Py_ssize_t *refcnts = PyMem_Realloc(tstate->types.refcounts,
+    Py_ssize_t *refcnts = PyMem_Realloc(tstate->refcounts.values,
                                         size * sizeof(Py_ssize_t));
     if (refcnts == NULL) {
         return -1;
     }
 
-    Py_ssize_t old_size = tstate->types.size;
+    Py_ssize_t old_size = tstate->refcounts.size;
     if (old_size < size) {
        memset(refcnts + old_size, 0, (size - old_size) * sizeof(Py_ssize_t));
     }
 
-    tstate->types.refcounts = refcnts;
-    tstate->types.size = size;
+    tstate->refcounts.values = refcnts;
+    tstate->refcounts.size = size;
     return 0;
 }
 
-void
-_PyType_AssignId(PyHeapTypeObject *type)
+Py_ssize_t
+_PyObject_AssignUniqueId(PyObject *obj)
 {
     PyInterpreterState *interp = _PyInterpreterState_GET();
-    struct _Py_type_id_pool *pool = &interp->type_ids;
+    struct _Py_unique_id_pool *pool = &interp->unique_ids;
 
     LOCK_POOL(pool);
     if (pool->freelist == NULL) {
         if (resize_interp_type_id_pool(pool) < 0) {
-            type->unique_id = -1;
             UNLOCK_POOL(pool);
-            return;
+            return -1;
         }
     }
 
-    _Py_type_id_entry *entry = pool->freelist;
+    _Py_unique_id_entry *entry = pool->freelist;
     pool->freelist = entry->next;
-    entry->type = type;
-    _PyObject_SetDeferredRefcount((PyObject *)type);
-    type->unique_id = (entry - pool->table);
+    entry->obj = obj;
+    _PyObject_SetDeferredRefcount(obj);
+    Py_ssize_t unique_id = (entry - pool->table);
     UNLOCK_POOL(pool);
+    return unique_id;
 }
 
 void
-_PyType_ReleaseId(PyHeapTypeObject *type)
+_PyObject_ReleaseUniqueId(Py_ssize_t unique_id)
 {
     PyInterpreterState *interp = _PyInterpreterState_GET();
-    struct _Py_type_id_pool *pool = &interp->type_ids;
+    struct _Py_unique_id_pool *pool = &interp->unique_ids;
 
-    if (type->unique_id < 0) {
-        // The type doesn't have an id assigned.
+    if (unique_id < 0) {
+        // The id is not assigned
         return;
     }
 
     LOCK_POOL(pool);
-    _Py_type_id_entry *entry = &pool->table[type->unique_id];
-    assert(entry->type == type);
+    _Py_unique_id_entry *entry = &pool->table[unique_id];
     entry->next = pool->freelist;
     pool->freelist = entry;
-
-    type->unique_id = -1;
     UNLOCK_POOL(pool);
 }
 
@@ -127,8 +126,8 @@ _PyType_IncrefSlow(PyHeapTypeObject *type)
         return;
     }
 
-    assert(type->unique_id < tstate->types.size);
-    tstate->types.refcounts[type->unique_id]++;
+    assert(type->unique_id < tstate->refcounts.size);
+    tstate->refcounts.values[type->unique_id]++;
 #ifdef Py_REF_DEBUG
     _Py_IncRefTotal((PyThreadState *)tstate);
 #endif
@@ -136,59 +135,64 @@ _PyType_IncrefSlow(PyHeapTypeObject *type)
 }
 
 void
-_PyType_MergeThreadLocalRefcounts(_PyThreadStateImpl *tstate)
+_PyObject_MergePerThreadRefcounts(_PyThreadStateImpl *tstate)
 {
-    if (tstate->types.refcounts == NULL) {
+    if (tstate->refcounts.values == NULL) {
         return;
     }
 
-    struct _Py_type_id_pool *pool = &tstate->base.interp->type_ids;
+    struct _Py_unique_id_pool *pool = &tstate->base.interp->unique_ids;
 
     LOCK_POOL(pool);
-    for (Py_ssize_t i = 0, n = tstate->types.size; i < n; i++) {
-        Py_ssize_t refcnt = tstate->types.refcounts[i];
+    for (Py_ssize_t i = 0, n = tstate->refcounts.size; i < n; i++) {
+        Py_ssize_t refcnt = tstate->refcounts.values[i];
         if (refcnt != 0) {
-            PyObject *type = (PyObject *)pool->table[i].type;
-            assert(PyType_Check(type));
-
-            _Py_atomic_add_ssize(&type->ob_ref_shared,
+            PyObject *obj = pool->table[i].obj;
+            _Py_atomic_add_ssize(&obj->ob_ref_shared,
                                  refcnt << _Py_REF_SHARED_SHIFT);
-            tstate->types.refcounts[i] = 0;
+            tstate->refcounts.values[i] = 0;
         }
     }
     UNLOCK_POOL(pool);
 }
 
 void
-_PyType_FinalizeThreadLocalRefcounts(_PyThreadStateImpl *tstate)
+_PyObject_FinalizePerThreadRefcounts(_PyThreadStateImpl *tstate)
 {
-    _PyType_MergeThreadLocalRefcounts(tstate);
+    _PyObject_MergePerThreadRefcounts(tstate);
 
-    PyMem_Free(tstate->types.refcounts);
-    tstate->types.refcounts = NULL;
-    tstate->types.size = 0;
-    tstate->types.is_finalized = 1;
+    PyMem_Free(tstate->refcounts.values);
+    tstate->refcounts.values = NULL;
+    tstate->refcounts.size = 0;
+    tstate->refcounts.is_finalized = 1;
 }
 
 void
-_PyType_FinalizeIdPool(PyInterpreterState *interp)
+_PyObject_FinalizeUniqueIdPool(PyInterpreterState *interp)
 {
-    struct _Py_type_id_pool *pool = &interp->type_ids;
+    struct _Py_unique_id_pool *pool = &interp->unique_ids;
 
     // First, set the free-list to NULL values
     while (pool->freelist) {
-        _Py_type_id_entry *next = pool->freelist->next;
-        pool->freelist->type = NULL;
+        _Py_unique_id_entry *next = pool->freelist->next;
+        pool->freelist->obj = NULL;
         pool->freelist = next;
     }
 
     // Now everything non-NULL is a type. Set the type's id to -1 in case it
     // outlives the interpreter.
     for (Py_ssize_t i = 0; i < pool->size; i++) {
-        PyHeapTypeObject *ht = pool->table[i].type;
-        if (ht) {
-            ht->unique_id = -1;
-            pool->table[i].type = NULL;
+        PyObject *obj = pool->table[i].obj;
+        pool->table[i].obj = NULL;
+        if (obj == NULL) {
+            continue;
+        }
+        if (PyType_Check(obj)) {
+            assert(PyType_HasFeature((PyTypeObject *)obj, Py_TPFLAGS_HEAPTYPE));
+            ((PyHeapTypeObject *)obj)->unique_id = -1;
+        }
+        else {
+            Py_UNREACHABLE();
         }
     }
     PyMem_Free(pool->table);