Skip to content

Commit 3c18fc8

Browse files
committed
Don't untrack dicts
1 parent 9e2d93c commit 3c18fc8

File tree

7 files changed

+8
-218
lines changed

7 files changed

+8
-218
lines changed

Lib/test/test_dict.py

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -880,115 +880,6 @@ class C(object):
880880
gc.collect()
881881
self.assertIs(ref(), None, "Cycle was not collected")
882882

883-
def _not_tracked(self, t):
884-
# Nested containers can take several collections to untrack
885-
gc.collect()
886-
gc.collect()
887-
self.assertFalse(gc.is_tracked(t), t)
888-
889-
def _tracked(self, t):
890-
self.assertTrue(gc.is_tracked(t), t)
891-
gc.collect()
892-
gc.collect()
893-
self.assertTrue(gc.is_tracked(t), t)
894-
895-
def test_string_keys_can_track_values(self):
896-
# Test that this doesn't leak.
897-
for i in range(10):
898-
d = {}
899-
for j in range(10):
900-
d[str(j)] = j
901-
d["foo"] = d
902-
903-
@support.cpython_only
904-
def test_track_literals(self):
905-
# Test GC-optimization of dict literals
906-
x, y, z, w = 1.5, "a", (1, None), []
907-
908-
self._not_tracked({})
909-
self._not_tracked({x:(), y:x, z:1})
910-
self._not_tracked({1: "a", "b": 2})
911-
self._not_tracked({1: 2, (None, True, False, ()): int})
912-
self._not_tracked({1: object()})
913-
914-
# Dicts with mutable elements are always tracked, even if those
915-
# elements are not tracked right now.
916-
self._tracked({1: []})
917-
self._tracked({1: ([],)})
918-
self._tracked({1: {}})
919-
self._tracked({1: set()})
920-
921-
@support.cpython_only
922-
def test_track_dynamic(self):
923-
# Test GC-optimization of dynamically-created dicts
924-
class MyObject(object):
925-
pass
926-
x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject()
927-
928-
d = dict()
929-
self._not_tracked(d)
930-
d[1] = "a"
931-
self._not_tracked(d)
932-
d[y] = 2
933-
self._not_tracked(d)
934-
d[z] = 3
935-
self._not_tracked(d)
936-
self._not_tracked(d.copy())
937-
d[4] = w
938-
self._tracked(d)
939-
self._tracked(d.copy())
940-
d[4] = None
941-
self._not_tracked(d)
942-
self._not_tracked(d.copy())
943-
944-
# dd isn't tracked right now, but it may mutate and therefore d
945-
# which contains it must be tracked.
946-
d = dict()
947-
dd = dict()
948-
d[1] = dd
949-
self._not_tracked(dd)
950-
self._tracked(d)
951-
dd[1] = d
952-
self._tracked(dd)
953-
954-
d = dict.fromkeys([x, y, z])
955-
self._not_tracked(d)
956-
dd = dict()
957-
dd.update(d)
958-
self._not_tracked(dd)
959-
d = dict.fromkeys([x, y, z, o])
960-
self._tracked(d)
961-
dd = dict()
962-
dd.update(d)
963-
self._tracked(dd)
964-
965-
d = dict(x=x, y=y, z=z)
966-
self._not_tracked(d)
967-
d = dict(x=x, y=y, z=z, w=w)
968-
self._tracked(d)
969-
d = dict()
970-
d.update(x=x, y=y, z=z)
971-
self._not_tracked(d)
972-
d.update(w=w)
973-
self._tracked(d)
974-
975-
d = dict([(x, y), (z, 1)])
976-
self._not_tracked(d)
977-
d = dict([(x, y), (z, w)])
978-
self._tracked(d)
979-
d = dict()
980-
d.update([(x, y), (z, 1)])
981-
self._not_tracked(d)
982-
d.update([(x, y), (z, w)])
983-
self._tracked(d)
984-
985-
@support.cpython_only
986-
def test_track_subtypes(self):
987-
# Dict subtypes are always tracked
988-
class MyDict(dict):
989-
pass
990-
self._tracked(MyDict())
991-
992883
def make_shared_key_dict(self, n):
993884
class C:
994885
pass

Objects/dictobject.c

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ new_dict(PyInterpreterState *interp,
883883
mp->ma_used = used;
884884
mp->_ma_watcher_tag = 0;
885885
ASSERT_CONSISTENT(mp);
886+
_PyObject_GC_TRACK(mp);
886887
return (PyObject *)mp;
887888
}
888889

@@ -1578,64 +1579,6 @@ _PyDict_HasOnlyStringKeys(PyObject *dict)
15781579
return 1;
15791580
}
15801581

