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

Skip to content

Commit 33fe22c

Browse files
committed
Change old space bit of young objects from 0 to gcstate->visited_space. This ensures that any object created *and* collected during cycle GC has the bit set correctly.
1 parent eebea7e commit 33fe22c

File tree

4 files changed

+53
-43
lines changed

4 files changed

+53
-43
lines changed

Include/internal/pycore_gc.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,19 @@ static inline void _PyObject_GC_SET_SHARED_INLINE(PyObject *op) {
113113
/* Bit 1 is set when the object is in generation which is GCed currently. */
114114
#define _PyGC_PREV_MASK_COLLECTING 2
115115

116-
/* Bit 0 is set if the object belongs to old space 1 */
116+
/* Bit 0 in _gc_next is the old space bit.
117+
* It is set as follows:
118+
* Young: gcstate->visited_space
119+
* old[0]: 0
120+
* old[1]: 1
121+
* permanent: 0
122+
*
123+
* During a collection all objects handled should have the bit set to
124+
* gcstate->visited_space, as objects are moved from the young gen
125+
* and the increment into old[gcstate->visited_space].
126+
* When object are moved from the pending space, old[gcstate->visited_space^1]
127+
* into the increment, the old space bit is flipped.
128+
*/
117129
#define _PyGC_NEXT_MASK_OLD_SPACE_1 1
118130

119131
#define _PyGC_PREV_SHIFT 2

Include/internal/pycore_object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ static inline void _PyObject_GC_TRACK(
318318
PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev);
319319
_PyGCHead_SET_NEXT(last, gc);
320320
_PyGCHead_SET_PREV(gc, last);
321-
_PyGCHead_SET_NEXT(gc, generation0);
322-
assert((gc->_gc_next & _PyGC_NEXT_MASK_OLD_SPACE_1) == 0);
321+
/* Young objects will be moved into the visited space during GC, so set the bit here */
322+
gc->_gc_next = ((uintptr_t)generation0) | interp->gc.visited_space;
323323
generation0->_gc_prev = (uintptr_t)gc;
324324
#endif
325325
}

Lib/test/test_gc.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -823,32 +823,10 @@ def test_get_objects_generations(self):
823823
self.assertTrue(
824824
any(l is element for element in gc.get_objects(generation=0))
825825
)
826-
self.assertFalse(
827-
any(l is element for element in gc.get_objects(generation=1))
828-
)
829-
self.assertFalse(
830-
any(l is element for element in gc.get_objects(generation=2))
831-
)
832-
gc.collect(generation=0)
833-
self.assertFalse(
834-
any(l is element for element in gc.get_objects(generation=0))
835-
)
836-
self.assertTrue(
837-
any(l is element for element in gc.get_objects(generation=1))
838-
)
839-
self.assertFalse(
840-
any(l is element for element in gc.get_objects(generation=2))
841-
)
842-
gc.collect(generation=2)
826+
gc.collect()
843827
self.assertFalse(
844828
any(l is element for element in gc.get_objects(generation=0))
845829
)
846-
self.assertFalse(
847-
any(l is element for element in gc.get_objects(generation=1))
848-
)
849-
self.assertTrue(
850-
any(l is element for element in gc.get_objects(generation=2))
851-
)
852830
del l
853831
gc.collect()
854832

Python/gc.c

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,20 @@ validate_consistent_old_space(PyGC_Head *head)
455455
assert(prev == GC_PREV(head));
456456
}
457457

458+
static void
459+
gc_list_validate_space(PyGC_Head *head, int space) {
460+
PyGC_Head *gc = GC_NEXT(head);
461+
while (gc != head) {
462+
assert(gc_old_space(gc) == space);
463+
gc = GC_NEXT(gc);
464+
}
465+
}
466+
458467
#else
459468
#define validate_list(x, y) do{}while(0)
460469
#define validate_old(g) do{}while(0)
461470
#define validate_consistent_old_space(l) do{}while(0)
471+
#define gc_list_validate_space(l, s) do{}while(0)
462472
#endif
463473

