Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
reset allocation counts in gc thread-local buffers
  • Loading branch information
kevmo314 committed Nov 28, 2025
commit a9edb7b7974507c7ae23dfbd8edd7c0025e74a5a
9 changes: 4 additions & 5 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -2207,11 +2207,10 @@ record_deallocation(PyThreadState *tstate)
{
struct _gc_thread_state *gc = &((_PyThreadStateImpl *)tstate)->gc;

gc->alloc_count--;
if (gc->alloc_count <= -LOCAL_ALLOC_COUNT_THRESHOLD) {
GCState *gcstate = &tstate->interp->gc;
_Py_atomic_add_int(&gcstate->young.count, (int)gc->alloc_count);
gc->alloc_count = 0;
// Only decrement if positive, matching gc.c behavior which prevents
// negative counts (see PyObject_GC_Del in gc.c).
if (gc->alloc_count > 0) {
gc->alloc_count--;
}
}

Expand Down
8 changes: 8 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,14 @@ tstate_delete_common(PyThreadState *tstate, int release_gil)
assert(tstate_impl->refcounts.values == NULL);
#endif

#ifdef Py_GIL_DISABLED
// Flush the thread's local GC allocation count to the global count
// before the thread state is deleted, otherwise the count is lost.
_Py_atomic_add_int(&tstate->interp->gc.young.count,
(int)((_PyThreadStateImpl *)tstate)->gc.alloc_count);
((_PyThreadStateImpl *)tstate)->gc.alloc_count = 0;
Comment thread
kevmo314 marked this conversation as resolved.
Outdated
#endif

#if _Py_TIER2
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
if (_tstate->jit_tracer_state.code_buffer != NULL) {
Expand Down
Loading