Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Slightly speed up deallocation of objects that are not tracked by the garbage
collector, such as :class:`int`, :class:`float` and :class:`str`.
19 changes: 13 additions & 6 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3292,13 +3292,20 @@ _Py_Dealloc(PyObject *op)
PyTypeObject *type = Py_TYPE(op);
unsigned long gc_flag = type->tp_flags & Py_TPFLAGS_HAVE_GC;
destructor dealloc = type->tp_dealloc;
PyThreadState *tstate = _PyThreadState_GET();
intptr_t margin = _Py_RecursionLimit_GetMargin(tstate);
if (margin < 2 && gc_flag) {
_PyTrash_thread_deposit_object(tstate, (PyObject *)op);
return;
PyThreadState *tstate = NULL;
intptr_t margin = 0;
if (gc_flag) {
tstate = _PyThreadState_GET();
margin = _Py_RecursionLimit_GetMargin(tstate);
if (margin < 2) {
_PyTrash_thread_deposit_object(tstate, (PyObject *)op);
return;
}
}
#ifdef Py_DEBUG
if (tstate == NULL) {
tstate = _PyThreadState_GET();
}
#if !defined(Py_GIL_DISABLED) && !defined(Py_STACKREF_DEBUG)
/* This assertion doesn't hold for the free-threading build, as
* PyStackRef_CLOSE_SPECIALIZED is not implemented */
Expand Down Expand Up @@ -3340,7 +3347,7 @@ _Py_Dealloc(PyObject *op)
Py_XDECREF(old_exc);
Py_DECREF(type);
#endif
if (tstate->delete_later && margin >= 4 && gc_flag) {
if (gc_flag && tstate->delete_later && margin >= 4) {
_PyTrash_thread_destroy_chain(tstate);
}
}
Expand Down
Loading