Skip to content

Commit 92f75ad

Browse files
committed
gh-161524: Speed up copying sparse dictionaries
1 parent 4575fb9 commit 92f75ad

4 files changed

Lines changed: 258 additions & 32 deletions

File tree

‎Lib/test/test_capi/test_watchers.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ def test_clone(self):
6363
d.update(d2)
6464
self.assert_events(["clone"])
6565

66+
def test_clone_sparse_dict(self):
67+
for keys in (list(range(200)), [f"key{i}" for i in range(200)]):
68+
for remaining in (1, 20):
69+
with self.subTest(key_type=type(keys[0]),
70+
remaining=remaining):
71+
source = {key: key for key in keys}
72+
for key in keys[:200 - remaining]:
73+
del source[key]
74+
75+
target = {}
76+
with self.watcher() as wid:
77+
self.watch(wid, target)
78+
target.update(source)
79+
self.assert_events(["clone"])
80+
81+
self.assertEqual(list(target.items()), list(source.items()))
82+
source.clear()
83+
self.assertEqual(len(target), remaining)
84+
6685
def test_no_event_if_not_watched(self):
6786
d = {}
6887
with self.watcher() as wid:

‎Lib/test/test_dict.py‎

Lines changed: 156 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def test_bool(self):
8686
self.assertIs(bool({}), False)
8787
self.assertIs(bool({1: 2}), True)
8888

89+
8990
def test_keys(self):
9091
d = {}
9192
self.assertEqual(set(d.keys()), set())
@@ -466,17 +467,153 @@ class A:
466467

467468
def test_copy_noncompact(self):
468469
# Dicts don't compact themselves on del/pop operations.
469-
# Copy will use a slow merging strategy that produces
470-
# a compacted copy when roughly 33% of dict is a non-used
471-
# keys-space (to optimize memory footprint).
472-
# In this test we want to hit the slow/compacting
473-
# branch of dict.copy() and make sure it works OK.
470+
# Copy used to take a slow merging path when about a third
471+
# of the entries were deleted. The compact clone now skips
472+
# deleted entries directly, even when very few keys remain.
473+
# Check that an almost emptied dict is copied and compacted.
474474
d = {k: k for k in range(1000)}
475-
for k in range(950):
475+
for k in range(999):
476476
del d[k]
477477
d2 = d.copy()
478478
self.assertEqual(d2, d)
479479

