Skip to content

Commit d7c0eaa

Browse files
committed
Backport generational GC patch to 3.14
1 parent 0d2101d commit d7c0eaa

File tree

14 files changed

+7043
-7824
lines changed

14 files changed

+7043
-7824
lines changed

Doc/data/python3.14.abi

Lines changed: 6061 additions & 6071 deletions
Large diffs are not rendered by default.

Doc/library/gc.rst

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,11 @@ The :mod:`!gc` module provides the following functions:
4040

4141
.. function:: collect(generation=2)
4242

43-
Perform a collection. The optional argument *generation*
43+
With no arguments, run a full collection. The optional argument *generation*
4444
may be an integer specifying which generation to collect (from 0 to 2). A
4545
:exc:`ValueError` is raised if the generation number is invalid. The sum of
4646
collected objects and uncollectable objects is returned.
4747

48-
Calling ``gc.collect(0)`` will perform a GC collection on the young generation.
49-
50-
Calling ``gc.collect(1)`` will perform a GC collection on the young generation
51-
and an increment of the old generation.
52-
53-
Calling ``gc.collect(2)`` or ``gc.collect()`` performs a full collection
54-
5548
The free lists maintained for a number of built-in types are cleared
5649
whenever a full collection or collection of the highest generation (2)
5750
is run. Not all items in some free lists may be freed due to the
@@ -60,9 +53,6 @@ The :mod:`!gc` module provides the following functions:
6053
The effect of calling ``gc.collect()`` while the interpreter is already
6154
performing a collection is undefined.
6255

63-
.. versionchanged:: 3.14
64-
``generation=1`` performs an increment of collection.
65-
6656

6757
.. function:: set_debug(flags)
6858

@@ -80,18 +70,12 @@ The :mod:`!gc` module provides the following functions:
8070

8171

8272
Returns a list of all objects tracked by the collector, excluding the list
83-
returned. If *generation* is not ``None``, return only the objects as follows:
84-
85-
* 0: All objects in the young generation
86-
* 1: No objects, as there is no generation 1 (as of Python 3.14)
87-
* 2: All objects in the old generation
73+
returned. If *generation* is not ``None``, return only the objects tracked by
74+
the collector that are in that generation.
8875

8976
.. versionchanged:: 3.8
9077
New *generation* parameter.
9178

92-
.. versionchanged:: 3.14
93-
Generation 1 is removed
94-
9579
.. audit-event:: gc.get_objects generation gc.get_objects
9680

9781
.. function:: get_stats()
@@ -108,43 +92,49 @@ The :mod:`!gc` module provides the following functions:
10892

10993
* ``uncollectable`` is the total number of objects which were found
11094
to be uncollectable (and were therefore moved to the :data:`garbage`
111-
list) inside this generation.
95+
list) inside this generation;
96+
97+
* ``candidates`` is the total number of objects in this generation which were
98+
considered for collection and traversed;
99+
100+
* ``duration`` is the total time in seconds spent in collections for this
101+
generation.
112102

113103
.. versionadded:: 3.4
114104

105+
.. versionchanged:: 3.14
106+
Add ``duration`` and ``candidates``.
107+
115108

116109
.. function:: set_threshold(threshold0, [threshold1, [threshold2]])
117110

118111
Set the garbage collection thresholds (the collection frequency). Setting
119112
*threshold0* to zero disables collection.
120113

121-
The GC classifies objects into two generations depending on whether they have
122-
survived a collection. New objects are placed in the young generation. If an
123-
object survives a collection it is moved into the old generation.
124-
125-
In order to decide when to run, the collector keeps track of the number of object
114+
The GC classifies objects into three generations depending on how many
115+
collection sweeps they have survived. New objects are placed in the youngest
116+
generation (generation ``0``). If an object survives a collection it is moved
117+
into the next older generation. Since generation ``2`` is the oldest
118+
generation, objects in that generation remain there after a collection. In
119+
order to decide when to run, the collector keeps track of the number object
126120
allocations and deallocations since the last collection. When the number of
127121
allocations minus the number of deallocations exceeds *threshold0*, collection
128-
starts. For each collection, all the objects in the young generation and some
129-
fraction of the old generation is collected.
122+
starts. Initially only generation ``0`` is examined. If generation ``0`` has
123+
been examined more than *threshold1* times since generation ``1`` has been
124+
examined, then generation ``1`` is examined as well.
125+
With the third generation, things are a bit more complicated,
126+
see `Garbage collector design <https://github.com/python/cpython/blob/3.14/InternalDocs/garbage_collector.md>`_
127+
for more information.
128+
129+
.. note::
130+
In the free-threaded build, the cycle collector is not generational.
131+
Collections operate over the entire tracked heap.
130132

131133
In the free-threaded build, the increase in process memory usage is also
132-
checked before running the collector. If the memory usage has not increased
134+
checked before running the collector. If the memory usage has not increased
133135
by 10% since the last collection and the net number of object allocations
134136
has not exceeded 40 times *threshold0*, the collection is not run.
135137

136-
The fraction of the old generation that is collected is **inversely** proportional
137-
to *threshold1*. The larger *threshold1* is, the slower objects in the old generation
138-
are collected.
139-
For the default value of 10, 1% of the old generation is scanned during each collection.
140-
141-
*threshold2* is ignored.
142-
143-
See `Garbage collector design <https://github.com/python/cpython/blob/3.14/InternalDocs/garbage_collector.md>`_ for more information.
144-
145-
.. versionchanged:: 3.14
146-
*threshold2* is ignored
147-
148138

