Skip to content

Commit a4348cc

Browse files
committed
Add documentation to the dict implementation
Issue #27350.
1 parent 58f7c5a commit a4348cc

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

Include/dictobject.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@ typedef struct _dictkeysobject PyDictKeysObject;
2222
*/
2323
typedef struct {
2424
PyObject_HEAD
25+
26+
/* Number of items in the dictionary */
2527
Py_ssize_t ma_used;
28+
2629
PyDictKeysObject *ma_keys;
30+
31+
/* If ma_values is NULL, the table is "combined": keys and values
32+
are stored in ma_keys (and ma_keys->dk_refcnt == 1).
33+
34+
If ma_values is not NULL, the table is splitted:
35+
keys are stored in ma_keys and values are stored in ma_values */
2736
PyObject **ma_values;
2837
} PyDictObject;
2938

Objects/dict-common.h

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,50 @@ typedef Py_ssize_t (*dict_lookup_func)
2222
/* See dictobject.c for actual layout of DictKeysObject */
2323
struct _dictkeysobject {
2424
Py_ssize_t dk_refcnt;
25+
26+
/* Size of the hash table (dk_indices). It must be a power of 2. */
2527
Py_ssize_t dk_size;
28+
29+
/* Function to lookup in the hash table (dk_indices):
30+
31+
- lookdict(): general-purpose, and may return DKIX_ERROR if (and
32+
only if) a comparison raises an exception.
33+
34+
- lookdict_unicode(): specialized to Unicode string keys, comparison of
35+
which can never raise an exception; that function can never return
36+
DKIX_ERROR.
37+
38+
- lookdict_unicode_nodummy(): similar to lookdict_unicode() but further
39+
specialized for Unicode string keys that cannot be the <dummy> value.
40+
41+
- lookdict_split(): Version of lookdict() for split tables. */
2642
dict_lookup_func dk_lookup;
43+
44+
/* Number of usable entries in dk_entries.
45+
0 <= dk_usable <= USABLE_FRACTION(dk_size) */
2746
Py_ssize_t dk_usable;
28-
Py_ssize_t dk_nentries; /* How many entries are used. */
29-
char dk_indices[8]; /* dynamically sized. 8 is minimum. */
47+
48+
/* Number of used entries in dk_entries.
49+
0 <= dk_nentries < dk_size */
50+
Py_ssize_t dk_nentries;
51+
52+
/* Actual hash table of dk_size entries. It holds indices in dk_entries,
53+
or DKIX_EMPTY(-1) or DKIX_DUMMY(-2).
54+
55+
Indices must be: 0 <= indice < USABLE_FRACTION(dk_size).
56+
57+
The size in bytes of an indice depends on dk_size:
58+
59+
- 1 byte if dk_size <= 0xff (char*)
60+
- 2 bytes if dk_size <= 0xffff (int16_t*)
61+
- 4 bytes if dk_size <= 0xffffffff (int32_t*)
62+
- 8 bytes otherwise (Py_ssize_t*)
63+
64+
Dynamically sized, 8 is minimum. */
65+
char dk_indices[8];
66+
67+
/* "PyDictKeyEntry dk_entries[dk_usable];" array follows:
68+
see the DK_ENTRIES() macro */
3069
};
3170

3271
#endif

Objects/dictobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
593593
Christian Tismer.
594594
595595
lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
596-
comparison raises an exception (this was new in Python 2.5).
596+
comparison raises an exception.
597597
lookdict_unicode() below is specialized to string keys, comparison of which can
598598
never raise an exception; that function can never return DKIX_ERROR.
599599
lookdict_unicode_nodummy is further specialized for string keys that cannot be

0 commit comments

Comments
 (0)