480+
def test_sparse_dict_copy(self):
481+
for keys in ([f'key{i}' for i in range(120)], list(range(120))):
482+
for remaining in (0, 1, 10, 40, 80, 119, 120):
483+
with self.subTest(key_type=type(keys[0]), remaining=remaining):
484+
source = {key: object() for key in keys}
485+
for key in keys[:120 - remaining]:
486+
del source[key]
487+
488+
expected = list(source.items())
489+
for result in (source.copy(), dict(source), {**source}):
490+
self.assertIsNot(result, source)
491+
self.assertEqual(list(result.items()), expected)
492+
for (_, value), (_, copied) in zip(
493+
expected, result.items()):
494+
self.assertIs(copied, value)
495+
result.clear()
496+
self.assertEqual(list(source.items()), expected)
497+
498+
def test_sparse_dict_copy_deletion_patterns(self):
499+
from random import Random
500+
501+
for key_type in (str, int):
502+
keys = [key_type(i) for i in range(1000)]
503+
for remaining in (1, 15, 100, 500):
504+
for pattern in ("front", "back", "middle", "random"):
505+
with self.subTest(key_type=key_type, remaining=remaining,
506+
pattern=pattern):
507+
if pattern == "front":
508+
deleted = keys[:1000 - remaining]
509+
elif pattern == "back":
510+
deleted = keys[remaining:]
511+
elif pattern == "middle":
512+
start = remaining // 2
513+
deleted = keys[start:start + 1000 - remaining]
514+
else:
515+
deleted = keys.copy()
516+
Random(161524).shuffle(deleted)
517+
deleted = deleted[:1000 - remaining]
518+
519+
source = dict.fromkeys(keys, object())
520+
for key in deleted:
521+
del source[key]
522+
expected = list(source.items())
523+
for copy in (source.copy(), dict(source), {**source}):
524+
self.assertEqual(list(copy.items()), expected)
525+
self.assertEqual(len(copy), remaining)
526+
copy.clear()
527+
self.assertEqual(list(source.items()), expected)
528+
529+
def test_sparse_dict_copy_reinserted_keys(self):
530+
for keys in ([f'key{i}' for i in range(120)], list(range(120))):
531+
source = {key: object() for key in keys}
532+
for key in keys[:100]:
533+
del source[key]
534+
for key in keys[:5]:
535+
source[key] = object()
536+
537+
expected = list(source.items())
538+
for result in (source.copy(), dict(source), {**source}):
539+
self.assertEqual(list(result.items()), expected)
540+
541+
def test_sparse_dict_copy_does_not_rehash(self):
542+
class Key:
543+
def __init__(self, value):
544+
self.value = value
545+
self.hash_calls = 0
546+
547+
def __hash__(self):
548+
self.hash_calls += 1
549+
return self.value % 5
550+
551+
def __eq__(self, other):
552+
return self is other
553+
554+
keys = [Key(i) for i in range(90)]
555+
source = {key: key.value for key in keys}
556+
for key in keys[:70]:
557+
del source[key]
558+
calls = [key.hash_calls for key in keys]
559+
expected = list(source.items())
560+
for result in (source.copy(), dict(source), {**source}):
561+
self.assertEqual([key.hash_calls for key in keys], calls)
562+
for (key, value), (copy_key, copy_value) in zip(
563+
expected, result.items()):
564+
self.assertIs(copy_key, key)
565+
self.assertEqual(copy_value, value)
566+
567+
def test_sparse_dict_copy_releases_references(self):
568+
class Key:
569+
pass
570+
571+
class Value:
572+
pass
573+
574+
key = Key()
575+
value = Value()
576+
source = {i: None for i in range(150)}
577+
source[key] = value
578+
for i in range(150):
579+
del source[i]
580+
581+
key_ref = weakref.ref(key)
582+
value_ref = weakref.ref(value)
583+
copies = [source.copy(), dict(source), {**source}]
584+
del source, key, value
585+
586+
self.assertIsNotNone(key_ref())
587+
self.assertIsNotNone(value_ref())
588+
copies.clear()
589+
gc.collect()
590+
self.assertIsNone(key_ref())
591+
self.assertIsNone(value_ref())
592+
593+
def test_sparse_dict_copy_density(self):
594+
for key_type in (str, int):
595+
for remaining in (1, 10, 14, 15, 16, 17, 30, 31, 32, 33, 500):
596+
with self.subTest(key_type=key_type, remaining=remaining):
597+
d = {key_type(i): i for i in range(1000)}
598+
for i in range(1000 - remaining):
599+
del d[key_type(i)]
600+
expected = list(d.items())
601+
for copy in (d.copy(), dict(d), {**d}):
602+
self.assertEqual(list(copy.items()), expected)
603+
604+
@support.nomemtest
605+
def test_sparse_dict_copy_no_memory(self):
606+
import _testcapi
607+
608+
d = dict.fromkeys(range(128))
609+
for i in range(96):
610+
del d[i]
611+
612+
for copy in (d.copy, lambda: dict(d), lambda: {**d}):
613+
with self.subTest(copy=copy):
614+
with self.assertRaises(MemoryError):
615+
_testcapi.call_with_nomemory(0, 0, copy)
616+
480617
@support.nomemtest
481618
def test_copy_empty_no_memory(self):
482619
import _testcapi
@@ -1972,6 +2109,19 @@ def keys(self):
19722109
self.assertEqual(type(d2), frozendict)
19732110
self.assertEqual(list(d2.items()), pairs[::-1])
19742111

2112+
def test_copy_from_sparse_dict(self):
2113+
for keys in ([f'key{i}' for i in range(1000)], list(range(1000))):
2114+
source = {key: key for key in keys}
2115+
for key in keys[:700]:
2116+
del source[key]
2117+
2118+
expected = list(source.items())
2119+
d = frozendict(source)
2120+
self.assertEqual(list(d.items()), expected)
2121+
self.assertEqual(list(d.copy().items()), expected)
2122+
self.assertEqual(list(dict(d).items()), expected)
2123+
self.assertEqual(list(FrozenDict(d).copy().items()), expected)
2124+
19752125
def test_merge(self):
19762126
# test "a | b" operator
19772127
self.assertEqual(frozendict(x=1) | frozendict(y=2),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up copying dictionaries with many deleted entries by copying live
2+
entries directly into a compact table instead of inserting them one by one.

‎Objects/dictobject.c‎

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,6 +4296,66 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
42964296
return dict_merge_from_seq2(d, seq2, override);
42974297
}
42984298

