Remove dead code in _PyDict_GetItemHint and rename to _PyDict_LookupIndex (GH-95948)

This commit is contained in:
Matthias Görgens 2022-08-18 17:19:21 +08:00 committed by GitHub
parent 586fc02be5
commit 4a6fa89465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 51 deletions

View File

@ -62,7 +62,7 @@ extern Py_ssize_t _PyDict_KeysSize(PyDictKeysObject *keys);
*/ */
extern Py_ssize_t _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr); extern Py_ssize_t _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
extern Py_ssize_t _PyDict_GetItemHint(PyDictObject *, PyObject *, Py_ssize_t, PyObject **); extern Py_ssize_t _PyDict_LookupIndex(PyDictObject *, PyObject *);
extern Py_ssize_t _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject *key); extern Py_ssize_t _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject *key);
extern PyObject *_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *); extern PyObject *_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *);

View File

@ -1686,50 +1686,12 @@ PyDict_GetItem(PyObject *op, PyObject *key)
} }
Py_ssize_t Py_ssize_t
_PyDict_GetItemHint(PyDictObject *mp, PyObject *key, _PyDict_LookupIndex(PyDictObject *mp, PyObject *key)
Py_ssize_t hint, PyObject **value)
{ {
assert(*value == NULL); PyObject *value;
assert(PyDict_CheckExact((PyObject*)mp)); assert(PyDict_CheckExact((PyObject*)mp));
assert(PyUnicode_CheckExact(key)); assert(PyUnicode_CheckExact(key));
if (hint >= 0 && hint < mp->ma_keys->dk_nentries) {
PyObject *res = NULL;
if (DK_IS_UNICODE(mp->ma_keys)) {
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(mp->ma_keys) + (size_t)hint;
if (ep->me_key == key) {
if (mp->ma_keys->dk_kind == DICT_KEYS_SPLIT) {
assert(mp->ma_values != NULL);
res = mp->ma_values->values[(size_t)hint];
}
else {
res = ep->me_value;
}
if (res != NULL) {
*value = res;
return hint;
}
}
}
else {
PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys) + (size_t)hint;
if (ep->me_key == key) {
if (mp->ma_keys->dk_kind == DICT_KEYS_SPLIT) {
assert(mp->ma_values != NULL);
res = mp->ma_values->values[(size_t)hint];
}
else {
res = ep->me_value;
}
if (res != NULL) {
*value = res;
return hint;
}
}
}
}
Py_hash_t hash = unicode_get_hash(key); Py_hash_t hash = unicode_get_hash(key);
if (hash == -1) { if (hash == -1) {
hash = PyObject_Hash(key); hash = PyObject_Hash(key);
@ -1738,7 +1700,7 @@ _PyDict_GetItemHint(PyDictObject *mp, PyObject *key,
} }
} }
return _Py_dict_lookup(mp, key, hash, value); return _Py_dict_lookup(mp, key, hash, &value);
} }
/* Same as PyDict_GetItemWithError() but with hash supplied by caller. /* Same as PyDict_GetItemWithError() but with hash supplied by caller.

View File

@ -511,7 +511,6 @@ specialize_module_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
{ {
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1); _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
PyModuleObject *m = (PyModuleObject *)owner; PyModuleObject *m = (PyModuleObject *)owner;
PyObject *value = NULL;
assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0); assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
PyDictObject *dict = (PyDictObject *)m->md_dict; PyDictObject *dict = (PyDictObject *)m->md_dict;
if (dict == NULL) { if (dict == NULL) {
@ -522,14 +521,13 @@ specialize_module_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT); SPECIALIZATION_FAIL(opcode, SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT);
return -1; return -1;
} }
Py_ssize_t index = _PyDict_GetItemHint(dict, &_Py_ID(__getattr__), -1, Py_ssize_t index = _PyDict_LookupIndex(dict, &_Py_ID(__getattr__));
&value);
assert(index != DKIX_ERROR); assert(index != DKIX_ERROR);
if (index != DKIX_EMPTY) { if (index != DKIX_EMPTY) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND); SPECIALIZATION_FAIL(opcode, SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND);
return -1; return -1;
} }
index = _PyDict_GetItemHint(dict, name, -1, &value); index = _PyDict_LookupIndex(dict, name);
assert (index != DKIX_ERROR); assert (index != DKIX_ERROR);
if (index != (uint16_t)index) { if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_RANGE); SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_RANGE);
@ -703,14 +701,13 @@ specialize_dict_access(
return 0; return 0;
} }
// We found an instance with a __dict__. // We found an instance with a __dict__.
PyObject *value = NULL; Py_ssize_t index =
Py_ssize_t hint = _PyDict_LookupIndex(dict, name);
_PyDict_GetItemHint(dict, name, -1, &value); if (index != (uint16_t)index) {
if (hint != (uint16_t)hint) {
SPECIALIZATION_FAIL(base_op, SPEC_FAIL_OUT_OF_RANGE); SPECIALIZATION_FAIL(base_op, SPEC_FAIL_OUT_OF_RANGE);
return 0; return 0;
} }
cache->index = (uint16_t)hint; cache->index = (uint16_t)index;
write_u32(cache->version, type->tp_version_tag); write_u32(cache->version, type->tp_version_tag);
_Py_SET_OPCODE(*instr, hint_op); _Py_SET_OPCODE(*instr, hint_op);
} }