Skip to content

Commit 498979f

Browse files
committed
Don't untrack dicts
1 parent 30aeb00 commit 498979f

File tree

11 files changed

+28
-270
lines changed

11 files changed

+28
-270
lines changed

Doc/library/gc.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ The :mod:`gc` module provides the following functions:
204204
>>> gc.is_tracked({})
205205
False
206206
>>> gc.is_tracked({"a": 1})
207-
False
208-
>>> gc.is_tracked({"a": []})
209207
True
210208

211209
.. versionadded:: 3.1

Include/internal/pycore_dict.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ extern int _PyDict_Next(
4343

4444
extern int _PyDict_HasOnlyStringKeys(PyObject *mp);
4545

46-
extern void _PyDict_MaybeUntrack(PyObject *mp);
47-
4846
// Export for '_ctypes' shared extension
4947
PyAPI_FUNC(Py_ssize_t) _PyDict_SizeOf(PyDictObject *);
5048

InternalDocs/garbage_collector.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ of `PyGC_Head` discussed in the `Memory layout and object structure`_ section:
532532
currently in. Instead, when that's needed, ad hoc tricks (like the
533533
`NEXT_MASK_UNREACHABLE` flag) are employed.
534534

535-
Optimization: delay tracking containers
536-
=======================================
535+
Optimization: delayed untracking containers
536+
===========================================
537537

538538
Certain types of containers cannot participate in a reference cycle, and so do
539539
not need to be tracked by the garbage collector. Untracking these objects
@@ -548,8 +548,8 @@ a container:
548548
As a general rule, instances of atomic types aren't tracked and instances of
549549
non-atomic types (containers, user-defined objects...) are. However, some
550550
type-specific optimizations can be present in order to suppress the garbage
551-
collector footprint of simple instances. Some examples of native types that
552-
benefit from delayed tracking:
551+
collector footprint of simple instances. Historically, both dictionaries and
552+
tuples were untracked during garbage collection. Now it is only tuples:
553553

554554
- Tuples containing only immutable objects (integers, strings etc,
555555
and recursively, tuples of immutable objects) do not need to be tracked. The
@@ -561,12 +561,6 @@ benefit from delayed tracking:
561561
already not tracked. Tuples are examined for untracking in all garbage collection
562562
cycles. It may take more than one cycle to untrack a tuple.
563563

