diff --git a/Include/cpython/bytesobject.h b/Include/cpython/bytesobject.h index 6b3f55224f..ae86e34634 100644 --- a/Include/cpython/bytesobject.h +++ b/Include/cpython/bytesobject.h @@ -4,13 +4,11 @@ typedef struct { PyObject_VAR_HEAD - Py_hash_t ob_shash; char ob_sval[1]; /* Invariants: * ob_sval contains space for 'ob_size+1' elements. * ob_sval[ob_size] == 0. - * ob_shash is the hash of the byte string or -1 if not computed yet. */ } PyBytesObject; diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index 8f62343443..cec6797906 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -85,7 +85,6 @@ extern "C" { #define _PyBytes_SIMPLE_INIT(CH, LEN) \ { \ _PyVarObject_IMMORTAL_INIT(&PyBytes_Type, LEN), \ - .ob_shash = -1, \ .ob_sval = { CH }, \ } #define _PyBytes_CHAR_INIT(CH) \ diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 3d8a21696d..d702164387 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -104,7 +104,6 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc) return PyErr_NoMemory(); } _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size); - op->ob_shash = -1; if (!use_calloc) { op->ob_sval[size] = '\0'; } @@ -168,7 +167,6 @@ PyBytes_FromString(const char *str) return PyErr_NoMemory(); } _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size); - op->ob_shash = -1; memcpy(op->ob_sval, str, size+1); return (PyObject *) op; } @@ -1427,7 +1425,6 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n) return PyErr_NoMemory(); } _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size); - op->ob_shash = -1; op->ob_sval[size] = '\0'; if (Py_SIZE(a) == 1 && n > 0) { memset(op->ob_sval, a->ob_sval[0] , n); @@ -1543,11 +1540,7 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op) static Py_hash_t bytes_hash(PyBytesObject *a) { - if (a->ob_shash == -1) { - /* Can't fail */ - a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a)); - } - return a->ob_shash; + return _Py_HashBytes(a->ob_sval, Py_SIZE(a)); } static PyObject* @@ -2849,8 +2842,6 @@ bytes_subtype_new(PyTypeObject *type, PyObject *tmp) if (pnew != NULL) { memcpy(PyBytes_AS_STRING(pnew), PyBytes_AS_STRING(tmp), n+1); - ((PyBytesObject *)pnew)->ob_shash = - ((PyBytesObject *)tmp)->ob_shash; } return pnew; } @@ -3032,7 +3023,6 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) sv = (PyBytesObject *) *pv; Py_SET_SIZE(sv, newsize); sv->ob_sval[newsize] = '\0'; - sv->ob_shash = -1; /* invalidate cached hash value */ return 0; error: *pv = 0; diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py index 0edf3af71d..b3ae49b078 100644 --- a/Tools/scripts/deepfreeze.py +++ b/Tools/scripts/deepfreeze.py @@ -157,11 +157,9 @@ def generate_bytes(self, name: str, b: bytes) -> str: with self.indent(): with self.block("struct"): self.write("PyObject_VAR_HEAD") - self.write("Py_hash_t ob_shash;") self.write(f"char ob_sval[{len(b) + 1}];") with self.block(f"{name} =", ";"): self.object_var_head("PyBytes_Type", len(b)) - self.write(".ob_shash = -1,") self.write(f".ob_sval = {make_string_literal(b)},") return f"& {name}.ob_base.ob_base"