]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
some performance improvements
authorNeil Horman <nhorman@openssl.org>
Wed, 15 May 2024 19:34:15 +0000 (15:34 -0400)
committerTomas Mraz <tomas@openssl.org>
Wed, 21 Aug 2024 13:21:25 +0000 (15:21 +0200)
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24504)

crypto/hashtable/hashtable.c

index 77c546dbb0062e65eacc3b7804ca7d57f47ccbf2..de5fdbeeca194110e821777444a17397b534ebcb 100644 (file)
  */
 #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;