hash: check if slot is empty with key index
authorPablo de Lara <pablo.de.lara.guarch@intel.com>
Fri, 26 Aug 2016 21:30:09 +0000 (22:30 +0100)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Thu, 29 Sep 2016 19:51:27 +0000 (21:51 +0200)
Instead of checking if the current and alternative signatures are 0,
it is faster to check if the key index associated to an entry
is 0, meaning that the slot is empty.

Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
Acked-by: Saikrishna Edupuganti <saikrishna.edupuganti@intel.com>
lib/librte_hash/rte_cuckoo_hash.c
lib/librte_hash/rte_cuckoo_hash.h
lib/librte_hash/rte_cuckoo_hash_x86.h

index 32f9f11..4de4422 100644 (file)
@@ -424,7 +424,7 @@ make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
                next_bucket_idx = bkt->signatures[i].alt & h->bucket_bitmask;
                next_bkt[i] = &h->buckets[next_bucket_idx];
                for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
-                       if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
+                       if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
                                break;
                }
 
@@ -610,7 +610,7 @@ __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
 #endif
                for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
                        /* Check if slot is available */
-                       if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
+                       if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
                                prim_bkt->signatures[i].current = sig;
                                prim_bkt->signatures[i].alt = alt_hash;
                                prim_bkt->key_idx[i] = new_idx;
@@ -708,7 +708,7 @@ __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
        /* Check if key is in primary location */
        for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
                if (bkt->signatures[i].current == sig &&
-                               bkt->signatures[i].sig != NULL_SIGNATURE) {
+                               bkt->key_idx[i] != EMPTY_SLOT) {
                        k = (struct rte_hash_key *) ((char *)keys +
                                        bkt->key_idx[i] * h->key_entry_size);
                        if (rte_hash_cmp_eq(key, k->key, h) == 0) {
@@ -824,7 +824,7 @@ __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
        /* Check if key is in primary location */
        for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
                if (bkt->signatures[i].current == sig &&
-                               bkt->signatures[i].sig != NULL_SIGNATURE) {
+                               bkt->key_idx[i] != EMPTY_SLOT) {
                        k = (struct rte_hash_key *) ((char *)keys +
                                        bkt->key_idx[i] * h->key_entry_size);
                        if (rte_hash_cmp_eq(key, k->key, h) == 0) {
@@ -835,7 +835,7 @@ __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
                                 * substracting the first dummy index
                                 */
                                ret = bkt->key_idx[i] - 1;
-                               bkt->key_idx[i] = 0;
+                               bkt->key_idx[i] = EMPTY_SLOT;
                                return ret;
                        }
                }
@@ -849,7 +849,7 @@ __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
        /* Check if key is in secondary location */
        for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
                if (bkt->signatures[i].current == alt_hash &&
-                               bkt->signatures[i].sig != NULL_SIGNATURE) {
+                               bkt->key_idx[i] != EMPTY_SLOT) {
                        k = (struct rte_hash_key *) ((char *)keys +
                                        bkt->key_idx[i] * h->key_entry_size);
                        if (rte_hash_cmp_eq(key, k->key, h) == 0) {
@@ -860,7 +860,7 @@ __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
                                 * substracting the first dummy index
                                 */
                                ret = bkt->key_idx[i] - 1;
-                               bkt->key_idx[i] = 0;
+                               bkt->key_idx[i] = EMPTY_SLOT;
                                return ret;
                        }
                }
@@ -1230,7 +1230,7 @@ rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32
        idx = *next % RTE_HASH_BUCKET_ENTRIES;
 
        /* If current position is empty, go to the next one */
-       while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
+       while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
                (*next)++;
                /* End of table */
                if (*next == total_entries)
index 6c76700..e290dab 100644 (file)
@@ -134,6 +134,8 @@ enum add_key_case {
 
 #define NULL_SIGNATURE                 0
 
+#define EMPTY_SLOT                     0
+
 #define KEY_ALIGNMENT                  16
 
 #define LCORE_CACHE_SIZE               64
index fa5630b..e16d69c 100644 (file)
@@ -53,8 +53,7 @@ rte_hash_cuckoo_insert_mw_tm(struct rte_hash_bucket *prim_bkt,
                        */
                        for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
                                /* Check if slot is available */
-                               if (likely(prim_bkt->signatures[i].sig ==
-                                               NULL_SIGNATURE)) {
+                               if (likely(prim_bkt->key_idx == EMPTY_SLOT)) {
                                        prim_bkt->signatures[i].current = sig;
                                        prim_bkt->signatures[i].alt = alt_hash;
                                        prim_bkt->key_idx[i] = new_idx;
@@ -171,7 +170,7 @@ rte_hash_cuckoo_make_space_mw_tm(const struct rte_hash *h,
                                        queue + RTE_HASH_BFS_QUEUE_MAX_LEN - 4)) {
                curr_bkt = tail->bkt;
                for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
-                       if (curr_bkt->signatures[i].sig == NULL_SIGNATURE) {
+                       if (curr_bkt->key_idx[i] == EMPTY_SLOT) {
                                if (likely(rte_hash_cuckoo_move_insert_mw_tm(h,
                                                tail, i, sig,
                                                alt_hash, new_idx) == 0))