GeezerVison is showing github.com — gh-108634: Py_TRACE_REFS uses a hash table (#108663) · 2union/ircpython@13a0007
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Doc/c-api/typeobj.rst -22Lines changed: 0 additions & 22 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -528,28 +528,6 @@ type objects) *must* have the :c:member:`~PyVarObject.ob_size` field. | 528 | 528 | This field is inherited by subtypes. | 529 | 529 |
| 530 | 530 |
| 531 | | -.. c:member:: PyObject* PyObject._ob_next | 532 | | - PyObject* PyObject._ob_prev | 533 | | -
| 534 | | - These fields are only present when the macro ``Py_TRACE_REFS`` is defined | 535 | | - (see the :option:`configure --with-trace-refs option <--with-trace-refs>`). | 536 | | - | 537 | | - Their initialization to ``NULL`` is taken care of by the | 538 | | - ``PyObject_HEAD_INIT`` macro. For :ref:`statically allocated objects | 539 | | - <static-types>`, these fields always remain ``NULL``. For :ref:`dynamically | 540 | | - allocated objects <heap-types>`, these two fields are used to link the | 541 | | - object into a doubly linked list of *all* live objects on the heap. | 542 | | - | 543 | | - This could be used for various debugging purposes; currently the only uses | 544 | | - are the :func:`sys.getobjects` function and to print the objects that are | 545 | | - still alive at the end of a run when the environment variable | 546 | | - :envvar:`PYTHONDUMPREFS` is set. | 547 | | - | 548 | | - **Inheritance:** | 549 | | - | 550 | | - These fields are not inherited by subtypes. | 551 | | - | 552 | | - | 553 | 531 | PyVarObject Slots | 554 | 532 | ----------------- | 555 | 533 |
| |
Collapse file Copy file name to clipboardExpand all lines: Doc/using/configure.rst +9-4Lines changed: 9 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -425,8 +425,7 @@ See also the :ref:`Python Development Mode <devmode>` and the | 425 | 425 | .. versionchanged:: 3.8 | 426 | 426 | Release builds and debug builds are now ABI compatible: defining the | 427 | 427 | ``Py_DEBUG`` macro no longer implies the ``Py_TRACE_REFS`` macro (see the | 428 | | - :option:`--with-trace-refs` option), which introduces the only ABI | 429 | | - incompatibility. | | 428 | + :option:`--with-trace-refs` option). | 430 | 429 |
| 431 | 430 |
| 432 | 431 | Debug options | @@ -447,8 +446,14 @@ Debug options | 447 | 446 | * Add :func:`sys.getobjects` function. | 448 | 447 | * Add :envvar:`PYTHONDUMPREFS` environment variable. | 449 | 448 |
| 450 | | - This build is not ABI compatible with release build (default build) or debug | 451 | | - build (``Py_DEBUG`` and ``Py_REF_DEBUG`` macros). | | 449 | + The :envvar:`PYTHONDUMPREFS` environment variable can be used to dump | | 450 | + objects and reference counts still alive at Python exit. | | 451 | + | | 452 | + :ref:`Statically allocated objects <static-types>` are not traced. | | 453 | + | | 454 | + .. versionchanged:: 3.13 | | 455 | + This build is now ABI compatible with release build and :ref:`debug build | | 456 | + <debug-build>`. | 452 | 457 |
| 453 | 458 | .. versionadded:: 3.8 | 454 | 459 |
| |
Collapse file Copy file name to clipboardExpand all lines: Doc/whatsnew/3.13.rst +9Lines changed: 9 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -828,6 +828,11 @@ Build Changes | 828 | 828 | * SQLite 3.15.2 or newer is required to build the :mod:`sqlite3` extension module. | 829 | 829 | (Contributed by Erlend Aasland in :gh:`105875`.) | 830 | 830 |
| | 831 | +* Python built with :file:`configure` :option:`--with-trace-refs` (tracing | | 832 | + references) is now ABI compatible with Python release build and | | 833 | + :ref:`debug build <debug-build>`. | | 834 | + (Contributed by Victor Stinner in :gh:`108634`.) | | 835 | + | 831 | 836 |
| 832 | 837 | C API Changes | 833 | 838 | ============= | @@ -900,6 +905,10 @@ New Features | 900 | 905 | (with an underscore prefix). | 901 | 906 | (Contributed by Victor Stinner in :gh:`108014`.) | 902 | 907 |
| | 908 | +* Python built with :file:`configure` :option:`--with-trace-refs` (tracing | | 909 | + references) now supports the :ref:`Limited API <limited-c-api>`. | | 910 | + (Contributed by Victor Stinner in :gh:`108634`.) | | 911 | + | 903 | 912 | Porting to Python 3.13 | 904 | 913 | ---------------------- | 905 | 914 |
| |
Collapse file Copy file name to clipboardExpand all lines: Include/internal/pycore_object.h +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,6 @@ PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *); | 55 | 55 | backwards compatible solution */ | 56 | 56 | #define _PyObject_HEAD_INIT(type) \ | 57 | 57 | { \ | 58 | | - _PyObject_EXTRA_INIT \ | 59 | 58 | .ob_refcnt = _Py_IMMORTAL_REFCNT, \ | 60 | 59 | .ob_type = (type) \ | 61 | 60 | }, | @@ -184,6 +183,8 @@ _PyType_HasFeature(PyTypeObject *type, unsigned long feature) { | 184 | 183 | extern void _PyType_InitCache(PyInterpreterState *interp); | 185 | 184 |
| 186 | 185 | extern void _PyObject_InitState(PyInterpreterState *interp); | | 186 | +extern void _PyObject_FiniState(PyInterpreterState *interp); | | 187 | +extern bool _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj); | 187 | 188 |
| 188 | 189 | /* Inline functions trading binary compatibility for speed: | 189 | 190 | _PyObject_Init() is the fast version of PyObject_Init(), and | @@ -302,7 +303,7 @@ extern void _PyDebug_PrintTotalRefs(void); | 302 | 303 | #endif | 303 | 304 |
| 304 | 305 | #ifdef Py_TRACE_REFS | 305 | | -extern void _Py_AddToAllObjects(PyObject *op, int force); | | 306 | +extern void _Py_AddToAllObjects(PyObject *op); | 306 | 307 | extern void _Py_PrintReferences(PyInterpreterState *, FILE *); | 307 | 308 | extern void _Py_PrintReferenceAddresses(PyInterpreterState *, FILE *); | 308 | 309 | #endif | |
Collapse file Copy file name to clipboardExpand all lines: Include/internal/pycore_object_state.h +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,8 @@ extern "C" { | 8 | 8 | # error "this header requires Py_BUILD_CORE define" | 9 | 9 | #endif | 10 | 10 |
| | 11 | +#include "pycore_hashtable.h" // _Py_hashtable_t | | 12 | + | 11 | 13 | struct _py_object_runtime_state { | 12 | 14 | #ifdef Py_REF_DEBUG | 13 | 15 | Py_ssize_t interpreter_leaks; | @@ -20,11 +22,10 @@ struct _py_object_state { | 20 | 22 | Py_ssize_t reftotal; | 21 | 23 | #endif | 22 | 24 | #ifdef Py_TRACE_REFS | 23 | | - /* Head of circular doubly-linked list of all objects. These are linked | 24 | | - * together via the _ob_prev and _ob_next members of a PyObject, which | 25 | | - * exist only in a Py_TRACE_REFS build. | 26 | | - */ | 27 | | - PyObject refchain; | | 25 | + // Hash table storing all objects. The key is the object pointer | | 26 | + // (PyObject*) and the value is always the number 1 (as uintptr_t). | | 27 | + // See _PyRefchain_IsTraced() and _PyRefchain_Trace() functions. | | 28 | + _Py_hashtable_t *refchain; | 28 | 29 | #endif | 29 | 30 | int _not_used; | 30 | 31 | }; | |
Collapse file Copy file name to clipboardExpand all lines: Include/internal/pycore_runtime_init.h +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -192,7 +192,7 @@ extern PyTypeObject _PyExc_MemoryError; | 192 | 192 | #ifdef Py_TRACE_REFS | 193 | 193 | # define _py_object_state_INIT(INTERP) \ | 194 | 194 | { \ | 195 | | - .refchain = {&INTERP.object_state.refchain, &INTERP.object_state.refchain}, \ | | 195 | + .refchain = NULL, \ | 196 | 196 | } | 197 | 197 | #else | 198 | 198 | # define _py_object_state_INIT(INTERP) \ | |
Collapse file Copy file name to clipboardExpand all lines: Include/modsupport.h -8Lines changed: 0 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -111,14 +111,6 @@ PyAPI_FUNC(int) PyModule_ExecDef(PyObject *module, PyModuleDef *def); | 111 | 111 | #define PYTHON_ABI_VERSION 3 | 112 | 112 | #define PYTHON_ABI_STRING "3" | 113 | 113 |
| 114 | | -#ifdef Py_TRACE_REFS | 115 | | - /* When we are tracing reference counts, rename module creation functions so | 116 | | - modules compiled with incompatible settings will generate a | 117 | | - link-time error. */ | 118 | | - #define PyModule_Create2 PyModule_Create2TraceRefs | 119 | | - #define PyModule_FromDefAndSpec2 PyModule_FromDefAndSpec2TraceRefs | 120 | | -#endif | 121 | | - | 122 | 114 | PyAPI_FUNC(PyObject *) PyModule_Create2(PyModuleDef*, int apiver); | 123 | 115 |
| 124 | 116 | #ifdef Py_LIMITED_API | |
Collapse file Copy file name to clipboardExpand all lines: Include/object.h -21Lines changed: 0 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,23 +58,6 @@ whose size is determined when the object is allocated. | 58 | 58 | # define Py_REF_DEBUG | 59 | 59 | #endif | 60 | 60 |
| 61 | | -#if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS) | 62 | | -# error Py_LIMITED_API is incompatible with Py_TRACE_REFS | 63 | | -#endif | 64 | | - | 65 | | -#ifdef Py_TRACE_REFS | 66 | | -/* Define pointers to support a doubly-linked list of all live heap objects. */ | 67 | | -#define _PyObject_HEAD_EXTRA \ | 68 | | - PyObject *_ob_next; \ | 69 | | - PyObject *_ob_prev; | 70 | | - | 71 | | -#define _PyObject_EXTRA_INIT _Py_NULL, _Py_NULL, | 72 | | - | 73 | | -#else | 74 | | -# define _PyObject_HEAD_EXTRA | 75 | | -# define _PyObject_EXTRA_INIT | 76 | | -#endif | 77 | | - | 78 | 61 | /* PyObject_HEAD defines the initial segment of every PyObject. */ | 79 | 62 | #define PyObject_HEAD PyObject ob_base; | 80 | 63 |
| @@ -130,14 +113,12 @@ check by comparing the reference count field to the immortality reference count. | 130 | 113 | #ifdef Py_BUILD_CORE | 131 | 114 | #define PyObject_HEAD_INIT(type) \ | 132 | 115 | { \ | 133 | | - _PyObject_EXTRA_INIT \ | 134 | 116 | { _Py_IMMORTAL_REFCNT }, \ | 135 | 117 | (type) \ | 136 | 118 | }, | 137 | 119 | #else | 138 | 120 | #define PyObject_HEAD_INIT(type) \ | 139 | 121 | { \ | 140 | | - _PyObject_EXTRA_INIT \ | 141 | 122 | { 1 }, \ | 142 | 123 | (type) \ | 143 | 124 | }, | @@ -164,8 +145,6 @@ check by comparing the reference count field to the immortality reference count. | 164 | 145 | * in addition, be cast to PyVarObject*. | 165 | 146 | */ | 166 | 147 | struct _object { | 167 | | - _PyObject_HEAD_EXTRA | 168 | | - | 169 | 148 | #if (defined(__GNUC__) || defined(__clang__)) \ | 170 | 149 | && !(defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L) | 171 | 150 | // On C99 and older, anonymous union is a GCC and clang extension | |
Collapse file Copy file name to clipboardExpand all lines: Include/pyport.h -6Lines changed: 0 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -684,12 +684,6 @@ extern char * _getpty(int *, int, mode_t, int); | 684 | 684 | # endif | 685 | 685 | #endif | 686 | 686 |
| 687 | | -/* Check that ALT_SOABI is consistent with Py_TRACE_REFS: | 688 | | - ./configure --with-trace-refs should must be used to define Py_TRACE_REFS */ | 689 | | -#if defined(ALT_SOABI) && defined(Py_TRACE_REFS) | 690 | | -# error "Py_TRACE_REFS ABI is not compatible with release and debug ABI" | 691 | | -#endif | 692 | | - | 693 | 687 | #if defined(__ANDROID__) || defined(__VXWORKS__) | 694 | 688 | // Use UTF-8 as the locale encoding, ignore the LC_CTYPE locale. | 695 | 689 | // See _Py_GetLocaleEncoding(), PyUnicode_DecodeLocale() | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/support/__init__.py +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -779,9 +779,6 @@ def python_is_optimized(): | 779 | 779 |
| 780 | 780 | _header = 'nP' | 781 | 781 | _align = '0n' | 782 | | -if hasattr(sys, "getobjects"): | 783 | | - _header = '2P' + _header | 784 | | - _align = '0P' | 785 | 782 | _vheader = _header + 'n' | 786 | 783 |
| 787 | 784 | def calcobjsize(fmt): | @@ -2469,3 +2466,5 @@ def adjust_int_max_str_digits(max_digits): | 2469 | 2466 | #Windows doesn't have os.uname() but it doesn't support s390x. | 2470 | 2467 | skip_on_s390x = unittest.skipIf(hasattr(os, 'uname') and os.uname().machine == 's390x', | 2471 | 2468 | 'skipped on s390x') | | 2469 | + | | 2470 | +Py_TRACE_REFS = hasattr(sys, 'getobjects') |
|