Skip to content

Commit e0eb44a

Browse files
Yhg1sAA-Turner
andauthored
[3.13] GH-124567: Revert the Incremental GC in 3.13 (#124770)
Revert the incremental GC in 3.13, since it's not clear that without further turning, the benefits outweigh the costs. Co-authored-by: Adam Turner <[email protected]>
1 parent bc1fae8 commit e0eb44a

File tree

11 files changed

+5329
-5712
lines changed

11 files changed

+5329
-5712
lines changed

Doc/data/python3.13.abi

+4,904-4,906
Large diffs are not rendered by default.

Doc/library/gc.rst

+14-39
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.13
64-
``generation=1`` performs an increment of collection.
65-
6656

6757
.. function:: set_debug(flags)
6858

@@ -78,20 +68,13 @@ The :mod:`gc` module provides the following functions:
7868

7969
.. function:: get_objects(generation=None)
8070

81-
8271
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.13)
87-
* 2: All objects in the old generation
72+
returned. If *generation* is not ``None``, return only the objects tracked by
73+
the collector that are in that generation.
8874

8975
.. versionchanged:: 3.8
9076
New *generation* parameter.
9177

92-
.. versionchanged:: 3.13
93-
Generation 1 is removed
94-
9578
.. audit-event:: gc.get_objects generation gc.get_objects
9679

9780
.. function:: get_stats()
@@ -118,27 +101,19 @@ The :mod:`gc` module provides the following functions:
118101
Set the garbage collection thresholds (the collection frequency). Setting
119102
*threshold0* to zero disables collection.
120103

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
104+
The GC classifies objects into three generations depending on how many
105+
collection sweeps they have survived. New objects are placed in the youngest
106+
generation (generation ``0``). If an object survives a collection it is moved
107+
into the next older generation. Since generation ``2`` is the oldest
108+
generation, objects in that generation remain there after a collection. In
109+
order to decide when to run, the collector keeps track of the number object
126110
allocations and deallocations since the last collection. When the number of
127111
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.
130-
131-
The fraction of the old generation that is collected is **inversely** proportional
132-
to *threshold1*. The larger *threshold1* is, the slower objects in the old generation
133-
are collected.
134-
For the default value of 10, 1% of the old generation is scanned during each collection.
135-
136-
*threshold2* is ignored.
137-
138-
See `Garbage collector design <https://devguide.python.org/garbage_collector>`_ for more information.
139-
140-
.. versionchanged:: 3.13
141-
*threshold2* is ignored
112+
starts. Initially only generation ``0`` is examined. If generation ``0`` has
113+
been examined more than *threshold1* times since generation ``1`` has been
114+
examined, then generation ``1`` is examined as well.
115+
With the third generation, things are a bit more complicated,
116+
see `Collecting the oldest generation <https://devguide.python.org/garbage_collector/#collecting-the-oldest-generation>`_ for more information.
142117

143118

144119
.. function:: get_count()

Doc/whatsnew/3.13.rst

-66
Original file line numberDiff line numberDiff line change
@@ -501,30 +501,6 @@ are not tier 3 supported platforms, but will have best-effort support.
501501
.. seealso:: :pep:`730`, :pep:`738`
502502

503503

504-
.. _whatsnew313-incremental-gc:
505-
506-
Incremental garbage collection
507-
------------------------------
508-
509-
The cycle garbage collector is now incremental.
510-
This means that maximum pause times are reduced
511-
by an order of magnitude or more for larger heaps.
512-
513-
There are now only two generations: young and old.
514-
When :func:`gc.collect` is not called directly, the
515-
GC is invoked a little less frequently. When invoked, it
516-
collects the young generation and an increment of the
517-
old generation, instead of collecting one or more generations.
518-
519-
The behavior of :func:`!gc.collect` changes slightly:
520-
521-
* ``gc.collect(1)``: Performs an increment of garbage collection,
522-
rather than collecting generation 1.
523-
* Other calls to :func:`!gc.collect` are unchanged.
524-
525-
(Contributed by Mark Shannon in :gh:`108362`.)
526-
527-
528504
Other Language Changes
529505
======================
530506

@@ -921,36 +897,6 @@ fractions
921897
(Contributed by Mark Dickinson in :gh:`111320`.)
922898

923899

