Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-40602: Add _Py_HashPointerRaw() function
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.
  • Loading branch information
vstinner committed May 12, 2020
commit 299db3233096fbbfa3aa4dea5b3c5845007164d8
2 changes: 2 additions & 0 deletions Include/pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extern "C" {
#ifndef Py_LIMITED_API
PyAPI_FUNC(Py_hash_t) _Py_HashDouble(double);
PyAPI_FUNC(Py_hash_t) _Py_HashPointer(const void*);
// Similar to _Py_HashPointer(), but don't replace -1 with -2
PyAPI_FUNC(Py_hash_t) _Py_HashPointerRaw(const void*);
PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);
#endif

Expand Down
2 changes: 1 addition & 1 deletion Python/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ _Py_hashtable_hash_ptr(struct _Py_hashtable_t *ht, const void *pkey)
{
void *key;
_Py_HASHTABLE_READ_KEY(ht, pkey, key);
return (Py_uhash_t)_Py_HashPointer(key);
return (Py_uhash_t)_Py_HashPointerRaw(key);
}


Expand Down
14 changes: 10 additions & 4 deletions Python/pyhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down