mirror of
https://github.com/python/cpython.git
synced 2025-09-15 13:16:12 +00:00
bpo-40602: Optimize _Py_hashtable_get_ptr() (GH-20066)
_Py_hashtable_get_entry_ptr() avoids comparing the entry hash: compare directly keys. Move _Py_hashtable_get_entry_ptr() just after _Py_hashtable_get_entry_generic().
This commit is contained in:
parent
5b0a30354d
commit
42bae3a3d9
1 changed files with 23 additions and 24 deletions
|
@ -193,6 +193,29 @@ _Py_hashtable_get_entry_generic(_Py_hashtable_t *ht, const void *key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Specialized for:
|
||||||
|
// hash_func == _Py_hashtable_hash_ptr
|
||||||
|
// compare_func == _Py_hashtable_compare_direct
|
||||||
|
static _Py_hashtable_entry_t *
|
||||||
|
_Py_hashtable_get_entry_ptr(_Py_hashtable_t *ht, const void *key)
|
||||||
|
{
|
||||||
|
Py_uhash_t key_hash = _Py_hashtable_hash_ptr(key);
|
||||||
|
size_t index = key_hash & (ht->num_buckets - 1);
|
||||||
|
_Py_hashtable_entry_t *entry = entry = TABLE_HEAD(ht, index);
|
||||||
|
while (1) {
|
||||||
|
if (entry == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// Compare directly keys (ignore entry->key_hash)
|
||||||
|
if (entry->key == key) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
entry = ENTRY_NEXT(entry);
|
||||||
|
}
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void*
|
void*
|
||||||
_Py_hashtable_steal(_Py_hashtable_t *ht, const void *key)
|
_Py_hashtable_steal(_Py_hashtable_t *ht, const void *key)
|
||||||
{
|
{
|
||||||
|
@ -275,30 +298,6 @@ _Py_hashtable_get(_Py_hashtable_t *ht, const void *key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Specialized for:
|
|
||||||
// hash_func == _Py_hashtable_hash_ptr
|
|
||||||
// compare_func == _Py_hashtable_compare_direct
|
|
||||||
_Py_hashtable_entry_t *
|
|
||||||
_Py_hashtable_get_entry_ptr(_Py_hashtable_t *ht, const void *key)
|
|
||||||
{
|
|
||||||
Py_uhash_t key_hash = _Py_hashtable_hash_ptr(key);
|
|
||||||
size_t index = key_hash & (ht->num_buckets - 1);
|
|
||||||
_Py_hashtable_entry_t *entry = entry = TABLE_HEAD(ht, index);
|
|
||||||
while (1) {
|
|
||||||
if (entry == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (entry->key_hash == key_hash) {
|
|
||||||
if (entry->key == key) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entry = ENTRY_NEXT(entry);
|
|
||||||
}
|
|
||||||
return entry;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
_Py_hashtable_foreach(_Py_hashtable_t *ht,
|
_Py_hashtable_foreach(_Py_hashtable_t *ht,
|
||||||
_Py_hashtable_foreach_func func,
|
_Py_hashtable_foreach_func func,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue