Skip to content

Commit 4a6fa89

Browse files
Remove dead code in _PyDict_GetItemHint and rename to _PyDict_LookupIndex (GH-95948)
1 parent 586fc02 commit 4a6fa89

File tree

3 files changed

+10
-51
lines changed

3 files changed

+10
-51
lines changed

Include/internal/pycore_dict.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ extern Py_ssize_t _PyDict_KeysSize(PyDictKeysObject *keys);
6262
*/
6363
extern Py_ssize_t _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
6464

65-
extern Py_ssize_t _PyDict_GetItemHint(PyDictObject *, PyObject *, Py_ssize_t, PyObject **);
65+
extern Py_ssize_t _PyDict_LookupIndex(PyDictObject *, PyObject *);
6666
extern Py_ssize_t _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject *key);
6767
extern PyObject *_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *);
6868

Objects/dictobject.c

+3-41
Original file line numberDiff line numberDiff line change
@@ -1686,50 +1686,12 @@ PyDict_GetItem(PyObject *op, PyObject *key)
16861686
}
16871687

16881688
Py_ssize_t
1689-
_PyDict_GetItemHint(PyDictObject *mp, PyObject *key,
1690-
Py_ssize_t hint, PyObject **value)
1689+
_PyDict_LookupIndex(PyDictObject *mp, PyObject *key)
16911690
{
1692-
assert(*value == NULL);
1691+
PyObject *value;
16931692
assert(PyDict_CheckExact((PyObject*)mp));
16941693
assert(PyUnicode_CheckExact(key));
16951694

1696-
if (hint >= 0 && hint < mp->ma_keys->dk_nentries) {
1697-
PyObject *res = NULL;
1698-
1699-
if (DK_IS_UNICODE(mp->ma_keys)) {
1700-
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(mp->ma_keys) + (size_t)hint;
1701-
if (ep->me_key == key) {
1702-
if (mp->ma_keys->dk_kind == DICT_KEYS_SPLIT) {
1703-
assert(mp->ma_values != NULL);
1704-
res = mp->ma_values->values[(size_t)hint];
1705-
}
1706-
else {
1707-
res = ep->me_value;
1708-
}
1709-
if (res != NULL) {
1710-
*value = res;
1711-
return hint;
1712-
}
1713-
}
1714-
}
1715-
else {
1716-
PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys) + (size_t)hint;
1717-
if (ep->me_key == key) {
1718-
if (mp->ma_keys->dk_kind == DICT_KEYS_SPLIT) {
1719-
assert(mp->ma_values != NULL);
1720-
res = mp->ma_values->values[(size_t)hint];
1721-
}
1722-
else {
1723-
res = ep->me_value;
1724-
}
1725-
if (res != NULL) {
1726-
*value = res;
1727-
return hint;
1728-
}
1729-
}
1730-
}
1731-
}
1732-
17331695
Py_hash_t hash = unicode_get_hash(key);
17341696
if (hash == -1) {
17351697
hash = PyObject_Hash(key);
@@ -1738,7 +1700,7 @@ _PyDict_GetItemHint(PyDictObject *mp, PyObject *key,
17381700
}
17391701
}
17401702

1741-
return _Py_dict_lookup(mp, key, hash, value);
1703+
return _Py_dict_lookup(mp, key, hash, &value);
17421704
}
17431705

17441706
/* Same as PyDict_GetItemWithError() but with hash supplied by caller.

Python/specialize.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ specialize_module_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
511511
{
512512
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
513513
PyModuleObject *m = (PyModuleObject *)owner;
514-
PyObject *value = NULL;
515514
assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
516515
PyDictObject *dict = (PyDictObject *)m->md_dict;
517516
if (dict == NULL) {
@@ -522,14 +521,13 @@ specialize_module_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
522521
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT);
523522
return -1;
524523
}
525-
Py_ssize_t index = _PyDict_GetItemHint(dict, &_Py_ID(__getattr__), -1,
526-
&value);
524+
Py_ssize_t index = _PyDict_LookupIndex(dict, &_Py_ID(__getattr__));
527525
assert(index != DKIX_ERROR);
528526
if (index != DKIX_EMPTY) {
529527
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND);
530528
return -1;
531529
}
532-
index = _PyDict_GetItemHint(dict, name, -1, &value);
530+
index = _PyDict_LookupIndex(dict, name);
533531
assert (index != DKIX_ERROR);
534532
if (index != (uint16_t)index) {
535533
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_RANGE);
@@ -703,14 +701,13 @@ specialize_dict_access(
703701
return 0;
704702
}
705703
// We found an instance with a __dict__.
706-
PyObject *value = NULL;
707-
Py_ssize_t hint =
708-
_PyDict_GetItemHint(dict, name, -1, &value);
709-
if (hint != (uint16_t)hint) {
704+
Py_ssize_t index =
705+
_PyDict_LookupIndex(dict, name);
706+
if (index != (uint16_t)index) {
710707
SPECIALIZATION_FAIL(base_op, SPEC_FAIL_OUT_OF_RANGE);
711708
return 0;
712709
}
713-
cache->index = (uint16_t)hint;
710+
cache->index = (uint16_t)index;
714711
write_u32(cache->version, type->tp_version_tag);
715712
_Py_SET_OPCODE(*instr, hint_op);
716713
}

0 commit comments

Comments
 (0)