1581-
#define MAINTAIN_TRACKING(mp, key, value) \
1582-
do { \
1583-
if (!_PyObject_GC_IS_TRACKED(mp)) { \
1584-
if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1585-
_PyObject_GC_MAY_BE_TRACKED(value)) { \
1586-
_PyObject_GC_TRACK(mp); \
1587-
} \
1588-
} \
1589-
} while(0)
1590-
1591-
void
1592-
_PyDict_MaybeUntrack(PyObject *op)
1593-
{
1594-
PyDictObject *mp;
1595-
PyObject *value;
1596-
Py_ssize_t i, numentries;
1597-
1598-
ASSERT_WORLD_STOPPED_OR_DICT_LOCKED(op);
1599-
1600-
if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1601-
return;
1602-
1603-
mp = (PyDictObject *) op;
1604-
ASSERT_CONSISTENT(mp);
1605-
numentries = mp->ma_keys->dk_nentries;
1606-
if (_PyDict_HasSplitTable(mp)) {
1607-
for (i = 0; i < numentries; i++) {
1608-
if ((value = mp->ma_values->values[i]) == NULL)
1609-
continue;
1610-
if (_PyObject_GC_MAY_BE_TRACKED(value)) {
1611-
return;
1612-
}
1613-
}
1614-
}
1615-
else {
1616-
if (DK_IS_UNICODE(mp->ma_keys)) {
1617-
PyDictUnicodeEntry *ep0 = DK_UNICODE_ENTRIES(mp->ma_keys);
1618-
for (i = 0; i < numentries; i++) {
1619-
if ((value = ep0[i].me_value) == NULL)
1620-
continue;
1621-
if (_PyObject_GC_MAY_BE_TRACKED(value))
1622-
return;
1623-
}
1624-
}
1625-
else {
1626-
PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
1627-
for (i = 0; i < numentries; i++) {
1628-
if ((value = ep0[i].me_value) == NULL)
1629-
continue;
1630-
if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1631-
_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1632-
return;
1633-
}
1634-
}
1635-
}
1636-
_PyObject_GC_UNTRACK(op);
1637-
}
1638-
16391582
void
16401583
_PyDict_EnablePerThreadRefcounting(PyObject *op)
16411584
{
@@ -1761,7 +1704,6 @@ insert_split_value(PyInterpreterState *interp, PyDictObject *mp, PyObject *key,
17611704
{
17621705
assert(PyUnicode_CheckExact(key));
17631706
ASSERT_DICT_LOCKED(mp);
1764-
MAINTAIN_TRACKING(mp, key, value);
17651707
PyObject *old_value = mp->ma_values->values[ix];
17661708
if (old_value == NULL) {
17671709
_PyDict_NotifyEvent(interp, PyDict_EVENT_ADDED, mp, key, value);
@@ -1818,8 +1760,6 @@ insertdict(PyInterpreterState *interp, PyDictObject *mp,
18181760
if (ix == DKIX_ERROR)
18191761
goto Fail;
18201762

1821-
MAINTAIN_TRACKING(mp, key, value);
1822-
18231763
if (ix == DKIX_EMPTY) {
18241764
assert(!_PyDict_HasSplitTable(mp));
18251765
/* Insert into new slot. */
@@ -1878,8 +1818,6 @@ insert_to_emptydict(PyInterpreterState *interp, PyDictObject *mp,
18781818
/* We don't decref Py_EMPTY_KEYS here because it is immortal. */
18791819
assert(mp->ma_values == NULL);
18801820

1881-
MAINTAIN_TRACKING(mp, key, value);
1882-
18831821
size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
18841822
dictkeys_set_index(newkeys, hashpos, 0);
18851823
if (unicode) {
@@ -3770,11 +3708,6 @@ dict_dict_merge(PyInterpreterState *interp, PyDictObject *mp, PyDictObject *othe
37703708
STORE_USED(mp, other->ma_used);
37713709
ASSERT_CONSISTENT(mp);
37723710

3773-
if (_PyObject_GC_IS_TRACKED(other) && !_PyObject_GC_IS_TRACKED(mp)) {
3774-
/* Maintain tracking. */
3775-
_PyObject_GC_TRACK(mp);
3776-
}
3777-
37783711
return 0;
37793712
}
37803713
}
@@ -4024,8 +3957,7 @@ copy_lock_held(PyObject *o)
40243957
split_copy->ma_used = mp->ma_used;
40253958
split_copy->_ma_watcher_tag = 0;
40263959
dictkeys_incref(mp->ma_keys);
4027-
if (_PyObject_GC_IS_TRACKED(mp))
4028-
_PyObject_GC_TRACK(split_copy);
3960+
_PyObject_GC_TRACK(split_copy);
40293961
return (PyObject *)split_copy;
40303962
}
40313963

@@ -4060,10 +3992,6 @@ copy_lock_held(PyObject *o)
40603992

40613993
new->ma_used = mp->ma_used;
40623994
ASSERT_CONSISTENT(new);
4063-
if (_PyObject_GC_IS_TRACKED(mp)) {
4064-
/* Maintain tracking. */
4065-
_PyObject_GC_TRACK(new);
4066-
}
40673995

40683996
return (PyObject *)new;
40693997
}
@@ -4350,8 +4278,6 @@ dict_setdefault_ref_lock_held(PyObject *d, PyObject *key, PyObject *default_valu
43504278
*result = NULL;
43514279
}
43524280
}
4353-
4354-
MAINTAIN_TRACKING(mp, key, value);
43554281
STORE_USED(mp, mp->ma_used + 1);
43564282
assert(mp->ma_keys->dk_usable >= 0);
43574283
ASSERT_CONSISTENT(mp);
@@ -4801,15 +4727,8 @@ dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
48014727
d->ma_values = NULL;
48024728
ASSERT_CONSISTENT(d);
48034729

4804-
if (type != &PyDict_Type) {
4805-
// Don't track if a subclass tp_alloc is PyType_GenericAlloc()
4806-
if (!_PyObject_GC_IS_TRACKED(d)) {
4807-
_PyObject_GC_TRACK(d);
4808-
}
4809-
}
4810-
else {
4811-
// _PyType_AllocNoTrack() does not track the created object
4812-
assert(!_PyObject_GC_IS_TRACKED(d));
4730+
if (!_PyObject_GC_IS_TRACKED(d)) {
4731+
_PyObject_GC_TRACK(d);
48134732
}
48144733
return self;
48154734
}
@@ -6755,9 +6674,6 @@ make_dict_from_instance_attributes(PyInterpreterState *interp,
67556674
}
67566675
}
67576676
PyDictObject *res = (PyDictObject *)new_dict(interp, keys, values, used, 0);
6758-
if (track && res) {
6759-
_PyObject_GC_TRACK(res);
6760-
}
67616677
return res;
67626678
}
67636679

Objects/moduleobject.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ static void
107107
track_module(PyModuleObject *m)
108108
{
109109
_PyDict_EnablePerThreadRefcounting(m->md_dict);
110-
PyObject_GC_Track(m->md_dict);
111-
112110
_PyObject_SetDeferredRefcount((PyObject *)m);
113111
PyObject_GC_Track(m);
114112
}

Python/bytecodes.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,10 +2316,6 @@ dummy_func(
23162316
DEOPT_IF(ep->me_key != name);
23172317
PyObject *old_value = ep->me_value;
23182318
DEOPT_IF(old_value == NULL);
2319-
/* Ensure dict is GC tracked if it needs to be */
2320-
if (!_PyObject_GC_IS_TRACKED(dict) && _PyObject_GC_MAY_BE_TRACKED(PyStackRef_AsPyObjectBorrow(value))) {
2321-
_PyObject_GC_TRACK(dict);
2322-
}
23232319
_PyDict_NotifyEvent(tstate->interp, PyDict_EVENT_MODIFIED, dict, name, PyStackRef_AsPyObjectBorrow(value));
23242320
ep->me_value = PyStackRef_AsPyObjectSteal(value);
23252321
// old_value should be DECREFed after GC track checking is done, if not, it could raise a segmentation fault,

Python/executor_cases.c.h

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/gc.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
726726
}
727727

728728
static void
729-
untrack_tuples_and_dicts(PyGC_Head *head)
729+
untrack_tuples(PyGC_Head *head)
730730
{
731731
PyGC_Head *next, *gc = GC_NEXT(head);
732732
while (gc != head) {
@@ -735,9 +735,6 @@ untrack_tuples_and_dicts(PyGC_Head *head)
735735
if (PyTuple_CheckExact(op)) {
736736
_PyTuple_MaybeUntrack(op);
737737
}
738-
else if (PyDict_CheckExact(op)) {
739-
_PyDict_MaybeUntrack(op);
740-
}
741738
gc = next;
742739
}
743740
}
@@ -1295,7 +1292,7 @@ gc_collect_young(PyThreadState *tstate,
12951292
GCState *gcstate = &tstate->interp->gc;
12961293
PyGC_Head *young = &gcstate->young.head;
12971294
PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
1298-
untrack_tuples_and_dicts(&gcstate->young.head);
1295+
untrack_tuples(&gcstate->young.head);
12991296
GC_STAT_ADD(0, collections, 1);
13001297
#ifdef Py_STATS
13011298
{
@@ -1576,7 +1573,7 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
15761573
GC_STAT_ADD(1, objects_transitively_reachable, objects_marked);
15771574
gcstate->work_to_do -= objects_marked;
15781575
gc_list_set_space(&gcstate->young.head, gcstate->visited_space);
1579-
untrack_tuples_and_dicts(&gcstate->young.head);
1576+
untrack_tuples(&gcstate->young.head);
15801577
gc_list_merge(&gcstate->young.head, &increment);
15811578
gcstate->young.count = 0;
15821579
gc_list_validate_space(&increment, gcstate->visited_space);
@@ -1621,7 +1618,7 @@ gc_collect_full(PyThreadState *tstate,
16211618
PyGC_Head *young = &gcstate->young.head;
16221619
PyGC_Head *pending = &gcstate->old[gcstate->visited_space^1].head;
16231620
PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head;
1624-
untrack_tuples_and_dicts(&gcstate->young.head);
1621+
untrack_tuples(&gcstate->young.head);
16251622
/* merge all generations into pending */
16261623
gc_list_validate_space(young, 1-gcstate->visited_space);
16271624
gc_list_merge(young, pending);

Python/generated_cases.c.h

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)