924-
gc
925-
--
926-
927-
The cyclic garbage collector is now incremental,
928-
which changes the meaning of the results of
929-
:meth:`~gc.get_threshold` and :meth:`~gc.set_threshold`
930-
as well as :meth:`~gc.get_count` and :meth:`~gc.get_stats`.
931-
932-
* For backwards compatibility, :meth:`~gc.get_threshold` continues to return
933-
a three-item tuple.
934-
The first value is the threshold for young collections, as before;
935-
the second value determines the rate at which the old collection is scanned
936-
(the default is 10, and higher values mean that the old collection
937-
is scanned more slowly).
938-
The third value is meaningless and is always zero.
939-
940-
* :meth:`~gc.set_threshold` ignores any items after the second.
941-
942-
* :meth:`~gc.get_count` and :meth:`~gc.get_stats` continue to return
943-
the same format of results.
944-
The only difference is that instead of the results referring to
945-
the young, aging and old generations,
946-
the results refer to the young generation
947-
and the aging and collecting spaces of the old generation.
948-
949-
In summary, code that attempted to manipulate the behavior of the cycle GC
950-
may not work exactly as intended, but it is very unlikely to be harmful.
951-
All other code will work just fine.
952-
953-
954900
glob
955901
----
956902

@@ -1515,11 +1461,6 @@ zipimport
15151461
Optimizations
15161462
=============
15171463

1518-
* The new :ref:`incremental garbage collector <whatsnew313-incremental-gc>`
1519-
means that maximum pause times are reduced
1520-
by an order of magnitude or more for larger heaps.
1521-
(Contributed by Mark Shannon in :gh:`108362`.)
1522-
15231464
* Several standard library modules have had
15241465
their import times significantly improved.
15251466
For example, the import time of the :mod:`typing` module
@@ -2632,13 +2573,6 @@ Changes in the Python API
26322573
Wrap it in :func:`staticmethod` if you want to preserve the old behavior.
26332574
(Contributed by Serhiy Storchaka in :gh:`121027`.)
26342575

2635-
* The :ref:`garbage collector is now incremental <whatsnew313-incremental-gc>`,
2636-
which means that the behavior of :func:`gc.collect` changes slightly:
2637-
2638-
* ``gc.collect(1)``: Performs an increment of garbage collection,
2639-
rather than collecting generation 1.
2640-
* Other calls to :func:`!gc.collect` are unchanged.
2641-
26422576
* An :exc:`OSError` is now raised by :func:`getpass.getuser`
26432577
for any failure to retrieve a username,
26442578
instead of :exc:`ImportError` on non-Unix platforms

Include/internal/pycore_gc.h

