gh-142889: Restructure PyDictKeysObject memory layout for simpler access by clintonsteiner · Pull Request #145097 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
gh-142889: Address PR review comments on PyDictKeysObject restructure
  replace char dk_indices[] with explicit union named dk_entries
  because union has non-zero size, update allocation and size reporting
  to use offsetof in place of sizeof, keeping actual memory footprint unchanged
  remove _DK_INDICES_END(), inline uses in macros
  adds PyDictKeysObject pointer note to layout diagram for dictobject.c
  • Loading branch information
clintonsteiner committed Mar 11, 2026
commit e6754e95a831f96f2d521a84b99fd35f68125f67
41 changes: 11 additions & 30 deletions Include/internal/pycore_dict.h
4 changes: 2 additions & 2 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1942,12 +1942,12 @@ dict_check_indices_layout(PyObject *self, PyObject *arg)

bool ok = true;
ok &= (header == base + indices_size);
ok &= (entries == header + sizeof(PyDictKeysObject));
ok &= (entries == header + offsetof(PyDictKeysObject, dk_entries));

size_t index_bytes = dict_index_bytes_for_keys(keys);
char *idx_base = (char *)_DK_INDICES_BASE(keys);
/* Index 0 is stored immediately before the header. */
char *idx0 = (char *)_DK_INDICES_END(keys) - (ptrdiff_t)index_bytes;
char *idx0 = (char *)keys - (ptrdiff_t)index_bytes;
ok &= (idx0 == idx_base + indices_size - (ptrdiff_t)index_bytes);

return PyBool_FromLong(ok);
Expand Down
18 changes: 9 additions & 9 deletions Objects/dictobject.c
Loading