Skip to content

Commit add1804

Browse files
committed
pass keys_size to _PyDict_NewKeysForClass
1 parent 6af0e7f commit add1804

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

Include/internal/pycore_dict.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ typedef struct {
8181
PyObject *me_value; /* This field is only meaningful for combined tables */
8282
} PyDictUnicodeEntry;
8383

84-
extern PyDictKeysObject *_PyDict_NewKeysForClass(void);
84+
extern PyDictKeysObject *_PyDict_NewKeysForClass(Py_ssize_t keys_size);
8585
extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
8686

8787
/* Gets a version number unique to the current state of the keys of dict, if possible.

Objects/dictobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6522,18 +6522,18 @@ dictvalues_reversed(PyObject *self, PyObject *Py_UNUSED(ignored))
65226522
/* Returns NULL if cannot allocate a new PyDictKeysObject,
65236523
but does not set an error */
65246524
PyDictKeysObject *
6525-
_PyDict_NewKeysForClass(void)
6525+
_PyDict_NewKeysForClass(Py_ssize_t keys_size)
65266526
{
65276527
PyInterpreterState *interp = _PyInterpreterState_GET();
65286528
PyDictKeysObject *keys = new_keys_object(
6529-
interp, NEXT_LOG2_SHARED_KEYS_MAX_SIZE, 1);
6529+
interp, estimate_log2_keysize(keys_size), 1);
65306530
if (keys == NULL) {
65316531
PyErr_Clear();
65326532
}
65336533
else {
65346534
assert(keys->dk_nentries == 0);
65356535
/* Set to max size+1 as it will shrink by one before each new object */
6536-
keys->dk_usable = SHARED_KEYS_MAX_SIZE;
6536+
keys->dk_usable = keys_size;
65376537
keys->dk_kind = DICT_KEYS_SPLIT;
65386538
}
65396539
return keys;

Objects/typeobject.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7760,6 +7760,12 @@ type_ready_set_new(PyTypeObject *type, int rerunbuiltin)
77607760
return 0;
77617761
}
77627762

7763+
static Py_ssize_t
7764+
expected_number_of_shared_keys(PyTypeObject *type)
7765+
{
7766+
return SHARED_KEYS_MAX_SIZE;
7767+
}
7768+
77637769
static int
77647770
type_ready_managed_dict(PyTypeObject *type)
77657771
{
@@ -7775,7 +7781,7 @@ type_ready_managed_dict(PyTypeObject *type)
77757781
}
77767782
PyHeapTypeObject* et = (PyHeapTypeObject*)type;
77777783
if (et->ht_cached_keys == NULL) {
7778-
et->ht_cached_keys = _PyDict_NewKeysForClass();
7784+
et->ht_cached_keys = _PyDict_NewKeysForClass(expected_number_of_shared_keys(type));
77797785
if (et->ht_cached_keys == NULL) {
77807786
PyErr_NoMemory();
77817787
return -1;

0 commit comments

Comments
 (0)