bpo-40602: Add _Py_HashPointerRaw() function (GH-20056)

Add a new _Py_HashPointerRaw() function which avoids replacing -1
with -2 to micro-optimize hash table using pointer keys: using
_Py_hashtable_hash_ptr() hash function.
This commit is contained in:
Victor Stinner 2020-05-12 18:46:20 +02:00 committed by GitHub
parent 4c9ea093cd
commit f453221c8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View file

@ -129,16 +129,22 @@ _Py_HashDouble(double v)
}
Py_hash_t
_Py_HashPointer(const void *p)
_Py_HashPointerRaw(const void *p)
{
Py_hash_t x;
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));
x = (Py_hash_t)y;
if (x == -1)
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;
}
return x;
}