gh-131525: Cache the result of tuple_hash (#131529)

* gh-131525: Cache the result of tuple_hash

* Fix debug builds

* Add blurb

* Fix formatting

* Pre-compute empty tuple singleton

* Mostly set the cache within tuple_alloc

* Fixes for TSAN

* Pre-compute empty tuple singleton

* Fix for 32-bit platforms

* Assert that op != NULL in _PyTuple_RESET_HASH_CACHE

* Use FT_ATOMIC_STORE_SSIZE_RELAXED macro

* Update Include/internal/pycore_tuple.h

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>

* Fix alignment

* atomic load

* Update Objects/tupleobject.c

Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>

---------

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
This commit is contained in:
Michael Droettboom 2025-03-27 09:57:06 -04:00 committed by GitHub
parent cf5e438c02
commit 8614f86b71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
102 changed files with 1218 additions and 183 deletions

View file

@ -145,9 +145,11 @@ _io_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kw
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(file), &_Py_ID(mode), &_Py_ID(buffering), &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(newline), &_Py_ID(closefd), &_Py_ID(opener), },
};
#undef NUM_KEYWORDS
@ -369,9 +371,11 @@ _io_open_code(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(path), },
};
#undef NUM_KEYWORDS
@ -406,4 +410,4 @@ _io_open_code(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
exit:
return return_value;
}
/*[clinic end generated code: output=2eaf6e914503bcfd input=a9049054013a1b77]*/
/*[clinic end generated code: output=7a8e032c0424bce2 input=a9049054013a1b77]*/

View file

@ -958,9 +958,11 @@ _io_BufferedReader___init__(PyObject *self, PyObject *args, PyObject *kwargs)
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(raw), &_Py_ID(buffer_size), },
};
#undef NUM_KEYWORDS
@ -1036,9 +1038,11 @@ _io_BufferedWriter___init__(PyObject *self, PyObject *args, PyObject *kwargs)
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(raw), &_Py_ID(buffer_size), },
};
#undef NUM_KEYWORDS
@ -1206,9 +1210,11 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs)
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(raw), &_Py_ID(buffer_size), },
};
#undef NUM_KEYWORDS
@ -1259,4 +1265,4 @@ skip_optional_pos:
exit:
return return_value;
}
/*[clinic end generated code: output=1a0562c66776fd53 input=a9049054013a1b77]*/
/*[clinic end generated code: output=3ee17211d2010462 input=a9049054013a1b77]*/

View file

@ -521,9 +521,11 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(initial_bytes), },
};
#undef NUM_KEYWORDS
@ -561,4 +563,4 @@ skip_optional_pos:
exit:
return return_value;
}
/*[clinic end generated code: output=477217b2bc464110 input=a9049054013a1b77]*/
/*[clinic end generated code: output=6dbfd82f4e9d4ef3 input=a9049054013a1b77]*/

View file

@ -66,9 +66,11 @@ _io_FileIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(file), &_Py_ID(mode), &_Py_ID(closefd), &_Py_ID(opener), },
};
#undef NUM_KEYWORDS
@ -541,4 +543,4 @@ _io_FileIO_isatty(PyObject *self, PyObject *Py_UNUSED(ignored))
#ifndef _IO_FILEIO_TRUNCATE_METHODDEF
#define _IO_FILEIO_TRUNCATE_METHODDEF
#endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
/*[clinic end generated code: output=f4e1f74c03d4ecdf input=a9049054013a1b77]*/
/*[clinic end generated code: output=1902fac9e39358aa input=a9049054013a1b77]*/

View file

@ -310,9 +310,11 @@ _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
};
#undef NUM_KEYWORDS
@ -550,4 +552,4 @@ _io_StringIO_newlines_get(PyObject *self, void *Py_UNUSED(context))
return return_value;
}
/*[clinic end generated code: output=69c9b100a359cbd5 input=a9049054013a1b77]*/
/*[clinic end generated code: output=5bfaaab7f41ee6b5 input=a9049054013a1b77]*/

View file

@ -318,9 +318,11 @@ _io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(decoder), &_Py_ID(translate), &_Py_ID(errors), },
};
#undef NUM_KEYWORDS
@ -388,9 +390,11 @@ _io_IncrementalNewlineDecoder_decode(PyObject *self, PyObject *const *args, Py_s
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(input), &_Py_ID(final), },
};
#undef NUM_KEYWORDS
@ -538,9 +542,11 @@ _io_TextIOWrapper___init__(PyObject *self, PyObject *args, PyObject *kwargs)
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(buffer), &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(newline), &_Py_ID(line_buffering), &_Py_ID(write_through), },
};
#undef NUM_KEYWORDS
@ -677,9 +683,11 @@ _io_TextIOWrapper_reconfigure(PyObject *self, PyObject *const *args, Py_ssize_t
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(newline), &_Py_ID(line_buffering), &_Py_ID(write_through), },
};
#undef NUM_KEYWORDS
@ -1304,4 +1312,4 @@ _io_TextIOWrapper__CHUNK_SIZE_set(PyObject *self, PyObject *value, void *Py_UNUS
return return_value;
}
/*[clinic end generated code: output=dad68d8c33d676e6 input=a9049054013a1b77]*/
/*[clinic end generated code: output=30404271a1151056 input=a9049054013a1b77]*/

View file

@ -65,9 +65,11 @@ _io__WindowsConsoleIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(file), &_Py_ID(mode), &_Py_ID(closefd), &_Py_ID(opener), },
};
#undef NUM_KEYWORDS
@ -461,4 +463,4 @@ _io__WindowsConsoleIO_isatty(PyObject *self, PyObject *Py_UNUSED(ignored))
#ifndef _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
#define _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
#endif /* !defined(_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF) */
/*[clinic end generated code: output=edc47f5c49589045 input=a9049054013a1b77]*/
/*[clinic end generated code: output=ce50bcd905f1f213 input=a9049054013a1b77]*/