4299+
4300+
/* Clone only live entries from a combined table, preserving insertion order.
4301+
The source is locked while its keys and values are referenced. */
4302+
static PyDictKeysObject *
4303+
clone_compact_dict_keys(PyDictObject *orig)
4304+
{
4305+
assert(PyAnyDict_Check(orig));
4306+
assert(Py_TYPE(orig)->tp_iter == dict_iter);
4307+
assert(orig->ma_values == NULL);
4308+
assert(orig->ma_used > 0);
4309+
if (!PyFrozenDict_Check(orig)) {
4310+
ASSERT_DICT_LOCKED(orig);
4311+
}
4312+
4313+
PyDictKeysObject *oldkeys = orig->ma_keys;
4314+
Py_ssize_t used = orig->ma_used;
4315+
int unicode = DK_IS_UNICODE(oldkeys);
4316+
PyDictKeysObject *keys = new_keys_object(estimate_log2_keysize(used),
4317+
unicode);
4318+
if (keys == NULL) {
4319+
return NULL;
4320+
}
4321+
4322+
Py_ssize_t n = 0;
4323+
if (unicode) {
4324+
PyDictUnicodeEntry *src = DK_UNICODE_ENTRIES(oldkeys);
4325+
PyDictUnicodeEntry *dst = DK_UNICODE_ENTRIES(keys);
4326+
for (Py_ssize_t i = 0; n < used; i++) {
4327+
while (src[i].me_value == NULL) {
4328+
i++;
4329+
}
4330+
assert(i < oldkeys->dk_nentries);
4331+
dst[n].me_key = Py_NewRef(src[i].me_key);
4332+
dst[n].me_value = Py_NewRef(src[i].me_value);
4333+
n++;
4334+
}
4335+
assert(n == used);
4336+
build_indices_unicode(keys, dst, n);
4337+
}
4338+
else {
4339+
PyDictKeyEntry *src = DK_ENTRIES(oldkeys);
4340+
PyDictKeyEntry *dst = DK_ENTRIES(keys);
4341+
for (Py_ssize_t i = 0; n < used; i++) {
4342+
while (src[i].me_value == NULL) {
4343+
i++;
4344+
}
4345+
assert(i < oldkeys->dk_nentries);
4346+
dst[n].me_key = Py_NewRef(src[i].me_key);
4347+
dst[n].me_hash = src[i].me_hash;
4348+
dst[n].me_value = Py_NewRef(src[i].me_value);
4349+
n++;
4350+
}
4351+
assert(n == used);
4352+
build_indices_generic(keys, dst, n);
4353+
}
4354+
keys->dk_nentries = n;
4355+
keys->dk_usable -= n;
4356+
return keys;
4357+
}
4358+
42994359
static int
43004360
dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **dupkey)
43014361
{
@@ -4315,15 +4375,18 @@ dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **
43154375
override = 1;
43164376
PyDictKeysObject *okeys = other->ma_keys;
43174377

4318-
// If other is clean, combined, and just allocated, just clone it.
4319-
if (mp->ma_values == NULL &&
4320-
other->ma_values == NULL &&
4321-
other->ma_used == okeys->dk_nentries &&
4322-
(DK_LOG_SIZE(okeys) == PyDict_LOG_MINSIZE ||
4323-
USABLE_FRACTION(DK_SIZE(okeys)/2) < other->ma_used)
4324-
) {
4378+
// Clone a combined table directly, compacting deleted entries.
4379+
if (mp->ma_values == NULL && other->ma_values == NULL) {
43254380
_PyDict_NotifyEvent(PyDict_EVENT_CLONED, mp, (PyObject *)other, NULL);
4326-
PyDictKeysObject *keys = clone_combined_dict_keys(other);
4381+
PyDictKeysObject *keys;
4382+
if (other->ma_used == okeys->dk_nentries &&
4383+
(DK_LOG_SIZE(okeys) == PyDict_LOG_MINSIZE ||
4384+
USABLE_FRACTION(DK_SIZE(okeys)/2) < other->ma_used)) {
4385+
keys = clone_combined_dict_keys(other);
4386+
}
4387+
else {
4388+
keys = clone_compact_dict_keys(other);
4389+
}
43274390
if (keys == NULL)
43284391
return -1;
43294392

@@ -4619,25 +4682,17 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict)
46194682
return (PyObject *)split_copy;
46204683
}
46214684

4622-
if (Py_TYPE(mp)->tp_iter == dict_iter &&
4623-
mp->ma_values == NULL &&
4624-
(mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
4685+
if (Py_TYPE(mp)->tp_iter == dict_iter && mp->ma_values == NULL)
46254686
{
4626-
/* Use fast-copy if:
4627-
4628-
(1) type(mp) doesn't override tp_iter; and
4629-
4630-
(2) 'mp' is not a split-dict; and
4631-
4632-
(3) if 'mp' is non-compact ('del' operation does not resize dicts),
4633-
do fast-copy only if it has at most 1/3 non-used keys.
4634-
4635-
The last condition (3) is important to guard against a pathological
4636-
case when a large dict is almost emptied with multiple del/pop
4637-
operations and copied after that. In cases like this, we defer to
4638-
PyDict_Merge, which produces a compacted copy.
4639-
*/
4640-
PyDictKeysObject *keys = clone_combined_dict_keys(mp);
4687+
/* Clone compact tables as-is, but omit deleted entries from sparse
4688+
tables so copying an almost emptied dict stays inexpensive. */
4689+
PyDictKeysObject *keys;
4690+
if (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3) {
4691+
keys = clone_combined_dict_keys(mp);
4692+
}
4693+
else {
4694+
keys = clone_compact_dict_keys(mp);
4695+
}
46414696
if (keys == NULL) {
46424697
return NULL;
46434698
}

0 commit comments

Comments
 (0)