[Python-checkins] bpo-40575: Avoid unnecessary overhead in _PyDict_GetItemIdWithError() (GH-20018)

scoder webhook-mailer at python.org
Mon May 11 00:04:39 EDT 2020


https://github.com/python/cpython/commit/6067d4bc3ce5ff4cfa5b47ceecc84a3941bc031c
commit: 6067d4bc3ce5ff4cfa5b47ceecc84a3941bc031c
branch: master
author: scoder <stefan_ml at behnel.de>
committer: GitHub <noreply at github.com>
date: 2020-05-11T06:04:31+02:00
summary:

bpo-40575: Avoid unnecessary overhead in _PyDict_GetItemIdWithError() (GH-20018)

Avoid unnecessary overhead in _PyDict_GetItemIdWithError() by calling
_PyDict_GetItem_KnownHash() instead of the more generic PyDict_GetItemWithError(),
since we already know the hash of interned strings.

files:
M Objects/dictobject.c

diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index fa35d16478f63..809a5ed778737 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1492,7 +1492,9 @@ _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
     kv = _PyUnicode_FromId(key); /* borrowed */
     if (kv == NULL)
         return NULL;
-    return PyDict_GetItemWithError(dp, kv);
+    Py_hash_t hash = ((PyASCIIObject *) kv)->hash;
+    assert (hash != -1);  /* interned strings have their hash value initialised */
+    return _PyDict_GetItem_KnownHash(dp, kv, hash);
 }
 
 PyObject *



More information about the Python-checkins mailing list