464474
/*** end of list stuff ***/
@@ -949,6 +959,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
949959
/* Invoke the callbacks we decided to honor. It's safe to invoke them
950960
* because they can't reference unreachable objects.
951961
*/
962+
int visited_space = get_gc_state()->visited_space;
952963
while (! gc_list_is_empty(&wrcb_to_call)) {
953964
PyObject *temp;
954965
PyObject *callback;
@@ -983,6 +994,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
983994
Py_DECREF(op);
984995
if (wrcb_to_call._gc_next == (uintptr_t)gc) {
985996
/* object is still alive -- move it */
997+
gc_set_old_space(gc, visited_space);
986998
gc_list_move(gc, old);
987999
}
9881000
else {
@@ -1390,6 +1402,14 @@ completed_cycle(GCState *gcstate)
13901402
assert(gc_list_is_empty(not_visited));
13911403
#endif
13921404
gcstate->visited_space = flip_old_space(gcstate->visited_space);
1405+
/* Make sure all young objects have old space bit set correctly */
1406+
PyGC_Head *young = &gcstate->young.head;
1407+
PyGC_Head *gc = GC_NEXT(young);
1408+
while (gc != young) {
1409+
PyGC_Head *next = GC_NEXT(gc);
1410+
gc_set_old_space(gc, gcstate->visited_space);
1411+
gc = next;
1412+
}
13931413
gcstate->work_to_do = 0;
13941414
}
13951415

@@ -1407,10 +1427,7 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
14071427
}
14081428
gc_list_merge(&gcstate->young.head, &increment);
14091429
gcstate->young.count = 0;
1410-
if (gcstate->visited_space) {
1411-
/* objects in visited space have bit set, so we set it here */
1412-
gc_list_set_space(&increment, 1);
1413-
}
1430+
gc_list_validate_space(&increment, gcstate->visited_space);
14141431
Py_ssize_t increment_size = 0;
14151432
while (increment_size < gcstate->work_to_do) {
14161433
if (gc_list_is_empty(not_visited)) {
@@ -1422,10 +1439,12 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
14221439
gc_set_old_space(gc, gcstate->visited_space);
14231440
increment_size += expand_region_transitively_reachable(&increment, gc, gcstate);
14241441
}
1442+
gc_list_validate_space(&increment, gcstate->visited_space);
14251443
GC_STAT_ADD(1, objects_queued, region_size);
14261444
PyGC_Head survivors;
14271445
gc_list_init(&survivors);
14281446
gc_collect_region(tstate, &increment, &survivors, UNTRACK_TUPLES, stats);
1447+
gc_list_validate_space(&survivors, gcstate->visited_space);
14291448
gc_list_merge(&survivors, visited);
14301449
assert(gc_list_is_empty(&increment));
14311450
gcstate->work_to_do += gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor;
@@ -1446,23 +1465,18 @@ gc_collect_full(PyThreadState *tstate,
14461465
GCState *gcstate = &tstate->interp->gc;
14471466
validate_old(gcstate);
14481467
PyGC_Head *young = &gcstate->young.head;
1449-
PyGC_Head *old0 = &gcstate->old[0].head;
1450-
PyGC_Head *old1 = &gcstate->old[1].head;
1451-
/* merge all generations into old0 */
1452-
gc_list_merge(young, old0);
1468+
PyGC_Head *pending = &gcstate->old[gcstate->visited_space^1].head;
1469+
PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
1470+
/* merge all generations into visited */
1471+
gc_list_validate_space(young, gcstate->visited_space);
1472+
gc_list_set_space(pending, gcstate->visited_space);
1473+
gc_list_merge(young, pending);
14531474
gcstate->young.count = 0;
1454-
PyGC_Head *gc = GC_NEXT(old1);
1455-
while (gc != old1) {
1456-
PyGC_Head *next = GC_NEXT(gc);
1457-
gc_set_old_space(gc, 0);
1458-
gc = next;
1459-
}
1460-
gc_list_merge(old1, old0);
1475+
gc_list_merge(pending, visited);
14611476

1462-
gc_collect_region(tstate, old0, old0,
1477+
gc_collect_region(tstate, visited, visited,
14631478
UNTRACK_TUPLES | UNTRACK_DICTS,
14641479
stats);
1465-
gcstate->visited_space = 1;
14661480
gcstate->young.count = 0;
14671481
gcstate->old[0].count = 0;
14681482
gcstate->old[1].count = 0;
@@ -1529,6 +1543,7 @@ gc_collect_region(PyThreadState *tstate,
15291543

15301544
/* Clear weakrefs and invoke callbacks as necessary. */
15311545
stats->collected += handle_weakrefs(&unreachable, to);
1546+
gc_list_validate_space(to, gcstate->visited_space);
15321547
validate_list(to, collecting_clear_unreachable_clear);
15331548
validate_list(&unreachable, collecting_set_unreachable_clear);
15341549

@@ -1562,6 +1577,7 @@ gc_collect_region(PyThreadState *tstate,
15621577
* this if they insist on creating this type of structure.
15631578
*/
15641579
handle_legacy_finalizers(tstate, gcstate, &finalizers, to);
1580+
gc_list_validate_space(to, gcstate->visited_space);
15651581
validate_list(to, collecting_clear_unreachable_clear);
15661582
}
15671583

@@ -1710,6 +1726,10 @@ void
17101726
_PyGC_Freeze(PyInterpreterState *interp)
17111727
{
17121728
GCState *gcstate = &interp->gc;
1729+
/* The permanent_generation has its old space bit set to zero */
1730+
if (gcstate->visited_space) {
1731+
gc_list_set_space(&gcstate->young.head, 0);
1732+
}
17131733
gc_list_merge(&gcstate->young.head, &gcstate->permanent_generation.head);
17141734
gcstate->young.count = 0;
17151735
PyGC_Head*old0 = &gcstate->old[0].head;

0 commit comments

Comments
 (0)