From 7271ad8524e5adda7422f3ed69a91bd53badac39 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 9 May 2020 19:50:26 +0200 Subject: [PATCH 1/2] bpo-40575: 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. --- Objects/dictobject.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index fa35d16478f635..27a74dec695037 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1488,11 +1488,14 @@ PyDict_GetItemWithError(PyObject *op, PyObject *key) PyObject * _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key) { + Py_hash_t hash; PyObject *kv; kv = _PyUnicode_FromId(key); /* borrowed */ if (kv == NULL) return NULL; - return PyDict_GetItemWithError(dp, kv); + hash = ((PyASCIIObject *) kv)->hash; + assert (hash != -1); /* interned strings have their hash value initialised */ + return _PyDict_GetItem_KnownHash(dp, kv, hash); } PyObject * From c7e07c32d815ddd5fbe90f444d529690afe734c7 Mon Sep 17 00:00:00 2001 From: scoder Date: Mon, 11 May 2020 05:24:21 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Inada Naoki --- Objects/dictobject.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 27a74dec695037..809a5ed7787370 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1488,12 +1488,11 @@ PyDict_GetItemWithError(PyObject *op, PyObject *key) PyObject * _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key) { - Py_hash_t hash; PyObject *kv; kv = _PyUnicode_FromId(key); /* borrowed */ if (kv == NULL) return NULL; - hash = ((PyASCIIObject *) kv)->hash; + Py_hash_t hash = ((PyASCIIObject *) kv)->hash; assert (hash != -1); /* interned strings have their hash value initialised */ return _PyDict_GetItem_KnownHash(dp, kv, hash); }