mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
GH-126491: GC: Mark objects reachable from roots before doing cycle collection (GH-127110)
* Mark almost all reachable objects before doing collection phase * Add stats for objects marked * Visit new frames before each increment * Update docs * Clearer calculation of work to do.
This commit is contained in:
parent
2a373da770
commit
a8dd821d5b
14 changed files with 365 additions and 113 deletions
|
|
@ -99,6 +99,8 @@ typedef struct _gc_stats {
|
|||
uint64_t collections;
|
||||
uint64_t object_visits;
|
||||
uint64_t objects_collected;
|
||||
uint64_t objects_transitively_reachable;
|
||||
uint64_t objects_not_transitively_reachable;
|
||||
} GCStats;
|
||||
|
||||
typedef struct _uop_stats {
|
||||
|
|
|
|||
|
|
@ -75,6 +75,7 @@ typedef struct _PyInterpreterFrame {
|
|||
_PyStackRef *stackpointer;
|
||||
uint16_t return_offset; /* Only relevant during a function call */
|
||||
char owner;
|
||||
char visited;
|
||||
/* Locals and stack */
|
||||
_PyStackRef localsplus[1];
|
||||
} _PyInterpreterFrame;
|
||||
|
|
@ -207,6 +208,7 @@ _PyFrame_Initialize(
|
|||
#endif
|
||||
frame->return_offset = 0;
|
||||
frame->owner = FRAME_OWNED_BY_THREAD;
|
||||
frame->visited = 0;
|
||||
|
||||
for (int i = null_locals_from; i < code->co_nlocalsplus; i++) {
|
||||
frame->localsplus[i] = PyStackRef_NULL;
|
||||
|
|
@ -389,6 +391,7 @@ _PyFrame_PushTrampolineUnchecked(PyThreadState *tstate, PyCodeObject *code, int
|
|||
frame->instr_ptr = _PyCode_CODE(code);
|
||||
#endif
|
||||
frame->owner = FRAME_OWNED_BY_THREAD;
|
||||
frame->visited = 0;
|
||||
frame->return_offset = 0;
|
||||
|
||||
#ifdef Py_GIL_DISABLED
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@ extern "C" {
|
|||
|
||||
/* GC information is stored BEFORE the object structure. */
|
||||
typedef struct {
|
||||
// Pointer to next object in the list.
|
||||
// Tagged pointer to next object in the list.
|
||||
// 0 means the object is not tracked
|
||||
uintptr_t _gc_next;
|
||||
|
||||
// Pointer to previous object in the list.
|
||||
// Tagged pointer to previous object in the list.
|
||||
// Lowest two bits are used for flags documented later.
|
||||
uintptr_t _gc_prev;
|
||||
} PyGC_Head;
|
||||
|
|
@ -284,6 +284,11 @@ struct gc_generation_stats {
|
|||
Py_ssize_t uncollectable;
|
||||
};
|
||||
|
||||
enum _GCPhase {
|
||||
GC_PHASE_MARK = 0,
|
||||
GC_PHASE_COLLECT = 1
|
||||
};
|
||||
|
||||
struct _gc_runtime_state {
|
||||
/* List of objects that still need to be cleaned up, singly linked
|
||||
* via their gc headers' gc_prev pointers. */
|
||||
|
|
@ -311,6 +316,7 @@ struct _gc_runtime_state {
|
|||
Py_ssize_t work_to_do;
|
||||
/* Which of the old spaces is the visited space */
|
||||
int visited_space;
|
||||
int phase;
|
||||
|
||||
#ifdef Py_GIL_DISABLED
|
||||
/* This is the number of objects that survived the last full
|
||||
|
|
|
|||
|
|
@ -471,8 +471,8 @@ static inline void _PyObject_GC_TRACK(
|
|||
PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev);
|
||||
_PyGCHead_SET_NEXT(last, gc);
|
||||
_PyGCHead_SET_PREV(gc, last);
|
||||
/* Young objects will be moved into the visited space during GC, so set the bit here */
|
||||
gc->_gc_next = ((uintptr_t)generation0) | (uintptr_t)interp->gc.visited_space;
|
||||
uintptr_t not_visited = 1 ^ interp->gc.visited_space;
|
||||
gc->_gc_next = ((uintptr_t)generation0) | not_visited;
|
||||
generation0->_gc_prev = (uintptr_t)gc;
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,6 +137,7 @@ extern PyTypeObject _PyExc_MemoryError;
|
|||
{ .threshold = 0, }, \
|
||||
}, \
|
||||
.work_to_do = -5000, \
|
||||
.phase = GC_PHASE_MARK, \
|
||||
}, \
|
||||
.qsbr = { \
|
||||
.wr_seq = QSBR_INITIAL, \
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue