gh-111545: Add Py_HashPointer() function (#112096)

* Implement _Py_HashPointerRaw() as a static inline function.
* Add Py_HashPointer() tests to test_capi.test_hash.
* Keep _Py_HashPointer() function as an alias to Py_HashPointer().
This commit is contained in:
Victor Stinner 2023-12-06 15:09:22 +01:00 committed by GitHub
parent f8852634ed
commit 828451dfde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 103 additions and 22 deletions

View file

@ -83,8 +83,6 @@ static Py_ssize_t hashstats[Py_HASH_STATS_MAX + 1] = {0};
*/
Py_hash_t _Py_HashPointer(const void *);
Py_hash_t
_Py_HashDouble(PyObject *inst, double v)
{
@ -132,23 +130,13 @@ _Py_HashDouble(PyObject *inst, double v)
}
Py_hash_t
_Py_HashPointerRaw(const void *p)
Py_HashPointer(const void *ptr)
{
size_t y = (size_t)p;
/* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
excessive hash collisions for dicts and sets */
y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
return (Py_hash_t)y;
}
Py_hash_t
_Py_HashPointer(const void *p)
{
Py_hash_t x = _Py_HashPointerRaw(p);
if (x == -1) {
x = -2;
Py_hash_t hash = _Py_HashPointerRaw(ptr);
if (hash == -1) {
hash = -2;
}
return x;
return hash;
}
Py_hash_t