@@ -10,61 +10,58 @@ extern "C" {
1010
1111#ifdef Py_GIL_DISABLED
1212
13- // This contains code for allocating unique ids to heap type objects
14- // and re-using those ids when the type is deallocated .
13+ // This contains code for allocating unique ids to objects for
14+ // per-thread reference counting .
1515//
16- // The type ids are used to implement per-thread reference counts of
17- // heap type objects to avoid contention on the reference count fields
18- // of heap type objects. Static type objects are immortal, so contention
19- // is not an issue for those types.
16+ // Per-thread reference counting is used along with deferred reference
17+ // counting to avoid scaling bottlenecks due to reference count contention.
2018//
21- // Type id of -1 is used to indicate a type doesn't use thread-local
22- // refcounting. This value is used when a type object is finalized by the GC
23- // and during interpreter shutdown to allow the type object to be
19+ // An id of -1 is used to indicate that an object doesn't use per-thread
20+ // refcounting. This value is used when the object is finalized by the GC
21+ // and during interpreter shutdown to allow the object to be
2422// deallocated promptly when the object's refcount reaches zero.
2523//
26- // Each entry implicitly represents a type id based on it's offset in the
24+ // Each entry implicitly represents a unique id based on its offset in the
2725// table. Non-allocated entries form a free-list via the 'next' pointer.
28- // Allocated entries store the corresponding PyTypeObject .
29- typedef union _Py_type_id_entry {
26+ // Allocated entries store the corresponding PyObject .
27+ typedef union _Py_unique_id_entry {
3028 // Points to the next free type id, when part of the freelist
31- union _Py_type_id_entry * next ;
29+ union _Py_unique_id_entry * next ;
3230
33- // Stores the type object when the id is assigned
34- PyHeapTypeObject * type ;
35- } _Py_type_id_entry ;
31+ // Stores the object when the id is assigned
32+ PyObject * obj ;
33+ } _Py_unique_id_entry ;
3634
37- struct _Py_type_id_pool {
35+ struct _Py_unique_id_pool {
3836 PyMutex mutex ;
3937
40- // combined table of types with allocated type ids and unallocated
41- // type ids.
42- _Py_type_id_entry * table ;
38+ // combined table of object with allocated unique ids and unallocated ids.
39+ _Py_unique_id_entry * table ;
4340
4441 // Next entry to allocate inside 'table' or NULL
45- _Py_type_id_entry * freelist ;
42+ _Py_unique_id_entry * freelist ;
4643
4744 // size of 'table'
4845 Py_ssize_t size ;
4946};
5047
51- // Assigns the next id from the pool of type ids.
52- extern void _PyType_AssignId ( PyHeapTypeObject * type );
48+ // Assigns the next id from the pool of ids.
49+ extern Py_ssize_t _PyObject_AssignUniqueId ( PyObject * obj );
5350
54- // Releases the allocated type id back to the pool.
55- extern void _PyType_ReleaseId ( PyHeapTypeObject * type );
51+ // Releases the allocated id back to the pool.
52+ extern void _PyObject_ReleaseUniqueId ( Py_ssize_t unique_id );
5653
57- // Merges the thread-local reference counts into the corresponding types .
58- extern void _PyType_MergeThreadLocalRefcounts (_PyThreadStateImpl * tstate );
54+ // Merges the per-thread reference counts into the corresponding objects .
55+ extern void _PyObject_MergePerThreadRefcounts (_PyThreadStateImpl * tstate );
5956
60- // Like _PyType_MergeThreadLocalRefcounts , but also frees the thread-local
57+ // Like _PyObject_MergePerThreadRefcounts , but also frees the per-thread
6158// array of refcounts.
62- extern void _PyType_FinalizeThreadLocalRefcounts (_PyThreadStateImpl * tstate );
59+ extern void _PyObject_FinalizePerThreadRefcounts (_PyThreadStateImpl * tstate );
6360
6461// Frees the interpreter's pool of type ids.
65- extern void _PyType_FinalizeIdPool (PyInterpreterState * interp );
62+ extern void _PyObject_FinalizeUniqueIdPool (PyInterpreterState * interp );
6663
67- // Increfs the type, resizing the thread-local refcount array if necessary.
64+ // Increfs the type, resizing the per-thread refcount array if necessary.
6865PyAPI_FUNC (void ) _PyType_IncrefSlow (PyHeapTypeObject * type );
6966
7067#endif /* Py_GIL_DISABLED */
0 commit comments