Skip to content

Commit 53835dd

Browse files
committed
Improve accounting of long lived objects.
Subtract the objects that are unreachable and add back the ones that are resurrected. This likely doesn't matter in most cases but if the GC is freeing a lot of cyclic garbage then we don't want to over count the long lived objects.
1 parent 40657c7 commit 53835dd

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Python/gc_free_threading.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,9 @@ scan_heap_visitor(const mi_heap_t *heap, const mi_heap_area_t *area,
12301230
else {
12311231
worklist_push(&state->unreachable, op);
12321232
}
1233+
// It is possible this object will be resurrected but
1234+
// for now we assume it will be deallocated.
1235+
state->long_lived_total--;
12331236
return true;
12341237
}
12351238

@@ -1911,6 +1914,7 @@ handle_resurrected_objects(struct collection_state *state)
19111914
_PyObject_ASSERT(op, Py_REFCNT(op) > 1);
19121915
worklist_remove(&iter);
19131916
merge_refcount(op, -1); // remove worklist reference
1917+
state->long_lived_total++;
19141918
}
19151919
}
19161920
}
@@ -2311,9 +2315,6 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,
23112315
}
23122316
}
23132317

2314-
// Record the number of live GC objects
2315-
interp->gc.long_lived_total = state->long_lived_total;
2316-
23172318
// Find weakref callbacks we will honor (but do not call them).
23182319
find_weakref_callbacks(state);
23192320
_PyEval_StartTheWorld(interp);
@@ -2336,6 +2337,9 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,
23362337
}
23372338
_PyEval_StartTheWorld(interp);
23382339

2340+
// Record the number of live GC objects
2341+
interp->gc.long_lived_total = state->long_lived_total;
2342+
23392343
if (err < 0) {
23402344
cleanup_worklist(&state->unreachable);
23412345
cleanup_worklist(&state->legacy_finalizers);

0 commit comments

Comments
 (0)