Skip to content

Commit

Permalink
remove all temporary immortalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidget-Spinner committed Jul 3, 2024
1 parent 32aaf2b commit 41c6218
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 104 deletions.
8 changes: 0 additions & 8 deletions Include/internal/pycore_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
14 changes: 0 additions & 14 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
65 changes: 0 additions & 65 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 0 additions & 13 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 0 additions & 4 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 41c6218

Please sign in to comment.