Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
5 changes: 5 additions & 0 deletions Include/internal/pycore_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ static inline void _PyObject_GC_TRACK(

PyInterpreterState *interp = _PyInterpreterState_GET();
PyGC_Head *generation0 = &interp->gc.young.head;
interp->gc.young.count++; /* number of tracked GC objects */
PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev);
_PyGCHead_SET_NEXT(last, gc);
_PyGCHead_SET_PREV(gc, last);
Expand Down Expand Up @@ -280,6 +281,10 @@ static inline void _PyObject_GC_UNTRACK(
_PyGCHead_SET_PREV(next, prev);
gc->_gc_next = 0;
gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED;
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->gc.young.count > 0) {
interp->gc.young.count--;
}
#endif
}

Expand Down
44 changes: 37 additions & 7 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,18 @@ PyTuple_Pack(Py_ssize_t n, ...)
return NULL;
}
items = result->ob_item;
bool track = false;
for (i = 0; i < n; i++) {
o = va_arg(vargs, PyObject *);
items[i] = Py_NewRef(o);
if (!track && _PyObject_GC_MAY_BE_TRACKED(items[i])) {
track = true;
}
}
va_end(vargs);
_PyObject_GC_TRACK(result);
if (track) {
_PyObject_GC_TRACK(result);
}
return (PyObject *)result;
}

Expand Down Expand Up @@ -376,12 +382,18 @@ PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
if (tuple == NULL) {
return NULL;
}
bool track = false;
PyObject **dst = tuple->ob_item;
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = src[i];
dst[i] = Py_NewRef(item);
if (!track && _PyObject_GC_MAY_BE_TRACKED(dst[i])) {
track = true;
}
}
if (track) {
_PyObject_GC_TRACK(tuple);
}
_PyObject_GC_TRACK(tuple);
return (PyObject *)tuple;
}

Expand All @@ -395,11 +407,17 @@ _PyTuple_FromStackRefStealOnSuccess(const _PyStackRef *src, Py_ssize_t n)
if (tuple == NULL) {
return NULL;
}
bool track = false;
PyObject **dst = tuple->ob_item;
for (Py_ssize_t i = 0; i < n; i++) {
dst[i] = PyStackRef_AsPyObjectSteal(src[i]);
if (!track && _PyObject_GC_MAY_BE_TRACKED(dst[i])) {
track = true;
}
}
if (track) {
_PyObject_GC_TRACK(tuple);
}
_PyObject_GC_TRACK(tuple);
return (PyObject *)tuple;
}

Expand All @@ -416,12 +434,18 @@ _PyTuple_FromArraySteal(PyObject *const *src, Py_ssize_t n)
}
return NULL;
}
bool track = false;
PyObject **dst = tuple->ob_item;
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = src[i];
dst[i] = item;
if (!track && _PyObject_GC_MAY_BE_TRACKED(item)) {
track = true;
}
}
if (track) {
_PyObject_GC_TRACK(tuple);
}
_PyObject_GC_TRACK(tuple);
return (PyObject *)tuple;
}

Expand Down Expand Up @@ -494,7 +518,9 @@ tuple_concat(PyObject *aa, PyObject *bb)
dest[i] = Py_NewRef(v);
}

_PyObject_GC_TRACK(np);
if (_PyObject_GC_IS_TRACKED(a) || _PyObject_GC_IS_TRACKED(b)) {
_PyObject_GC_TRACK(np);
}
return (PyObject *)np;
}

Expand Down Expand Up @@ -543,7 +569,9 @@ tuple_repeat(PyObject *self, Py_ssize_t n)
_Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
sizeof(PyObject *)*input_size);
}
_PyObject_GC_TRACK(np);
if (_PyObject_GC_IS_TRACKED(a)) {
_PyObject_GC_TRACK(np);
}
return (PyObject *) np;
}

Expand Down Expand Up @@ -821,7 +849,9 @@ tuple_subscript(PyObject *op, PyObject* item)
dest[i] = it;
}

_PyObject_GC_TRACK(result);
if (_PyObject_GC_IS_TRACKED(self)) {
_PyObject_GC_TRACK(result);
}
return (PyObject *)result;
}
}
Expand Down
4 changes: 0 additions & 4 deletions Python/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2296,7 +2296,6 @@ _PyObject_GC_Link(PyObject *op)
GCState *gcstate = &tstate->interp->gc;
gc->_gc_next = 0;
gc->_gc_prev = 0;
gcstate->young.count++; /* number of allocated GC objects */
gcstate->heap_size++;
if (gcstate->young.count > gcstate->young.threshold &&
gcstate->enabled &&
Expand Down Expand Up @@ -2428,9 +2427,6 @@ PyObject_GC_Del(void *op)
#endif
}
GCState *gcstate = get_gc_state();
if (gcstate->young.count > 0) {
gcstate->young.count--;
}
gcstate->heap_size--;
PyObject_Free(((char *)op)-presize);
}
Expand Down
Loading