Skip to content

Commit eea9266

Browse files
committed
gh-161524: Preserve fast-copy rationale for sparse dictionaries
1 parent 92f75ad commit eea9266

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

‎Objects/dictobject.c‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4684,8 +4684,21 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict)
46844684

46854685
if (Py_TYPE(mp)->tp_iter == dict_iter && mp->ma_values == NULL)
46864686
{
4687-
/* Clone compact tables as-is, but omit deleted entries from sparse
4688-
tables so copying an almost emptied dict stays inexpensive. */
4687+
/* Use fast-copy if:
4688+
4689+
(1) type(mp) doesn't override tp_iter; and
4690+
4691+
(2) 'mp' is not a split-dict.
4692+
4693+
Dicts don't compact themselves on del/pop operations, so a large
4694+
dict can have very few live entries. Previously, fast-copy was
4695+
restricted to tables with at most 1/3 non-used entries; otherwise
4696+
PyDict_Merge produced a compacted copy.
4697+
4698+
Clone tables with few deleted entries as-is. For sparse tables,
4699+
copy only live entries into a compact table instead of retaining
4700+
deleted slots or reinserting entries one by one.
4701+
*/
46894702
PyDictKeysObject *keys;
46904703
if (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3) {
46914704
keys = clone_combined_dict_keys(mp);

0 commit comments

Comments
 (0)