+11-40
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,11 @@ static inline void _PyObject_GC_SET_SHARED_INLINE(PyObject *op) {
142142

143143
/* Bit flags for _gc_prev */
144144
/* Bit 0 is set when tp_finalize is called */
145-
#define _PyGC_PREV_MASK_FINALIZED 1
145+
#define _PyGC_PREV_MASK_FINALIZED (1)
146146
/* Bit 1 is set when the object is in generation which is GCed currently. */
147-
#define _PyGC_PREV_MASK_COLLECTING 2
148-
149-
/* Bit 0 in _gc_next is the old space bit.
150-
* It is set as follows:
151-
* Young: gcstate->visited_space
152-
* old[0]: 0
153-
* old[1]: 1
154-
* permanent: 0
155-
*
156-
* During a collection all objects handled should have the bit set to
157-
* gcstate->visited_space, as objects are moved from the young gen
158-
* and the increment into old[gcstate->visited_space].
159-
* When object are moved from the pending space, old[gcstate->visited_space^1]
160-
* into the increment, the old space bit is flipped.
161-
*/
162-
#define _PyGC_NEXT_MASK_OLD_SPACE_1 1
163-
164-
#define _PyGC_PREV_SHIFT 2
147+
#define _PyGC_PREV_MASK_COLLECTING (2)
148+
/* The (N-2) most significant bits contain the real address. */
149+
#define _PyGC_PREV_SHIFT (2)
165150
#define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT)
166151

167152
/* set for debugging information */
@@ -187,21 +172,18 @@ typedef enum {
187172
// Lowest bit of _gc_next is used for flags only in GC.
188173
// But it is always 0 for normal code.
189174
static inline PyGC_Head* _PyGCHead_NEXT(PyGC_Head *gc) {
190-
uintptr_t next = gc->_gc_next & _PyGC_PREV_MASK;
175+
uintptr_t next = gc->_gc_next;
191176
return (PyGC_Head*)next;
192177
}
193178
static inline void _PyGCHead_SET_NEXT(PyGC_Head *gc, PyGC_Head *next) {
194-
uintptr_t unext = (uintptr_t)next;
195-
assert((unext & ~_PyGC_PREV_MASK) == 0);
196-
gc->_gc_next = (gc->_gc_next & ~_PyGC_PREV_MASK) | unext;
179+
gc->_gc_next = (uintptr_t)next;
197180
}
198181

199182
// Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
200183
static inline PyGC_Head* _PyGCHead_PREV(PyGC_Head *gc) {
201184
uintptr_t prev = (gc->_gc_prev & _PyGC_PREV_MASK);
202185
return (PyGC_Head*)prev;
203186
}
204-
205187
static inline void _PyGCHead_SET_PREV(PyGC_Head *gc, PyGC_Head *prev) {
206188
uintptr_t uprev = (uintptr_t)prev;
207189
assert((uprev & ~_PyGC_PREV_MASK) == 0);
@@ -287,13 +269,6 @@ struct gc_generation {
287269
generations */
288270
};
289271

290-
struct gc_collection_stats {
291-
/* number of collected objects */
292-
Py_ssize_t collected;
293-
/* total number of uncollectable objects (put into gc.garbage) */
294-
Py_ssize_t uncollectable;
295-
};
296-
297272
/* Running stats per generation */
298273
struct gc_generation_stats {
299274
/* total number of collections */
@@ -315,8 +290,8 @@ struct _gc_runtime_state {
315290
int enabled;
316291
int debug;
317292
/* linked lists of container objects */
318-
struct gc_generation young;
319-
struct gc_generation old[2];
293+
struct gc_generation generations[NUM_GENERATIONS];
294+
PyGC_Head *generation0;
320295
/* a permanent generation which won't be collected */
321296
struct gc_generation permanent_generation;
322297
struct gc_generation_stats generation_stats[NUM_GENERATIONS];
@@ -327,12 +302,6 @@ struct _gc_runtime_state {
327302
/* a list of callbacks to be invoked when collection is performed */
328303
PyObject *callbacks;
329304

330-
Py_ssize_t heap_size;
331-
Py_ssize_t work_to_do;
332-
/* Which of the old spaces is the visited space */
333-
int visited_space;
334-
335-
#ifdef Py_GIL_DISABLED
336305
/* This is the number of objects that survived the last full
337306
collection. It approximates the number of long lived objects
338307
tracked by the GC.
@@ -345,6 +314,7 @@ struct _gc_runtime_state {
345314
the first time. */
346315
Py_ssize_t long_lived_pending;
347316

317+
#ifdef Py_GIL_DISABLED
348318
/* gh-117783: Deferred reference counting is not fully implemented yet, so
349319
as a temporary measure we treat objects using deferred reference
350320
counting as immortal. The value may be zero, one, or a negative number:
@@ -365,7 +335,8 @@ struct _gc_thread_state {
365335

366336
extern void _PyGC_InitState(struct _gc_runtime_state *);
367337

368-
extern Py_ssize_t _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason);
338+
extern Py_ssize_t _PyGC_Collect(PyThreadState *tstate, int generation,
339+
_PyGC_Reason reason);
369340
extern void _PyGC_CollectNoFail(PyThreadState *tstate);
370341

371342
/* Freeze objects tracked by the GC and ignore them in future collections. */

Include/internal/pycore_object.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,11 @@ static inline void _PyObject_GC_TRACK(
353353
filename, lineno, __func__);
354354

355355
PyInterpreterState *interp = _PyInterpreterState_GET();
356-
PyGC_Head *generation0 = &interp->gc.young.head;
356+
PyGC_Head *generation0 = interp->gc.generation0;
357357
PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev);
358358
_PyGCHead_SET_NEXT(last, gc);
359359
_PyGCHead_SET_PREV(gc, last);
360-
/* Young objects will be moved into the visited space during GC, so set the bit here */
361-
gc->_gc_next = ((uintptr_t)generation0) | interp->gc.visited_space;
360+
_PyGCHead_SET_NEXT(gc, generation0);
362361
generation0->_gc_prev = (uintptr_t)gc;
363362
#endif
364363
}

Include/internal/pycore_runtime_init.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ extern PyTypeObject _PyExc_MemoryError;
228228
}, \
229229
.gc = { \
230230
.enabled = 1, \
231-
.young = { .threshold = 2000, }, \
232-
.old = { \
231+
.generations = { \
232+
/* .head is set in _PyGC_InitState(). */ \
233+
{ .threshold = 2000, }, \
234+
{ .threshold = 10, }, \
233235
{ .threshold = 10, }, \
234-
{ .threshold = 0, }, \
235236
}, \
236-
.work_to_do = -5000, \
237237
}, \
238238
.qsbr = { \
239239
.wr_seq = QSBR_INITIAL, \

0 commit comments

Comments
 (0)