@@ -601,6 +601,7 @@ struct lru_cache_object;
601601typedef struct lru_list_elem {
602602 PyObject_HEAD
603603 struct lru_list_elem * prev , * next ; /* borrowed links */
604+ Py_hash_t hash ;
604605 PyObject * key , * result ;
605606} lru_list_elem ;
606607
@@ -762,10 +763,14 @@ static PyObject *
762763infinite_lru_cache_wrapper (lru_cache_object * self , PyObject * args , PyObject * kwds )
763764{
764765 PyObject * result ;
766+ Py_hash_t hash ;
765767 PyObject * key = lru_cache_make_key (args , kwds , self -> typed );
766768 if (!key )
767769 return NULL ;
768- result = PyDict_GetItemWithError (self -> cache , key );
770+ hash = PyObject_Hash (key );
771+ if (hash == -1 )
772+ return NULL ;
773+ result = _PyDict_GetItem_KnownHash (self -> cache , key , hash );
769774 if (result ) {
770775 Py_INCREF (result );
771776 self -> hits ++ ;
@@ -781,7 +786,7 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
781786 Py_DECREF (key );
782787 return NULL ;
783788 }
784- if (PyDict_SetItem (self -> cache , key , result ) < 0 ) {
789+ if (_PyDict_SetItem_KnownHash (self -> cache , key , result , hash ) < 0 ) {
785790 Py_DECREF (result );
786791 Py_DECREF (key );
787792 return NULL ;
@@ -813,11 +818,15 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
813818{
814819 lru_list_elem * link ;
815820 PyObject * key , * result ;
821+ Py_hash_t hash ;
816822
817823 key = lru_cache_make_key (args , kwds , self -> typed );
818824 if (!key )
819825 return NULL ;
820- link = (lru_list_elem * )PyDict_GetItemWithError (self -> cache , key );
826+ hash = PyObject_Hash (key );
827+ if (hash == -1 )
828+ return NULL ;
829+ link = (lru_list_elem * )_PyDict_GetItem_KnownHash (self -> cache , key , hash );
821830 if (link ) {
822831 lru_cache_extricate_link (link );
823832 lru_cache_append_link (self , link );
@@ -845,7 +854,8 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
845854 /* Remove it from the cache.
846855 The cache dict holds one reference to the link,
847856 and the linked list holds yet one reference to it. */
848- if (PyDict_DelItem (self -> cache , link -> key ) < 0 ) {
857+ if (_PyDict_DelItem_KnownHash (self -> cache , link -> key ,
858+ link -> hash ) < 0 ) {
849859 lru_cache_append_link (self , link );
850860 Py_DECREF (key );
851861 Py_DECREF (result );
@@ -859,9 +869,11 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
859869 oldkey = link -> key ;
860870 oldresult = link -> result ;
861871
872+ link -> hash = hash ;
862873 link -> key = key ;
863874 link -> result = result ;
864- if (PyDict_SetItem (self -> cache , key , (PyObject * )link ) < 0 ) {
875+ if (_PyDict_SetItem_KnownHash (self -> cache , key , (PyObject * )link ,
876+ hash ) < 0 ) {
865877 Py_DECREF (link );
866878 Py_DECREF (oldkey );
867879 Py_DECREF (oldresult );
@@ -881,10 +893,12 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
881893 return NULL ;
882894 }
883895
896+ link -> hash = hash ;
884897 link -> key = key ;
885898 link -> result = result ;
886899 _PyObject_GC_TRACK (link );
887- if (PyDict_SetItem (self -> cache , key , (PyObject * )link ) < 0 ) {
900+ if (_PyDict_SetItem_KnownHash (self -> cache , key , (PyObject * )link ,
901+ hash ) < 0 ) {
888902 Py_DECREF (link );
889903 return NULL ;
890904 }
0 commit comments