Skip to content

Commit d14bb0d

Browse files
authored
Merge branch 'main' into wasi-package
2 parents 28126f4 + 206788a commit d14bb0d

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

Lib/test/test_capi/test_mem.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ def check(self, code):
2424
out = assert_python_failure(
2525
'-c', code,
2626
PYTHONMALLOC=self.PYTHONMALLOC,
27-
# FreeBSD: instruct jemalloc to not fill freed() memory
28-
# with junk byte 0x5a, see JEMALLOC(3)
27+
# Instruct the system allocator to not fill freed() memory
28+
# with junk bytes:
29+
# FreeBSD: jemalloc, see JEMALLOC(3).
2930
MALLOC_CONF="junk:false",
31+
# OpenBSD: see MALLOC.CONF(5).
32+
MALLOC_OPTIONS="j",
3033
)
3134
stderr = out.err
3235
return stderr.decode('ascii', 'replace')
@@ -102,7 +105,9 @@ def check_pyobject_is_freed(self, func_name):
102105
assert_python_ok(
103106
'-c', code,
104107
PYTHONMALLOC=self.PYTHONMALLOC,
108+
# See the comment in check() above.
105109
MALLOC_CONF="junk:false",
110+
MALLOC_OPTIONS="j",
106111
)
107112

108113
def test_pyobject_null_is_freed(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Slightly speed up deallocation of objects that are not tracked by the garbage
2+
collector, such as :class:`int`, :class:`float` and :class:`str`.

Objects/object.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,13 +3292,20 @@ _Py_Dealloc(PyObject *op)
32923292
PyTypeObject *type = Py_TYPE(op);
32933293
unsigned long gc_flag = type->tp_flags & Py_TPFLAGS_HAVE_GC;
32943294
destructor dealloc = type->tp_dealloc;
3295-
PyThreadState *tstate = _PyThreadState_GET();
3296-
intptr_t margin = _Py_RecursionLimit_GetMargin(tstate);
3297-
if (margin < 2 && gc_flag) {
3298-
_PyTrash_thread_deposit_object(tstate, (PyObject *)op);
3299-
return;
3295+
PyThreadState *tstate = NULL;
3296+
intptr_t margin = 0;
3297+
if (gc_flag) {
3298+
tstate = _PyThreadState_GET();
3299+
margin = _Py_RecursionLimit_GetMargin(tstate);
3300+
if (margin < 2) {
3301+
_PyTrash_thread_deposit_object(tstate, (PyObject *)op);
3302+
return;
3303+
}
33003304
}
33013305
#ifdef Py_DEBUG
3306+
if (tstate == NULL) {
3307+
tstate = _PyThreadState_GET();
3308+
}
33023309
#if !defined(Py_GIL_DISABLED) && !defined(Py_STACKREF_DEBUG)
33033310
/* This assertion doesn't hold for the free-threading build, as
33043311
* PyStackRef_CLOSE_SPECIALIZED is not implemented */
@@ -3340,7 +3347,7 @@ _Py_Dealloc(PyObject *op)
33403347
Py_XDECREF(old_exc);
33413348
Py_DECREF(type);
33423349
#endif
3343-
if (tstate->delete_later && margin >= 4 && gc_flag) {
3350+
if (gc_flag && tstate->delete_later && margin >= 4) {
33443351
_PyTrash_thread_destroy_chain(tstate);
33453352
}
33463353
}

0 commit comments

Comments
 (0)