some performance improvements

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24504)
This commit is contained in:
Neil Horman 2024-05-15 15:34:15 -04:00 committed by Tomas Mraz
parent 435531ec24
commit 14efc05314

View File

@ -76,8 +76,10 @@
*/
#if defined(__GNUC__) || defined(__CLANG__)
#define PREFETCH_NEIGHBORHOOD(x) __builtin_prefetch(x.entries)
#define PREFETCH(x) __builtin_prefetch(x)
#else
#define PREFETCH_NEIGHBORHOOD(x)
#define PREFETCH(x)
#endif
static ossl_unused uint64_t fnv1a_hash(uint8_t *key, size_t len)
@ -520,7 +522,9 @@ static ossl_inline int match_key(HT_KEY *a, HT_KEY *b)
* keys match if they are both present, the same size
* and compare equal in memory
*/
if (a != NULL && b != NULL && a->keysize == b->keysize)
PREFETCH(a->keybuf);
PREFETCH(b->keybuf);
if (a->keybuf != NULL && b->keybuf != NULL && a->keysize == b->keysize)
return !memcmp(a->keybuf, b->keybuf, a->keysize);
return 1;
@ -548,13 +552,12 @@ static int ossl_ht_insert_locked(HT *h, uint64_t hash,
if (ival == NULL)
empty_idx = j;
if (compare_hash(hash, ihash)) {
if (compare_hash(hash, ihash) && match_key(&newval->value.key,
&ival->key)) {
/* Its the same hash, lets make sure its not a collision */
if (match_key(&newval->value.key, &ival->key)) {
if (olddata == NULL) {
/* invalid */
return 0;
}
if (olddata == NULL) {
/* invalid */
return 0;
}
/* Do a replacement */
if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[j].hash,
@ -688,7 +691,6 @@ static void free_old_entry(void *arg)
int ossl_ht_delete(HT *h, HT_KEY *key)
{
struct ht_mutable_data_st *md = ossl_rcu_deref(&h->md);
uint64_t hash;
uint64_t neigh_idx;
size_t j;
@ -702,9 +704,8 @@ int ossl_ht_delete(HT *h, HT_KEY *key)
PREFETCH_NEIGHBORHOOD(h->md->neighborhoods[neigh_idx]);
for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
v = (struct ht_internal_value_st *)h->md->neighborhoods[neigh_idx].entries[j].value;
if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash)) {
if (!match_key(key, &v->value.key))
continue;
if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash)
&& match_key(key, &v->value.key)) {
if (!CRYPTO_atomic_store(&h->md->neighborhoods[neigh_idx].entries[j].hash,
0, h->atomic_lock))
break;