564-
- Dictionaries containing only immutable objects also do not need to be tracked.
565-
Dictionaries are untracked when created. If a tracked item is inserted into a
566-
dictionary (either as a key or value), the dictionary becomes tracked. During a
567-
full garbage collection (all generations), the collector will untrack any dictionaries
568-
whose contents are not tracked.
569-
570564
The garbage collector module provides the Python function `is_tracked(obj)`, which returns
571565
the current tracking status of the object. Subsequent garbage collections may change the
572566
tracking status of the object.
@@ -581,8 +575,6 @@ tracking status of the object.
581575
>>> gc.is_tracked({})
582576
False
583577
>>> gc.is_tracked({"a": 1})
584-
False
585-
>>> gc.is_tracked({"a": []})
586578
True
587579
```
588580

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: 19 additions & 92 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) {
@@ -4024,8 +3962,7 @@ copy_lock_held(PyObject *o)
40243962
split_copy->ma_used = mp->ma_used;
40253963
split_copy->_ma_watcher_tag = 0;
40263964
dictkeys_incref(mp->ma_keys);
4027-
if (_PyObject_GC_IS_TRACKED(mp))
4028-
_PyObject_GC_TRACK(split_copy);
3965+
_PyObject_GC_TRACK(split_copy);
40293966
return (PyObject *)split_copy;
40303967
}
40313968

@@ -4060,11 +3997,6 @@ copy_lock_held(PyObject *o)
40603997

40613998
new->ma_used = mp->ma_used;
40623999
ASSERT_CONSISTENT(new);
4063-
if (_PyObject_GC_IS_TRACKED(mp)) {
4064-
/* Maintain tracking. */
4065-
_PyObject_GC_TRACK(new);
4066-
}
4067-
40684000
return (PyObject *)new;
40694001
}
40704002

@@ -4351,7 +4283,6 @@ dict_setdefault_ref_lock_held(PyObject *d, PyObject *key, PyObject *default_valu
43514283
}
43524284
}
43534285

4354-
MAINTAIN_TRACKING(mp, key, value);
43554286
STORE_USED(mp, mp->ma_used + 1);
43564287
assert(mp->ma_keys->dk_usable >= 0);
43574288
ASSERT_CONSISTENT(mp);
@@ -4800,16 +4731,8 @@ dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
48004731
d->ma_keys = Py_EMPTY_KEYS;
48014732
d->ma_values = NULL;
48024733
ASSERT_CONSISTENT(d);
4803-
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));
4734+
if (!_PyObject_GC_IS_TRACKED(d)) {
4735+
_PyObject_GC_TRACK(d);
48134736
}
48144737
return self;
48154738
}
@@ -6746,19 +6669,14 @@ make_dict_from_instance_attributes(PyInterpreterState *interp,
67466669
{
67476670
dictkeys_incref(keys);
67486671
Py_ssize_t used = 0;
6749-
Py_ssize_t track = 0;
67506672
size_t size = shared_keys_usable_size(keys);
67516673
for (size_t i = 0; i < size; i++) {
67526674
PyObject *val = values->values[i];
67536675
if (val != NULL) {
67546676
used += 1;
6755-
track += _PyObject_GC_MAY_BE_TRACKED(val);
67566677
}
67576678
}
67586679
PyDictObject *res = (PyDictObject *)new_dict(interp, keys, values, used, 0);
6759-
if (track && res) {
6760-
_PyObject_GC_TRACK(res);
6761-
}
67626680
return res;
67636681
}
67646682

@@ -7204,6 +7122,7 @@ _PyObject_SetManagedDict(PyObject *obj, PyObject *new_dict)
72047122
// since we locked it.
72057123
dict = _PyObject_ManagedDictPointer(obj)->dict;
72067124
err = _PyDict_DetachFromObject(dict, obj);
7125+
assert(err == 0 || new_dict == NULL);
72077126
if (err == 0) {
72087127
FT_ATOMIC_STORE_PTR(_PyObject_ManagedDictPointer(obj)->dict,
72097128
(PyDictObject *)Py_XNewRef(new_dict));
@@ -7236,7 +7155,21 @@ void
72367155
PyObject_ClearManagedDict(PyObject *obj)
72377156
{
72387157
if (_PyObject_SetManagedDict(obj, NULL) < 0) {
7158+
/* Must be out of memory */
7159+
assert(PyErr_Occurred() == PyExc_MemoryError);
72397160
PyErr_WriteUnraisable(NULL);
7161+
/* Clear the dict */
7162+
PyDictObject *dict = _PyObject_GetManagedDict(obj);
7163+
Py_BEGIN_CRITICAL_SECTION2(dict, obj);
7164+
dict = _PyObject_ManagedDictPointer(obj)->dict;
7165+
PyInterpreterState *interp = _PyInterpreterState_GET();
7166+
PyDictKeysObject *oldkeys = dict->ma_keys;
7167+
set_keys(dict, Py_EMPTY_KEYS);
7168+
dict->ma_values = NULL;
7169+
dictkeys_decref(interp, oldkeys, IS_DICT_SHARED(dict));
7170+
STORE_USED(dict, 0);
7171+
set_dict_inline_values(obj, NULL);
7172+
Py_END_CRITICAL_SECTION2();
72407173
}
72417174
}
72427175

@@ -7261,12 +7194,6 @@ _PyDict_DetachFromObject(PyDictObject *mp, PyObject *obj)
72617194
PyDictValues *values = copy_values(mp->ma_values);
72627195

72637196
if (values == NULL) {
7264-
/* Out of memory. Clear the dict */
7265-
PyInterpreterState *interp = _PyInterpreterState_GET();
7266-
PyDictKeysObject *oldkeys = mp->ma_keys;
7267-
set_keys(mp, Py_EMPTY_KEYS);
7268-
dictkeys_decref(interp, oldkeys, IS_DICT_SHARED(mp));
7269-
STORE_USED(mp, 0);
72707197
PyErr_NoMemory();
72717198
return -1;
72727199
}

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
}

0 commit comments

Comments
 (0)