diff --git a/Include/tupleobject.h b/Include/tupleobject.h --- a/Include/tupleobject.h +++ b/Include/tupleobject.h @@ -24,6 +24,7 @@ #ifndef Py_LIMITED_API typedef struct { PyObject_VAR_HEAD + Py_hash_t ob_hash; /* Hash value; -1 if not set */ PyObject *ob_item[1]; /* ob_item contains space for 'ob_size' elements. diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -118,6 +118,7 @@ count_tracked++; #endif _PyObject_GC_TRACK(op); + op->ob_hash = -1; return (PyObject *) op; } @@ -332,6 +333,9 @@ register Py_ssize_t len = Py_SIZE(v); register PyObject **p; Py_uhash_t mult = _PyHASH_MULTIPLIER; + if (v->ob_hash != -1) { + return v->ob_hash; + } x = 0x345678UL; p = v->ob_item; while (--len >= 0) { @@ -345,6 +349,7 @@ x += 97531UL; if (x == (Py_uhash_t)-1) x = -2; + v->ob_hash = x; return x; }