Skip to content

Commit da422e0

Browse files
Move the flag to the dict keys object.
1 parent 8205f62 commit da422e0

6 files changed

Lines changed: 14 additions & 18 deletions

File tree

Include/dictobject.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ typedef struct {
2626
/* Number of items in the dictionary */
2727
Py_ssize_t ma_used;
2828

29-
/* Whether OrderedDict's cache is synchronized with dict table */
30-
unsigned int ma_clean: 1;
31-
3229
/* Dictionary version: globally unique, value change each time
3330
the dictionary is modified */
3431
uint64_t ma_version_tag;

Lib/test/test_ordered_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ def test_sizeof_exact(self):
676676
size = support.calcobjsize
677677
check = self.check_sizeof
678678

679-
basicsize = size('niQ2P' + '3Pn2P') + calcsize('2nP2n')
679+
basicsize = size('nQ2P' + '3Pn2P') + calcsize('2nP2ni0P')
680680

681681
entrysize = calcsize('n2P')
682682
p = calcsize('P')

Lib/test/test_sys.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,9 @@ def inner():
965965
# method-wrapper (descriptor object)
966966
check({}.__iter__, size('2P'))
967967
# dict
968-
check({}, size('niQ2P') + calcsize('2nP2n') + 8 + (8*2//3)*calcsize('n2P'))
968+
check({}, size('nQ2P') + calcsize('2nP2ni0P') + 8 + (8*2//3)*calcsize('n2P'))
969969
longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
970-
check(longdict, size('niQ2P') + calcsize('2nP2n') + 16 + (16*2//3)*calcsize('n2P'))
970+
check(longdict, size('nQ2P') + calcsize('2nP2ni0P') + 16 + (16*2//3)*calcsize('n2P'))
971971
# dictionary-keyview
972972
check({}.keys(), size('P'))
973973
# dictionary-valueview
@@ -1128,15 +1128,15 @@ def delx(self): del self.__x
11281128
'4P')
11291129
class newstyleclass(object): pass
11301130
# Separate block for PyDictKeysObject with 8 keys and 5 entries
1131-
check(newstyleclass, s + calcsize("2nP2n0P") + 8 + 5*calcsize("n2P"))
1131+
check(newstyleclass, s + calcsize("2nP2ni0P") + 8 + 5*calcsize("n2P"))
11321132
# dict with shared keys
1133-
check(newstyleclass().__dict__, size('niQ2P') + 5*self.P)
1133+
check(newstyleclass().__dict__, size('nQ2P') + 5*self.P)
11341134
o = newstyleclass()
11351135
o.a = o.b = o.c = o.d = o.e = o.f = o.g = o.h = 1
11361136
# Separate block for PyDictKeysObject with 16 keys and 10 entries
1137-
check(newstyleclass, s + calcsize("2nP2n0P") + 16 + 10*calcsize("n2P"))
1137+
check(newstyleclass, s + calcsize("2nP2ni0P") + 16 + 10*calcsize("n2P"))
11381138
# dict with shared keys
1139-
check(newstyleclass().__dict__, size('niQ2P') + 10*self.P)
1139+
check(newstyleclass().__dict__, size('nQ2P') + 10*self.P)
11401140
# unicode
11411141
# each tuple contains a string and its expected character size
11421142
# don't put any static strings here, as they may contain

Objects/dict-common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ struct _dictkeysobject {
4646
/* Number of used entries in dk_entries. */
4747
Py_ssize_t dk_nentries;
4848

49+
/* Whether OrderedDict's cache is synchronized with dict table */
50+
unsigned int dk_clean;
51+
4952
/* Actual hash table of dk_size entries. It holds indices in dk_entries,
5053
or DKIX_EMPTY(-1) or DKIX_DUMMY(-2).
5154

Objects/dictobject.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ static PyDictKeysObject *new_keys_object(Py_ssize_t size)
544544
dk->dk_usable = usable;
545545
dk->dk_lookup = lookdict_unicode_nodummy;
546546
dk->dk_nentries = 0;
547+
dk->dk_clean = 0;
547548
memset(&dk->dk_indices.as_1[0], 0xff, es * size);
548549
memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
549550
return dk;
@@ -591,7 +592,6 @@ new_dict(PyDictKeysObject *keys, PyObject **values)
591592
mp->ma_keys = keys;
592593
mp->ma_values = values;
593594
mp->ma_used = 0;
594-
mp->ma_clean = 0;
595595
mp->ma_version_tag = DICT_NEXT_VERSION();
596596
assert(_PyDict_CheckConsistency(mp));
597597
return (PyObject *)mp;
@@ -1087,13 +1087,12 @@ dictresize(PyDictObject *mp, Py_ssize_t minsize)
10871087
mp->ma_keys = oldkeys;
10881088
return -1;
10891089
}
1090-
mp->ma_clean = 0;
10911090
// New table must be large enough.
10921091
assert(mp->ma_keys->dk_usable >= mp->ma_used);
10931092
if (oldkeys->dk_lookup == lookdict)
10941093
mp->ma_keys->dk_lookup = lookdict;
10951094
}
1096-
mp->ma_clean = 0;
1095+
mp->ma_keys->dk_clean = 0;
10971096

10981097
numentries = mp->ma_used;
10991098
oldentries = DK_ENTRIES(oldkeys);
@@ -1587,7 +1586,6 @@ PyDict_Clear(PyObject *op)
15871586
mp->ma_keys = Py_EMPTY_KEYS;
15881587
mp->ma_values = empty_values;
15891588
mp->ma_used = 0;
1590-
mp->ma_clean = 0;
15911589
mp->ma_version_tag = DICT_NEXT_VERSION();
15921590
/* ...then clear the keys and values */
15931591
if (oldvalues != NULL) {
@@ -2511,7 +2509,6 @@ PyDict_Copy(PyObject *o)
25112509
split_copy->ma_values = newvalues;
25122510
split_copy->ma_keys = mp->ma_keys;
25132511
split_copy->ma_used = mp->ma_used;
2514-
split_copy->ma_clean = 0;
25152512
DK_INCREF(mp->ma_keys);
25162513
for (i = 0, n = size; i < n; i++) {
25172514
PyObject *value = mp->ma_values[i];
@@ -3099,7 +3096,6 @@ dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
30993096
_PyObject_GC_UNTRACK(d);
31003097

31013098
d->ma_used = 0;
3102-
d->ma_clean = 0;
31033099
d->ma_version_tag = DICT_NEXT_VERSION();
31043100
d->ma_keys = new_keys_object(PyDict_MINSIZE);
31053101
if (d->ma_keys == NULL) {

Objects/odictobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ _odict_resize(PyODictObject *od) {
584584
/* Replace the old fast nodes table. */
585585
_odict_free_fast_nodes(od);
586586
od->od_fast_nodes = fast_nodes;
587-
((PyDictObject *)od)->ma_clean = 1;
587+
((PyDictObject *)od)->ma_keys->dk_clean = 1;
588588
return 0;
589589
}
590590

@@ -593,7 +593,7 @@ static Py_ssize_t
593593
_odict_get_index(PyODictObject *od, PyObject *key, Py_hash_t hash)
594594
{
595595
/* Ensure od_fast_nodes and dk_entries are in sync. */
596-
if (!((PyDictObject *)od)->ma_clean) {
596+
if (!((PyDictObject *)od)->ma_keys->dk_clean) {
597597
int resize_res = _odict_resize(od);
598598
if (resize_res < 0)
599599
return -1;

0 commit comments

Comments
 (0)