forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-122417: Implement per-thread heap type refcounts (python#122418
) The free-threaded build partially stores heap type reference counts in distributed manner in per-thread arrays. This avoids reference count contention when creating or destroying instances. Co-authored-by: Ken Jin <kenjin@python.org>
- Loading branch information
Showing
18 changed files
with
427 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Core_and_Builtins/2024-07-29-19-20-25.gh-issue-122417.NVgs0a.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
In the free-threaded build, the reference counts for heap type objects are now | ||
partially stored in a distributed manner in per-thread arrays. This reduces | ||
contention on the heap type's reference count fields when creating or | ||
destroying instances of the same type from multiple threads concurrently. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.