149139
.. function:: get_count()
150140

Include/internal/pycore_gc.h

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ static inline void _PyGC_CLEAR_FINALIZED(PyObject *op) {
207207

208208
extern void _Py_ScheduleGC(PyThreadState *tstate);
209209

210-
#ifndef Py_GIL_DISABLED
211-
extern void _Py_TriggerGC(struct _gc_runtime_state *gcstate);
212-
#endif
213-
214210

215211
/* Tell the GC to track this object.
216212
*
@@ -220,7 +216,7 @@ extern void _Py_TriggerGC(struct _gc_runtime_state *gcstate);
220216
* ob_traverse method.
221217
*
222218
* Internal note: interp->gc.generation0->_gc_prev doesn't have any bit flags
223-
* because it's not object header. So we don't use _PyGCHead_PREV() and
219+
* because it's not an object header. So we don't use _PyGCHead_PREV() and
224220
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
225221
*
226222
* See also the public PyObject_GC_Track() function.
@@ -245,18 +241,12 @@ static inline void _PyObject_GC_TRACK(
245241
filename, lineno, __func__);
246242

247243
struct _gc_runtime_state *gcstate = &_PyInterpreterState_GET()->gc;
248-
PyGC_Head *generation0 = &gcstate->young.head;
249-
PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev);
244+
PyGC_Head *generation0 = gcstate->generation0;
245+
PyGC_Head *last = (PyGC_Head *)(generation0->_gc_prev);
250246
_PyGCHead_SET_NEXT(last, gc);
251247
_PyGCHead_SET_PREV(gc, last);
252-
uintptr_t not_visited = 1 ^ gcstate->visited_space;
253-
gc->_gc_next = ((uintptr_t)generation0) | not_visited;
248+
_PyGCHead_SET_NEXT(gc, generation0);
254249
generation0->_gc_prev = (uintptr_t)gc;
255-
gcstate->young.count++; /* number of tracked GC objects */
256-
gcstate->heap_size++;
257-
if (gcstate->young.count > gcstate->young.threshold) {
258-
_Py_TriggerGC(gcstate);
259-
}
260250
#endif
261251
}
262252

@@ -291,11 +281,6 @@ static inline void _PyObject_GC_UNTRACK(
291281
_PyGCHead_SET_PREV(next, prev);
292282
gc->_gc_next = 0;
293283
gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED;
294-
struct _gc_runtime_state *gcstate = &_PyInterpreterState_GET()->gc;
295-
if (gcstate->young.count > 0) {
296-
gcstate->young.count--;
297-
}
298-
gcstate->heap_size--;
299284
#endif
300285
}
301286

Include/internal/pycore_interp_structs.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,19 @@ struct gc_collection_stats {
187187

188188
/* Running stats per generation */
189189
struct gc_generation_stats {
190+
PyTime_t ts_start;
191+
PyTime_t ts_stop;
192+
190193
/* total number of collections */
191194
Py_ssize_t collections;
192195
/* total number of collected objects */
193196
Py_ssize_t collected;
194197
/* total number of uncollectable objects (put into gc.garbage) */
195198
Py_ssize_t uncollectable;
199+
/* total number of objects considered for collection */
200+
Py_ssize_t candidates;
201+
/* total duration of the collection in seconds */
202+
double duration;
196203
};
197204

198205
enum _GCPhase {
@@ -214,14 +221,23 @@ struct _gc_runtime_state {
214221
/* Is automatic collection enabled? */
215222
int enabled;
216223
int debug;
217-
/* linked lists of container objects */
224+
225+
/* Generational GC state used in GIL builds. */
226+
struct gc_generation generations[NUM_GENERATIONS];
227+
PyGC_Head *generation0;
228+
struct gc_generation_stats generation_stats_gen[NUM_GENERATIONS];
229+
230+
/* Incremental/free-threaded GC state. */
218231
struct gc_generation young;
219232
struct gc_generation old[2];
233+
220234
/* a permanent generation which won't be collected */
221235
struct gc_generation permanent_generation;
222236
struct gc_generation_stats generation_stats[NUM_GENERATIONS];
223237
/* true if we are currently running the collector */
224238
int collecting;
239+
/* The frame that started the current collection, or NULL. */
240+
_PyInterpreterFrame *frame;
225241
/* list of uncollectable objects */
226242
PyObject *garbage;
227243
/* a list of callbacks to be invoked when collection is performed */
@@ -233,7 +249,6 @@ struct _gc_runtime_state {
233249
int visited_space;
234250
int phase;
235251

236-
#ifdef Py_GIL_DISABLED
237252
/* This is the number of objects that survived the last full
238253
collection. It approximates the number of long lived objects
239254
tracked by the GC.
@@ -246,6 +261,7 @@ struct _gc_runtime_state {
246261
the first time. */
247262
Py_ssize_t long_lived_pending;
248263

264+
#ifdef Py_GIL_DISABLED
249265
/* True if gc.freeze() has been used. */
250266
int freeze_active;
251267

Include/internal/pycore_runtime_init.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ extern PyTypeObject _PyExc_MemoryError;
137137
}, \
138138
.gc = { \
139139
.enabled = 1, \
140+
.generations = { \
141+
{ .threshold = 2000, }, \
142+
{ .threshold = 10, }, \
143+
{ .threshold = 10, }, \
144+
}, \
140145
.young = { .threshold = 2000, }, \
141146
.old = { \
142147
{ .threshold = 10, }, \

0 commit comments

Comments
 (0)