From 41c621827a42dbd63991ffa312ba8663b1414571 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 4 Jul 2024 01:50:58 +0800 Subject: [PATCH] remove all temporary immortalization --- Include/internal/pycore_gc.h | 8 ----- Modules/_testinternalcapi.c | 14 -------- Objects/codeobject.c | 65 ------------------------------------ Python/bltinmodule.c | 13 -------- Python/gc_free_threading.c | 4 --- 5 files changed, 104 deletions(-) diff --git a/Include/internal/pycore_gc.h b/Include/internal/pycore_gc.h index d2460a8f083562..498c60418762cb 100644 --- a/Include/internal/pycore_gc.h +++ b/Include/internal/pycore_gc.h @@ -344,14 +344,6 @@ struct _gc_runtime_state { collections, and are awaiting to undergo a full collection for the first time. */ Py_ssize_t long_lived_pending; - - /* gh-117783: Deferred reference counting is not fully implemented yet, so - as a temporary measure we treat objects using deferred reference - counting as immortal. The value may be zero, one, or a negative number: - 0: immortalize deferred RC objects once the first thread is created - 1: immortalize all deferred RC objects immediately - <0: suppressed; don't immortalize objects */ - int immortalize; #endif }; diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 6e6386bc044dc3..4a72105cbad100 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -1968,27 +1968,13 @@ get_py_thread_id(PyObject *self, PyObject *Py_UNUSED(ignored)) static PyObject * suppress_immortalization(PyObject *self, PyObject *value) { -#ifdef Py_GIL_DISABLED - int suppress = PyObject_IsTrue(value); - if (suppress < 0) { - return NULL; - } - PyInterpreterState *interp = PyInterpreterState_Get(); - // Subtract two to suppress immortalization (so that 1 -> -1) - _Py_atomic_add_int(&interp->gc.immortalize, suppress ? -2 : 2); -#endif Py_RETURN_NONE; } static PyObject * get_immortalize_deferred(PyObject *self, PyObject *Py_UNUSED(ignored)) { -#ifdef Py_GIL_DISABLED - PyInterpreterState *interp = PyInterpreterState_Get(); - return PyBool_FromLong(_Py_atomic_load_int(&interp->gc.immortalize) >= 0); -#else Py_RETURN_FALSE; -#endif } static PyObject * diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 7493280c898750..d027c505929903 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -105,16 +105,6 @@ PyCode_ClearWatcher(int watcher_id) static int should_intern_string(PyObject *o) { -#ifdef Py_GIL_DISABLED - // The free-threaded build interns (and immortalizes) all string constants - // unless we've disabled immortalizing objects that use deferred reference - // counting. - PyInterpreterState *interp = _PyInterpreterState_GET(); - if (_Py_atomic_load_int(&interp->gc.immortalize) < 0) { - return 1; - } -#endif - // compute if s matches [a-zA-Z0-9_] const unsigned char *s, *e; @@ -130,10 +120,6 @@ should_intern_string(PyObject *o) return 1; } -#ifdef Py_GIL_DISABLED -static PyObject *intern_one_constant(PyObject *op); -#endif - static int intern_strings(PyObject *tuple) { @@ -235,27 +221,6 @@ intern_constants(PyObject *tuple, int *modified) } Py_DECREF(tmp); } - - // Intern non-string constants in the free-threaded build, but only if - // we are also immortalizing objects that use deferred reference - // counting. - PyThreadState *tstate = PyThreadState_GET(); - if (!_Py_IsImmortal(v) && !PyCode_Check(v) && - !PyUnicode_CheckExact(v) && - _Py_atomic_load_int(&tstate->interp->gc.immortalize) >= 0) - { - PyObject *interned = intern_one_constant(v); - if (interned == NULL) { - return -1; - } - else if (interned != v) { - PyTuple_SET_ITEM(tuple, i, interned); - Py_SETREF(v, interned); - if (modified) { - *modified = 1; - } - } - } #endif } return 0; @@ -2495,36 +2460,6 @@ _PyCode_ConstantKey(PyObject *op) } #ifdef Py_GIL_DISABLED -static PyObject * -intern_one_constant(PyObject *op) -{ - PyInterpreterState *interp = _PyInterpreterState_GET(); - _Py_hashtable_t *consts = interp->code_state.constants; - - assert(!PyUnicode_CheckExact(op)); // strings are interned separately - - _Py_hashtable_entry_t *entry = _Py_hashtable_get_entry(consts, op); - if (entry == NULL) { - if (_Py_hashtable_set(consts, op, op) != 0) { - return NULL; - } - -#ifdef Py_REF_DEBUG - Py_ssize_t refcnt = Py_REFCNT(op); - if (refcnt != 1) { - // Adjust the reftotal to account for the fact that we only - // restore a single reference in _PyCode_Fini. - _Py_AddRefTotal(_PyThreadState_GET(), -(refcnt - 1)); - } -#endif - - _Py_SetImmortal(op); - return op; - } - - assert(_Py_IsImmortal(entry->value)); - return (PyObject *)entry->value; -} static int compare_constants(const void *key1, const void *key2) { diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 6e50623cafa4ed..d58cb928a4996e 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -866,21 +866,8 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, if (str == NULL) goto error; -#ifdef Py_GIL_DISABLED - // gh-118527: Disable immortalization of code constants for explicit - // compile() calls to get consistent frozen outputs between the default - // and free-threaded builds. - // Subtract two to suppress immortalization (so that 1 -> -1) - PyInterpreterState *interp = _PyInterpreterState_GET(); - _Py_atomic_add_int(&interp->gc.immortalize, -2); -#endif - result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize); -#ifdef Py_GIL_DISABLED - _Py_atomic_add_int(&interp->gc.immortalize, 2); -#endif - Py_XDECREF(source_copy); goto finally; diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index 5392e70caf8b2c..2a26323b566849 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -755,10 +755,6 @@ _PyGC_Init(PyInterpreterState *interp) { GCState *gcstate = &interp->gc; - // gh-117783: immortalize objects that would use deferred refcounting - // once the first non-main thread is created (but not in subinterpreters). - gcstate->immortalize = _Py_IsMainInterpreter(interp) ? 0 : -1; - gcstate->garbage = PyList_New(0); if (gcstate->garbage == NULL) { return _PyStatus_NO_MEMORY();