Message 276083 - Python tracker (original) (raw)
Xiang: Preserving insertion order in shared-key dict is possible, but it's hard. If we allow it, we should add tons of test for shared-key dict.
I'll describe why it's harder than you think.
Current implementation allow insertion to split table only when:
/* When insertion order is different from shared key, we can't share
* the key anymore. Convert this instance to combine table.
*/
if (_PyDict_HasSplitTable(mp) &&
((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
(ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
if (insertion_resize(mp) < 0) {ix >= 0 && *value_addr == NULL means it's pending slot.
mp->ma_used == ix ensure insertion order. And if we allow deletion, this check will be broken.
For example:
a, b = C() a.a, a.b, a.c = 1, 2, 3 # shared-key order b.a, b.b, b.c = 1, 2, 3 # same order, no problem
del b.a # mp->ma_used = 2 del b.b # mp->ma_used = 1
b.b = 42 # It's OK because mp->ma_used == ix == 1. Keep sharing.
assert(list(b.dict.keys()) == ["c", "b"]) # AssertionError! Actual order is [(PENDING "a",) "b", "c"]