gh-142889: Improve layout of dictionary keys by brijkapadia · Pull Request #150640 · python/cpython · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
51 changes: 26 additions & 25 deletions Include/internal/pycore_dict.h
15 changes: 15 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,21 @@ def __hash__(self):
self.assertEqual(dict_getitem_knownhash(d, k1, hash(k1)), 1)
self.assertRaises(Exc, dict_getitem_knownhash, d, k2, hash(k2))

@support.cpython_only
def test_dict_keys_layout(self):
_testinternalcapi = import_helper.import_module('_testinternalcapi')
check_layout = _testinternalcapi.dict_keys_layout

for i in range(4):
self.assertTrue(check_layout({j: j for j in range(10**i)}))

@support.cpython_only
def test_dict_keys_to_base(self):
_testinternalcapi = import_helper.import_module('_testinternalcapi')
check_base = _testinternalcapi.dict_keys_to_base

for i in range(4):
self.assertTrue(check_base({j: j for j in range(10**i)}))

from test import mapping_tests

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update the memory layout of how :class:`dict` handles the memory of the keys to improve performance.
2 changes: 1 addition & 1 deletion Modules/Setup.stdlib.in
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
@MODULE_XXSUBTYPE_TRUE@xxsubtype xxsubtype.c
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c _testinternalcapi/complex.c _testinternalcapi/interpreter.c _testinternalcapi/tokenizer.c _testinternalcapi/tuple.c _testinternalcapi/typecache.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c _testinternalcapi/complex.c _testinternalcapi/interpreter.c _testinternalcapi/tokenizer.c _testinternalcapi/tuple.c _testinternalcapi/typecache.c _testinternalcapi/dict.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/modsupport.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c _testcapi/frame.c _testcapi/type.c _testcapi/function.c _testcapi/module.c _testcapi/weakref.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/capsule.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/slots.c _testlimitedcapi/sys.c _testlimitedcapi/threadstate.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c _testlimitedcapi/weakref.c _testlimitedcapi/run.c _testlimitedcapi/type.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
Expand Down
3 changes: 3 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3428,6 +3428,9 @@ module_exec(PyObject *module)
if (_PyTestInternalCapi_Init_TypeCache(module) < 0) {
return 1;
}
if (_PyTestInternalCapi_Init_Dict(module) < 0) {
return 1;
}

Py_ssize_t sizeof_gc_head = 0;
#ifndef Py_GIL_DISABLED
Expand Down
50 changes: 50 additions & 0 deletions Modules/_testinternalcapi/dict.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "parts.h"

#include "pycore_dict.h"

static PyObject*
dict_keys_layout(PyObject *self, PyObject *arg)
{
PyDictObject *mp = (PyDictObject *)arg;
PyDictKeysObject *keys = mp->ma_keys;

size_t indices_size = DK_INDEX_BYTES(keys);

char *base = _DK_INDICES(keys);
char *header = (char *)keys;
char *entries = (char *)_DK_ENTRIES(keys);

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

return PyBool_FromLong(ok);
}

static PyObject*
dict_keys_to_base(PyObject *self, PyObject *arg)
{
PyDictObject *mp = (PyDictObject *)arg;
PyDictKeysObject *keys = mp->ma_keys;

void *base = _DK_INDICES(keys);
size_t indices_size = DK_INDEX_BYTES(keys);
bool ok = _DK_FROM_BASE(base, indices_size) == keys;

return PyBool_FromLong(ok);
}

static PyMethodDef test_methods[] = {
{"dict_keys_layout", dict_keys_layout, METH_O},
{"dict_keys_to_base", dict_keys_to_base, METH_O},
{NULL},
};

int
_PyTestInternalCapi_Init_Dict(PyObject *m)
{
if (PyModule_AddFunctions(m, test_methods) < 0) {
return -1;
}
return 0;
}
1 change: 1 addition & 0 deletions Modules/_testinternalcapi/parts.h
Loading
Loading