Skip to contentSkip to conversation

hash: optimize compare logic

RFC Superseded Stephen Hemminger 3 participants 26 messages Aug 21, 2025 – Aug 26, 2025

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover hash: optimize compare logic

Recent discussion around handling small keys motivated furthur examination of the compare logic.

Stephen Hemminger (3):
hash: move table of hash compare functions out of header
hash: reduce architecture special cases
hash: add support for common small key sizes

 lib/hash/rte_cmp_arm64.h   |  52 ----------
 lib/hash/rte_cmp_x86.h     |  56 +----------
 lib/hash/rte_cuckoo_hash.c | 194 +++++++++++++++++++++++++++++++++++--
 lib/hash/rte_cuckoo_hash.h |  79 +--------------
 4 files changed, 187 insertions(+), 194 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

RFC 1/3 hash: move table of hash compare functions out of header

Remove the definition of the compare jump table from the header file so the internal details are not exposed. Prevents future ABI breakage if new sizes are added.

Make other macros local if possible, header should only contain exposed API.

---
 lib/hash/rte_cuckoo_hash.c | 74 ++++++++++++++++++++++++++++++-----
 lib/hash/rte_cuckoo_hash.h | 79 +-------------------------------------
 2 files changed, 65 insertions(+), 88 deletions(-)
lib/hash/rte_cuckoo_hash.c +64 -10
index 2c92c51624..619fe0c691 100644
@@ -25,14 +25,51 @@
 #include <rte_tailq.h>
 
 #include "rte_hash.h"
+#include "rte_cuckoo_hash.h"
 
-/* needs to be before rte_cuckoo_hash.h */
 RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
 #define RTE_LOGTYPE_HASH hash_logtype
 #define HASH_LOG(level, ...) \
 	RTE_LOG_LINE(level, HASH, "" __VA_ARGS__)
 
-#include "rte_cuckoo_hash.h"
+/* Macro to enable/disable run-time checking of function parameters */
+#if defined(RTE_LIBRTE_HASH_DEBUG)
+#define RETURN_IF_TRUE(cond, retval) do { \
+	if (cond) \
+		return retval; \
+} while (0)
+#else
+#define RETURN_IF_TRUE(cond, retval)
+#endif
+
+#if defined(RTE_ARCH_X86)
+#include "rte_cmp_x86.h"
+#endif
+
+#if defined(RTE_ARCH_ARM64)
+#include "rte_cmp_arm64.h"
+#endif
+
+/*
+ * All different options to select a key compare function,
+ * based on the key size and custom function.
+ * Not in rte_cuckoo_hash.h to avoid ABI issues.
+ */
+enum cmp_jump_table_case {
+	KEY_CUSTOM = 0,
+#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
+	KEY_16_BYTES,
+	KEY_32_BYTES,
+	KEY_48_BYTES,
+	KEY_64_BYTES,
+	KEY_80_BYTES,
+	KEY_96_BYTES,
+	KEY_112_BYTES,
+	KEY_128_BYTES,
+#endif
+	KEY_OTHER_BYTES,
+	NUM_KEY_CMP_CASES,
+};
 
 /* Enum used to select the implementation of the signature comparison function to use
  * eg: a system supporting SVE might want to use a NEON or scalar implementation.
@@ -117,6 +154,25 @@ void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
 	h->rte_hash_custom_cmp_eq = func;
 }
 
+/*
+ * Table storing all different key compare functions
+ * (multi-process supported)
+ */
+static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
+	[KEY_CUSTOM] = NULL,
+#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
+	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,
+	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
+	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
+	[KEY_64_BYTES] = rte_hash_k64_cmp_eq,
+	[KEY_80_BYTES] = rte_hash_k80_cmp_eq,
+	[KEY_96_BYTES] = rte_hash_k96_cmp_eq,
+	[KEY_112_BYTES] = rte_hash_k112_cmp_eq,
+	[KEY_128_BYTES] = rte_hash_k128_cmp_eq,
+#endif
+	[KEY_OTHER_BYTES] = memcmp,
+};
+
 static inline int
 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
 {
@@ -390,13 +446,13 @@ rte_hash_create(const struct rte_hash_parameters *params)
 		goto err_unlock;
 	}
 
-/*
- * If x86 architecture is used, select appropriate compare function,
- * which may use x86 intrinsics, otherwise use memcmp
- */
-#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
 	/* Select function to compare keys */
 	switch (params->key_len) {
+#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
+	/*
+	 * If x86 architecture is used, select appropriate compare function,
+	 * which may use x86 intrinsics, otherwise use memcmp
+	 */
 	case 16:
 		h->cmp_jump_table_idx = KEY_16_BYTES;
 		break;
@@ -421,13 +477,11 @@ rte_hash_create(const struct rte_hash_parameters *params)
 	case 128:
 		h->cmp_jump_table_idx = KEY_128_BYTES;
 		break;
+#endif
 	default:
 		/* If key is not multiple of 16, use generic memcmp */
 		h->cmp_jump_table_idx = KEY_OTHER_BYTES;
 	}
-#else
-	h->cmp_jump_table_idx = KEY_OTHER_BYTES;
-#endif
 
 	if (use_local_cache) {
 		local_free_slots = rte_zmalloc_socket(NULL,
lib/hash/rte_cuckoo_hash.h +1 -78

94 lines not on this page · raw message

Signature · 1 line
2.47.2
#4 lore raw

RFC 2/3 hash: reduce architecture special cases

In reply to Stephen Hemminger

Make comparison of power of 2 sizes compatible across platforms. Keep the special case code for 16 bytes for x86 and arm64 but also add simple xor for others.

Need to make rte_hash_k32_cmp_eq() exposed because ip_frag code poaches it.

---
 lib/hash/rte_cmp_arm64.h   | 52 ----------------------
 lib/hash/rte_cmp_x86.h     | 56 +-----------------------
 lib/hash/rte_cuckoo_hash.c | 90 +++++++++++++++++++++++++++++++++-----
 3 files changed, 80 insertions(+), 118 deletions(-)
lib/hash/rte_cmp_arm64.h +0 -52
index a3e85635eb..c7c56526d0 100644
@@ -31,55 +31,3 @@ rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
 		rte_hash_k16_cmp_eq((const char *) key1 + 16,
 				(const char *) key2 + 16, key_len);
 }
-
-static inline int
-rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 16,
-				(const char *) key2 + 16, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k32_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k80_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k96_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k112_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 96,
-				(const char *) key2 + 96, key_len);
-}
-
-static inline int
-rte_hash_k128_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k64_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
lib/hash/rte_cmp_x86.h +2 -54

60 lines not on this page · raw message

lib/hash/rte_cuckoo_hash.c +78 -12

124 lines not on this page · raw message

Signature · 1 line
2.47.2
#6 lore raw

RFC 3/3 hash: add support for common small key sizes

In reply to Stephen Hemminger

Add new compare functions for common small key sizes.

Bugzilla ID: 1775

---
 lib/hash/rte_cuckoo_hash.c | 54 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
lib/hash/rte_cuckoo_hash.c +54 -0
index 3212695d92..825889c320 100644
@@ -49,6 +49,11 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
  */
 enum cmp_jump_table_case {
 	KEY_CUSTOM = 0,
+	KEY_2_BYTES,
+	KEY_4_BYTES,
+	KEY_6_BYTES,
+	KEY_8_BYTES,
+	KEY_12_BYTES,
 	KEY_16_BYTES,
 	KEY_32_BYTES,
 	KEY_48_BYTES,
@@ -86,6 +91,50 @@ rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
 }
 #endif
 
+static inline int
+rte_hash_k2_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+
+	return k1[0] ^ k2[0];
+}
+
+static inline int
+rte_hash_k4_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint32_t *k1 = key1;
+	const uint32_t *k2 = key2;
+
+	return k1[0] ^ k2[0];
+}
+
+static inline int
+rte_hash_k6_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) | (k1[2] ^ k2[2]);
+}
+
+static inline int
+rte_hash_k8_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint64_t *k1 = key1;
+	const uint64_t *k2 = key2;
+
+	return k1[0] ^ k2[0];
+}
+
+static inline int
+rte_hash_k12_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint32_t *k1 = key1;
+	const uint32_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) | (k1[2] ^ k2[2]);
+}
 
 static inline int
 rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
@@ -228,6 +277,11 @@ void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
  */
 static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
 	[KEY_CUSTOM] = NULL,
+	[KEY_2_BYTES] = rte_hash_k2_cmp_eq,
+	[KEY_4_BYTES] = rte_hash_k4_cmp_eq,
+	[KEY_6_BYTES] = rte_hash_k6_cmp_eq,
+	[KEY_8_BYTES] = rte_hash_k8_cmp_eq,
+	[KEY_12_BYTES] = rte_hash_k12_cmp_eq,
 	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,
 	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
 	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
Signature · 1 line
2.47.2
#7 lore raw

Re: [RFC 3/3] hash: add support for common small key sizes

Quoted 28 lines · Stephen Hemminger

Add new compare functions for common small key sizes.

Bugzilla ID: 1775

---
  lib/hash/rte_cuckoo_hash.c | 54 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 54 insertions(+)
lib/hash/rte_cuckoo_hash.c +5 -0
index 3212695d92..825889c320 100644
@@ -49,6 +49,11 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
   */
  enum cmp_jump_table_case {
  	KEY_CUSTOM = 0,
+	KEY_2_BYTES,
+	KEY_4_BYTES,
+	KEY_6_BYTES,
+	KEY_8_BYTES,
+	KEY_12_BYTES,
  	KEY_16_BYTES,
  	KEY_32_BYTES,
  	KEY_48_BYTES,

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 99

Quoted 9 lines
lib/hash/rte_cuckoo_hash.c +6 -0
@@ -86,6 +91,50 @@ rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
  }
  #endif
 
+static inline int
+rte_hash_k2_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+

What we do now is to require the keys are 16-bit aligned (which wasn't the case before).

You could

uint16_t k1;
memcpy(&k1, key1, sizeof(uint16_t));
instead.

Would generate the same code, but be safe from any future alignment issues.

Anyway, maybe it's safe to assume the keys are aligned, so this is not an issue.

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 101

Quoted 2 lines
lib/hash/rte_cuckoo_hash.c +2 -0
+	return k1[0] ^ k2[0];
+}

Haven't you implemented "neq" rather than "eq" here? If the keys are equal, the result is 0. Should be != 0.

Would it be worth adding a comment like "use XOR to make this branch-free"? It may not be obvious to all readers.

That said, I’m not sure this trick will actually change the generated object code - especially if the result of the eq function is still used in a conditional afterward. Anyway, keeping it seems like a good conservative approach.

Quoted 51 lines
lib/hash/rte_cuckoo_hash.c +41 -0
+
+static inline int
+rte_hash_k4_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint32_t *k1 = key1;
+	const uint32_t *k2 = key2;
+
+	return k1[0] ^ k2[0];
+}
+
+static inline int
+rte_hash_k6_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) | (k1[2] ^ k2[2]);
+}
+
+static inline int
+rte_hash_k8_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint64_t *k1 = key1;
+	const uint64_t *k2 = key2;
+
+	return k1[0] ^ k2[0];
+}
+
+static inline int
+rte_hash_k12_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint32_t *k1 = key1;
+	const uint32_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) | (k1[2] ^ k2[2]);
+}
 
  static inline int
  rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
@@ -228,6 +277,11 @@ void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
   */
  static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
  	[KEY_CUSTOM] = NULL,
+	[KEY_2_BYTES] = rte_hash_k2_cmp_eq,
+	[KEY_4_BYTES] = rte_hash_k4_cmp_eq,
+	[KEY_6_BYTES] = rte_hash_k6_cmp_eq,
+	[KEY_8_BYTES] = rte_hash_k8_cmp_eq,
+	[KEY_12_BYTES] = rte_hash_k12_cmp_eq,
  	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,
  	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
  	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
Acked-by on v1 1/3
#2 lore raw

RE: [RFC 1/3] hash: move table of hash compare functions out of header

In reply to Stephen Hemminger
Quoted 12 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 21 August 2025 22.35

Remove the definition of the compare jump table from the header file so the internal details are not exposed. Prevents future ABI breakage if new sizes are added.

Make other macros local if possible, header should only contain exposed API.

---

[...]

Quoted 38 lines
lib/hash/rte_cuckoo_hash.c +20 -0
+/*
+ * All different options to select a key compare function,
+ * based on the key size and custom function.
+ * Not in rte_cuckoo_hash.h to avoid ABI issues.
+ */
+enum cmp_jump_table_case {
+	KEY_CUSTOM = 0,
+#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
+	KEY_16_BYTES,
+	KEY_32_BYTES,
+	KEY_48_BYTES,
+	KEY_64_BYTES,
+	KEY_80_BYTES,
+	KEY_96_BYTES,
+	KEY_112_BYTES,
+	KEY_128_BYTES,
+#endif
+	KEY_OTHER_BYTES,
+	NUM_KEY_CMP_CASES,
+};
 
 /* Enum used to select the implementation of the signature comparison

function to use
* eg: a system supporting SVE might want to use a NEON or scalar
implementation.

lib/hash/rte_cuckoo_hash.c +8 -0
@@ -117,6 +154,25 @@ void rte_hash_set_cmp_func(struct rte_hash *h,
 rte_hash_cmp_eq_t func)
 	h->rte_hash_custom_cmp_eq = func;
 }
 
+/*
+ * Table storing all different key compare functions
+ * (multi-process supported)
+ */
+static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
+	[KEY_CUSTOM] = NULL,
+#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
+	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,

lib/hash/rte_cuckoo_hash.c void rte_hash_set_cmp_func(struct rte_hash *h, · line 175

Quoted 10 lines
lib/hash/rte_cuckoo_hash.c +10 -0
+	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
+	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
+	[KEY_64_BYTES] = rte_hash_k64_cmp_eq,
+	[KEY_80_BYTES] = rte_hash_k80_cmp_eq,
+	[KEY_96_BYTES] = rte_hash_k96_cmp_eq,
+	[KEY_112_BYTES] = rte_hash_k112_cmp_eq,
+	[KEY_128_BYTES] = rte_hash_k128_cmp_eq,
+#endif
+	[KEY_OTHER_BYTES] = memcmp,
+};

Nice trick explicitly indexing these here; it reduces the risk of not matching the cmp_jump_table_case.

Consider adding static_assert() that RTE_DIM(cmp_jump_table) == NUM_KEY_CMP_CASES.

With or without suggested static_assert()...

Good cleanup!

Acked-by on v1 2/3
#5 lore raw

RE: [RFC 2/3] hash: reduce architecture special cases

In reply to Stephen Hemminger
Quoted 9 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 21 August 2025 22.35

Make comparison of power of 2 sizes compatible across platforms. Keep the special case code for 16 bytes for x86 and arm64 but also add simple xor for others.

Need to make rte_hash_k32_cmp_eq() exposed because ip_frag code poaches it.

One more argument why these architecture optimized compare functions should be exposed as generic EAL functions, like rte_mov16() etc. for rte_memcpy(), instead of hiding them away in the hash library.

It could be rte_memeq16(const void *, const void *) etc. in /lib/eal/include, with architecture optimized variants, and a generic variant.

And a wrapper calling them in the hash library, such as:

static inline int
rte_hash_k16_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)

{
	return !rte_memeq16(key1, key2);
}
Quoted 3 lines

---

With our without suggested feature creep...

#8 lore raw

RE: [RFC 3/3] hash: add support for common small key sizes

In reply to Mattias Rönnblom
Quoted 15 lines

From: Mattias Rönnblom [mailto:[email protected]]
Sent: Friday, 22 August 2025 09.20

Quoted 11 lines · Stephen Hemminger

Add new compare functions for common small key sizes.

Bugzilla ID: 1775

---
  lib/hash/rte_cuckoo_hash.c | 54 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 54 insertions(+)
Quoted 13 lines

lib/hash/rte_cuckoo_hash.c RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO); · line 56

Quoted 13 lines
lib/hash/rte_cuckoo_hash.c +5 -0
index 3212695d92..825889c320 100644
@@ -49,6 +49,11 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
   */
  enum cmp_jump_table_case {
  	KEY_CUSTOM = 0,
+	KEY_2_BYTES,
+	KEY_4_BYTES,
+	KEY_6_BYTES,
+	KEY_8_BYTES,
+	KEY_12_BYTES,

While you are at it, consider adding handlers for 3, 5, 10 and 14 bytes too.

Quoted 19 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2,

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +0 -0
  	KEY_16_BYTES,
  	KEY_32_BYTES,
  	KEY_48_BYTES,
@@ -86,6 +91,50 @@ rte_hash_k32_cmp_eq(const void *key1, const void *key2,

size_t key_len)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 5 lines
lib/hash/rte_cuckoo_hash.c +2 -0
  }
  #endif
 
+static inline int
+rte_hash_k2_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 99

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +4 -0
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+

What we do now is to require the keys are 16-bit aligned (which wasn't the case before).

Quoted 7 lines

You could

uint16_t k1;
memcpy(&k1, key1, sizeof(uint16_t));
instead.

Would generate the same code, but be safe from any future alignment issues.

Or use the unaligned types, e.g.:

	const unaligned_uint16_t *k1 = key1;
	const unaligned_uint16_t *k2 = key2;
Quoted 3 lines

Anyway, maybe it's safe to assume the keys are aligned, so this is not an issue.

Lots of DPDK code ignores alignment issues.

Quoted 6 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, · line 101

Quoted 3 lines
lib/hash/rte_cuckoo_hash.c +2 -0
+	return k1[0] ^ k2[0];
+}

Haven't you implemented "neq" rather than "eq" here? If the keys are equal, the result is 0. Should be != 0.

Not a bug.
These hash compare functions are in fact "neq", not "eq".
Having "_cmp_eq" in the function names is extremely unfortunate and misleading.

Quoted 8 lines

Would it be worth adding a comment like "use XOR to make this branch-free"? It may not be obvious to all readers.

That said, I’m not sure this trick will actually change the generated object code - especially if the result of the eq function is still used in a conditional afterward. Anyway, keeping it seems like a good conservative approach.

I wonder if any compiler is clever enough to use a different memcmp implementation if we inform the compiler at build time that we don't care if key1 is less than or greater key2, only if they differ or not. If so, the OTHER_BYTES handler shouldn't call memcmp() directly, but a wrapper around it:

rte_hash_k_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)

{
	return memcmp(key1, key2, key_len) != 0;
}
Quoted 25 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +3 -0
+
+static inline int
+rte_hash_k4_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 9 lines
lib/hash/rte_cuckoo_hash.c +9 -0
+{
+	const uint32_t *k1 = key1;
+	const uint32_t *k2 = key2;
+
+	return k1[0] ^ k2[0];
+}
+
+static inline int
+rte_hash_k6_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 9 lines
lib/hash/rte_cuckoo_hash.c +9 -0
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) | (k1[2] ^ k2[2]);
+}
+
+static inline int
+rte_hash_k8_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

Quoted 5 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 127

Quoted 5 lines
lib/hash/rte_cuckoo_hash.c +5 -0
+{
+	const uint64_t *k1 = key1;
+	const uint64_t *k2 = key2;
+
+	return k1[0] ^ k2[0];

I think this has a truncation bug when converting from uint64_t to int return type.
Also consider if 32 bit architecture need a different implementation. (Don't know, just raising a potential flag.)

Quoted 27 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +4 -0
+}
+
+static inline int
+rte_hash_k12_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c void rte_hash_set_cmp_func(struct rte_hash *h,

Quoted 10 lines
lib/hash/rte_cuckoo_hash.c +6 -0
+{
+	const uint32_t *k1 = key1;
+	const uint32_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) | (k1[2] ^ k2[2]);
+}
 
  static inline int
  rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
@@ -228,6 +277,11 @@ void rte_hash_set_cmp_func(struct rte_hash *h,

rte_hash_cmp_eq_t func)

Quoted 11 lines
lib/hash/rte_cuckoo_hash.c +5 -0
   */
  static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
  	[KEY_CUSTOM] = NULL,
+	[KEY_2_BYTES] = rte_hash_k2_cmp_eq,
+	[KEY_4_BYTES] = rte_hash_k4_cmp_eq,
+	[KEY_6_BYTES] = rte_hash_k6_cmp_eq,
+	[KEY_8_BYTES] = rte_hash_k8_cmp_eq,
+	[KEY_12_BYTES] = rte_hash_k12_cmp_eq,
  	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,
  	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
  	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
#9 lore raw

Re: [RFC 3/3] hash: add support for common small key sizes

Quoted 56 lines · Morten Brørup
Quoted 15 lines

From: Mattias Rönnblom [mailto:[email protected]]
Sent: Friday, 22 August 2025 09.20

Quoted 11 lines · Stephen Hemminger

Add new compare functions for common small key sizes.

Bugzilla ID: 1775

---
   lib/hash/rte_cuckoo_hash.c | 54 ++++++++++++++++++++++++++++++++++++++
   1 file changed, 54 insertions(+)
Quoted 13 lines

lib/hash/rte_cuckoo_hash.c RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO); · line 56

Quoted 13 lines
lib/hash/rte_cuckoo_hash.c +5 -0
index 3212695d92..825889c320 100644
@@ -49,6 +49,11 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
    */
   enum cmp_jump_table_case {
   	KEY_CUSTOM = 0,
+	KEY_2_BYTES,
+	KEY_4_BYTES,
+	KEY_6_BYTES,
+	KEY_8_BYTES,
+	KEY_12_BYTES,

While you are at it, consider adding handlers for 3, 5, 10 and 14 bytes too.

Quoted 25 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2,

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +0 -0
   	KEY_16_BYTES,
   	KEY_32_BYTES,
   	KEY_48_BYTES,
@@ -86,6 +91,50 @@ rte_hash_k32_cmp_eq(const void *key1, const void *key2,

size_t key_len)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 5 lines
lib/hash/rte_cuckoo_hash.c +2 -0
   }
   #endif
 
+static inline int
+rte_hash_k2_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 99

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +4 -0
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+

What we do now is to require the keys are 16-bit aligned (which wasn't the case before).

You could

uint16_t k1;
memcpy(&k1, key1, sizeof(uint16_t));
instead.

Quoted 6 lines
Quoted 1 line

Would generate the same code, but be safe from any future alignment issues.

Or use the unaligned types, e.g.:

	const unaligned_uint16_t *k1 = key1;
	const unaligned_uint16_t *k2 = key2;

Could you explain why that is safe? Doesn't
__attribute__((__aligned__(1)))
just say specify the object doesn't have any alignment requirements, without asking the compiler to deal with it?

Quoted 7 lines
Quoted 3 lines

Anyway, maybe it's safe to assume the keys are aligned, so this is not an issue.

Lots of DPDK code ignores alignment issues.

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, · line 101

Quoted 2 lines
lib/hash/rte_cuckoo_hash.c +2 -0
+	return k1[0] ^ k2[0];
+}

Haven't you implemented "neq" rather than "eq" here? If the keys are equal, the result is 0. Should be != 0.

Not a bug.

Well, the function body doesn't do what the function name tells it. :)

Quoted 7 lines

These hash compare functions are in fact "neq", not "eq".
Having "_cmp_eq" in the function names is extremely unfortunate and misleading.

Quoted 4 lines

Would it be worth adding a comment like "use XOR to make this branch-free"? It may not be obvious to all readers.

Quoted 6 lines
Quoted 4 lines

That said, I’m not sure this trick will actually change the generated object code - especially if the result of the eq function is still used in a conditional afterward. Anyway, keeping it seems like a good conservative approach.

I wonder if any compiler is clever enough to use a different memcmp implementation if we inform the compiler at build time that we don't care if key1 is less than or greater key2, only if they differ or not.

All what is needed is a constant-size length. (Only tested with the most recent GCC and clang.)

At least GCC will emit a cmp instruction though (so not branch free), if that matters.

Quoted 7 lines

If so, the OTHER_BYTES handler shouldn't call memcmp() directly, but a wrapper around it:

rte_hash_k_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)

{
	return memcmp(key1, key2, key_len) != 0;
}

Should work. (Remove __rte_unused.)

Quoted 62 lines
Quoted 25 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +3 -0
+
+static inline int
+rte_hash_k4_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 9 lines
lib/hash/rte_cuckoo_hash.c +9 -0
+{
+	const uint32_t *k1 = key1;
+	const uint32_t *k2 = key2;
+
+	return k1[0] ^ k2[0];
+}
+
+static inline int
+rte_hash_k6_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 9 lines
lib/hash/rte_cuckoo_hash.c +9 -0
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) | (k1[2] ^ k2[2]);
+}
+
+static inline int
+rte_hash_k8_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

Quoted 5 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 127

Quoted 5 lines
lib/hash/rte_cuckoo_hash.c +5 -0
+{
+	const uint64_t *k1 = key1;
+	const uint64_t *k2 = key2;
+
+	return k1[0] ^ k2[0];

I think this has a truncation bug when converting from uint64_t to int return type.
Also consider if 32 bit architecture need a different implementation. (Don't know, just raising a potential flag.)

Quoted 27 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +4 -0
+}
+
+static inline int
+rte_hash_k12_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c void rte_hash_set_cmp_func(struct rte_hash *h,

Quoted 10 lines
lib/hash/rte_cuckoo_hash.c +6 -0
+{
+	const uint32_t *k1 = key1;
+	const uint32_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) | (k1[2] ^ k2[2]);
+}
 
   static inline int
   rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
@@ -228,6 +277,11 @@ void rte_hash_set_cmp_func(struct rte_hash *h,

rte_hash_cmp_eq_t func)

Quoted 11 lines
lib/hash/rte_cuckoo_hash.c +5 -0
    */
   static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
   	[KEY_CUSTOM] = NULL,
+	[KEY_2_BYTES] = rte_hash_k2_cmp_eq,
+	[KEY_4_BYTES] = rte_hash_k4_cmp_eq,
+	[KEY_6_BYTES] = rte_hash_k6_cmp_eq,
+	[KEY_8_BYTES] = rte_hash_k8_cmp_eq,
+	[KEY_12_BYTES] = rte_hash_k12_cmp_eq,
   	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,
   	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
   	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
#12 lore raw

Re: [RFC 3/3] hash: add support for common small key sizes

In reply to Mattias Rönnblom
Quoted 44 lines · On Fri, 22 Aug 2025 09:19:45 +0200 Mattias Rönnblom <[email protected]>…
Quoted 28 lines · Stephen Hemminger

Add new compare functions for common small key sizes.

Bugzilla ID: 1775

---
  lib/hash/rte_cuckoo_hash.c | 54 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 54 insertions(+)
lib/hash/rte_cuckoo_hash.c +5 -0
index 3212695d92..825889c320 100644
@@ -49,6 +49,11 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
   */
  enum cmp_jump_table_case {
  	KEY_CUSTOM = 0,
+	KEY_2_BYTES,
+	KEY_4_BYTES,
+	KEY_6_BYTES,
+	KEY_8_BYTES,
+	KEY_12_BYTES,
  	KEY_16_BYTES,
  	KEY_32_BYTES,
  	KEY_48_BYTES,

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 99

Quoted 9 lines
lib/hash/rte_cuckoo_hash.c +6 -0
@@ -86,6 +91,50 @@ rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
  }
  #endif
 
+static inline int
+rte_hash_k2_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+  

What we do now is to require the keys are 16-bit aligned (which wasn't the case before).

You could

Quoted 8 lines

uint16_t k1;
memcpy(&k1, key1, sizeof(uint16_t));
instead.

Would generate the same code, but be safe from any future alignment issues.

Anyway, maybe it's safe to assume the keys are aligned, so this is not an issue.

The keys are always in rte_hash_keys which has the key field aligned at uintptr_t.

Quoted 6 lines

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 101

Quoted 3 lines
lib/hash/rte_cuckoo_hash.c +2 -0
+	return k1[0] ^ k2[0];
+}  

Haven't you implemented "neq" rather than "eq" here? If the keys are equal, the result is 0. Should be != 0.

The functions use same return value as memcmp() which returns 0 on match.

Quoted 8 lines

Would it be worth adding a comment like "use XOR to make this branch-free"? It may not be obvious to all readers.

That said, I’m not sure this trick will actually change the generated object code - especially if the result of the eq function is still used in a conditional afterward. Anyway, keeping it seems like a good conservative approach.

Compiler is not smart enough to get past the array of functions to optimize across that.

#13 lore raw

v2 cover Cuckoo hash cleanup and optimizations

In reply to Stephen Hemminger

Recent discussion around handling small keys motivated furthur examination of the compare logic.

Stephen Hemminger (4):
hash: move table of hash compare functions out of header
hash: use static_assert
hash: reduce architecture special cases
hash: add support for common small key sizes

 app/test/test_hash.c       |   4 +-
 lib/hash/rte_cmp_arm64.h   |  56 +--------
 lib/hash/rte_cmp_generic.h |  35 ++++++
 lib/hash/rte_cmp_x86.h     |  60 +---------
 lib/hash/rte_cuckoo_hash.c | 238 +++++++++++++++++++++++++++++++++++--
 lib/hash/rte_cuckoo_hash.h |  84 +------------
 6 files changed, 275 insertions(+), 202 deletions(-)
 create mode 100644 lib/hash/rte_cmp_generic.h
Signature · 1 line
2.47.2
#14 lore raw

PATCH v2 1/4 hash: move table of hash compare functions out of header

Remove the definition of the compare jump table from the header file so the internal details are not exposed. Prevents future ABI breakage if new sizes are added.

Make other macros local if possible, header should only contain exposed API.

---
 lib/hash/rte_cuckoo_hash.c | 74 ++++++++++++++++++++++++++++++-----
 lib/hash/rte_cuckoo_hash.h | 79 +-------------------------------------
 2 files changed, 65 insertions(+), 88 deletions(-)
lib/hash/rte_cuckoo_hash.c +64 -10
index 2c92c51624..619fe0c691 100644
@@ -25,14 +25,51 @@
 #include <rte_tailq.h>
 
 #include "rte_hash.h"
+#include "rte_cuckoo_hash.h"
 
-/* needs to be before rte_cuckoo_hash.h */
 RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
 #define RTE_LOGTYPE_HASH hash_logtype
 #define HASH_LOG(level, ...) \
 	RTE_LOG_LINE(level, HASH, "" __VA_ARGS__)
 
-#include "rte_cuckoo_hash.h"
+/* Macro to enable/disable run-time checking of function parameters */
+#if defined(RTE_LIBRTE_HASH_DEBUG)
+#define RETURN_IF_TRUE(cond, retval) do { \
+	if (cond) \
+		return retval; \
+} while (0)
+#else
+#define RETURN_IF_TRUE(cond, retval)
+#endif
+
+#if defined(RTE_ARCH_X86)
+#include "rte_cmp_x86.h"
+#endif
+
+#if defined(RTE_ARCH_ARM64)
+#include "rte_cmp_arm64.h"
+#endif
+
+/*
+ * All different options to select a key compare function,
+ * based on the key size and custom function.
+ * Not in rte_cuckoo_hash.h to avoid ABI issues.
+ */
+enum cmp_jump_table_case {
+	KEY_CUSTOM = 0,
+#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
+	KEY_16_BYTES,
+	KEY_32_BYTES,
+	KEY_48_BYTES,
+	KEY_64_BYTES,
+	KEY_80_BYTES,
+	KEY_96_BYTES,
+	KEY_112_BYTES,
+	KEY_128_BYTES,
+#endif
+	KEY_OTHER_BYTES,
+	NUM_KEY_CMP_CASES,
+};
 
 /* Enum used to select the implementation of the signature comparison function to use
  * eg: a system supporting SVE might want to use a NEON or scalar implementation.
@@ -117,6 +154,25 @@ void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
 	h->rte_hash_custom_cmp_eq = func;
 }
 
+/*
+ * Table storing all different key compare functions
+ * (multi-process supported)
+ */
+static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
+	[KEY_CUSTOM] = NULL,
+#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
+	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,
+	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
+	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
+	[KEY_64_BYTES] = rte_hash_k64_cmp_eq,
+	[KEY_80_BYTES] = rte_hash_k80_cmp_eq,
+	[KEY_96_BYTES] = rte_hash_k96_cmp_eq,
+	[KEY_112_BYTES] = rte_hash_k112_cmp_eq,
+	[KEY_128_BYTES] = rte_hash_k128_cmp_eq,
+#endif
+	[KEY_OTHER_BYTES] = memcmp,
+};
+
 static inline int
 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
 {
@@ -390,13 +446,13 @@ rte_hash_create(const struct rte_hash_parameters *params)
 		goto err_unlock;
 	}
 
-/*
- * If x86 architecture is used, select appropriate compare function,
- * which may use x86 intrinsics, otherwise use memcmp
- */
-#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
 	/* Select function to compare keys */
 	switch (params->key_len) {
+#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
+	/*
+	 * If x86 architecture is used, select appropriate compare function,
+	 * which may use x86 intrinsics, otherwise use memcmp
+	 */
 	case 16:
 		h->cmp_jump_table_idx = KEY_16_BYTES;
 		break;
@@ -421,13 +477,11 @@ rte_hash_create(const struct rte_hash_parameters *params)
 	case 128:
 		h->cmp_jump_table_idx = KEY_128_BYTES;
 		break;
+#endif
 	default:
 		/* If key is not multiple of 16, use generic memcmp */
 		h->cmp_jump_table_idx = KEY_OTHER_BYTES;
 	}
-#else
-	h->cmp_jump_table_idx = KEY_OTHER_BYTES;
-#endif
 
 	if (use_local_cache) {
 		local_free_slots = rte_zmalloc_socket(NULL,
lib/hash/rte_cuckoo_hash.h +1 -78

94 lines not on this page · raw message

Signature · 1 line
2.47.2
#15 lore raw

PATCH v2 2/4 hash: use static_assert

In reply to Stephen Hemminger

Now that static_assert is available, better than pre-processor use of #if condition.

---
 lib/hash/rte_cuckoo_hash.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
lib/hash/rte_cuckoo_hash.h +2 -3
index 16fe999c4c..90fda7d7e0 100644
@@ -24,9 +24,8 @@
  */
 #define RTE_HASH_BUCKET_ENTRIES		8
 
-#if !RTE_IS_POWER_OF_2(RTE_HASH_BUCKET_ENTRIES)
-#error RTE_HASH_BUCKET_ENTRIES must be a power of 2
-#endif
+static_assert(RTE_IS_POWER_OF_2(RTE_HASH_BUCKET_ENTRIES),
+	      "RTE_HASH_BUCKET_ENTRIES must be a power of 2");
 
 #define NULL_SIGNATURE			0
 
Signature · 1 line
2.47.2
#17 lore raw

PATCH v2 3/4 hash: reduce architecture special cases

In reply to Stephen Hemminger

Make comparison of sizes compatible across platforms. Keep the special case code for 16 bytes for x86 and arm64 but also add simple xor for others.

Need to keep rte_hash_k32_cmp_eq() exposed because ip_frag code poaches it.

---
 lib/hash/rte_cmp_arm64.h   | 56 +------------------------
 lib/hash/rte_cmp_generic.h | 35 ++++++++++++++++
 lib/hash/rte_cmp_x86.h     | 60 ++------------------------
 lib/hash/rte_cuckoo_hash.c | 86 +++++++++++++++++++++++++++++++++-----
 4 files changed, 116 insertions(+), 121 deletions(-)
 create mode 100644 lib/hash/rte_cmp_generic.h
lib/hash/rte_cmp_arm64.h +2 -54
index a3e85635eb..2b2a37ebd2 100644
@@ -2,7 +2,7 @@
  * Copyright(c) 2015 Cavium, Inc
  */
 
-/* Functions to compare multiple of 16 byte keys (up to 128 bytes) */
+/* Functions to compare multiple of 16 byte keys */
 static inline int
 rte_hash_k16_cmp_eq(const void *key1, const void *key2,
 		    size_t key_len __rte_unused)
@@ -27,59 +27,7 @@ rte_hash_k16_cmp_eq(const void *key1, const void *key2,
 static inline int
 rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
 {
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
 		rte_hash_k16_cmp_eq((const char *) key1 + 16,
 				(const char *) key2 + 16, key_len);
 }
-
-static inline int
-rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 16,
-				(const char *) key2 + 16, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k32_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k80_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k96_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k112_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 96,
-				(const char *) key2 + 96, key_len);
-}
-
-static inline int
-rte_hash_k128_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k64_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
lib/hash/rte_cmp_generic.h new file +35 -0
new file mode 100644
index 0000000000..f846d562e3
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Stephen Hemminger
+ */
+
+#ifndef _RTE_CMP_GENERIC_H_
+#define _RTE_CMP_GENERIC_H_
+
+/* Function to compare 16 byte keys */
+static inline int
+rte_hash_k16_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+#ifdef RTE_ARCH_64
+	const uint64_t *k1 = key1;
+	const unaligned_uint64_t *k2 = key2;
+
+	return ((k1[0] ^ k2[0]) | (k1[1] ^ k2[1])) != 0;
+#else
+	const uint32_t *k1 = key1;
+	const unaligned_uint32_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) |
+	       (k1[2] ^ k2[2]) | (k1[3] ^ k2[3]);
+#endif
+}
+
+/* Function to compare 32 byte keys */
+static inline int
+rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 16,
+				(const uint8_t *) key2 + 16, key_len);
+}
+
+#endif
lib/hash/rte_cmp_x86.h +4 -56

70 lines not on this page · raw message

lib/hash/rte_cuckoo_hash.c +75 -11

120 lines not on this page · raw message

Signature · 1 line
2.47.2
#24 lore raw

PATCH v2 4/4 hash: add support for common small key sizes

In reply to Stephen Hemminger

Add new compare functions for common small key sizes.

Bugzilla ID: 1775

---
 app/test/test_hash.c       |   4 +-
 lib/hash/rte_cuckoo_hash.c | 100 +++++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+), 1 deletion(-)
app/test/test_hash.c +3 -1
index 5791fd7f4c..24588a4a96 100644
@@ -35,7 +35,9 @@
  */
 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
 static uint32_t hashtest_initvals[] = {0};
-static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
+static uint32_t hashtest_key_lens[] = {
+	0, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 14, 15, 16, 21, 31, 32, 33, 63, 64
+};
 #define MAX_KEYSIZE 64
 /******************************************************************************/
 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15)
lib/hash/rte_cuckoo_hash.c +100 -0

118 lines not on this page · raw message

Signature · 1 line
2.47.2
#10 lore raw

RE: [RFC 3/3] hash: add support for common small key sizes

In reply to Mattias Rönnblom
Quoted 24 lines
Quoted 17 lines
Quoted 17 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static inline int
+rte_hash_k2_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 99

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +4 -0
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+

What we do now is to require the keys are 16-bit aligned (which wasn't the case before).

You could

uint16_t k1;
memcpy(&k1, key1, sizeof(uint16_t));
instead.

Quoted 6 lines
Quoted 1 line

Would generate the same code, but be safe from any future alignment issues.

Or use the unaligned types, e.g.:

	const unaligned_uint16_t *k1 = key1;
	const unaligned_uint16_t *k2 = key2;
Quoted 4 lines

Could you explain why that is safe? Doesn't __attribute__((__aligned__(1)))
just say specify the object doesn't have any alignment requirements, without asking the compiler to deal with it?

It is safe because the compiler does deal with it. Here's a simple example:
https://godbolt.org/z/r39zdeEcx

I don't know how MSVC deals with it, but it doesn't support alignment sensitive architectures, so no real problem.

Quoted 14 lines
Quoted 14 lines
Quoted 4 lines

Anyway, maybe it's safe to assume the keys are aligned, so this is not an issue.

Lots of DPDK code ignores alignment issues.

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+	return k1[0] ^ k2[0];
+}

Haven't you implemented "neq" rather than "eq" here? If the keys are equal, the result is 0. Should be != 0.

Quoted 3 lines
Quoted 1 line

Not a bug.

Well, the function body doesn't do what the function name tells it. :)

Agree. They really should be renamed to _cmp_neq.
And _cmp_eq wrappers could be kept for backwards API compatibility. Possibly marked deprecated.

Quoted 14 lines
Quoted 3 lines

These hash compare functions are in fact "neq", not "eq". Having "_cmp_eq" in the function names is extremely unfortunate and

misleading.

Quoted 10 lines
Quoted 9 lines

Would it be worth adding a comment like "use XOR to make this branch-free"? It may not be obvious to all readers.

That said, I’m not sure this trick will actually change the generated object code - especially if the result of the eq function is still used in a conditional afterward. Anyway, keeping it seems like a good conservative approach.

Quoted 6 lines
Quoted 1 line

I wonder if any compiler is clever enough to use a different memcmp

implementation if we inform the compiler at build time that we don't care if key1 is less than or greater key2, only if they differ or not.

All what is needed is a constant-size length. (Only tested with the most recent GCC and clang.)

Yes. But that was not what I was thinking about... I was wondering if "memcmp()!=0" compiles to code that calls some other memcmp implementation that doesn't check which of the two strings is lower, but only tests if they differ.

Quoted 7 lines

At least GCC will emit a cmp instruction though (so not branch free), if that matters.

Quoted 1 line

If so, the OTHER_BYTES handler shouldn't call memcmp() directly, but a

wrapper around it:

Quoted 1 line
Quoted 8 lines
Quoted 1 line

rte_hash_k_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

Quoted 4 lines
{
	return memcmp(key1, key2, key_len) != 0;
}

Should work. (Remove __rte_unused.)

I just tested it in Compiler Explorer, and the function calls the same memcmp(), regardless of the !=0. So no benefit compared to calling memcmp() directly from the jump table.

#3 lore raw

Re: [RFC 1/3] hash: move table of hash compare functions out of header

In reply to Morten Brørup
Quoted 1 line · On Fri, 22 Aug 2025 11:05:06 +0200 Morten Brørup <[email protected]>…

Nice trick explicitly indexing these here; it reduces the risk of not matching the cmp_jump_table_case.

Yes, there were bugs in other code because of table mismatches.

Quoted 2 lines

Consider adding static_assert() that RTE_DIM(cmp_jump_table) == NUM_KEY_CMP_CASES.

Since array is explictly sized probably not needed.

#11 lore raw

Re: [RFC 3/3] hash: add support for common small key sizes

In reply to Morten Brørup
Quoted 24 lines · Morten Brørup
Quoted 24 lines
Quoted 23 lines
Quoted 11 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static inline int
+rte_hash_k2_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

lib/hash/rte_cuckoo_hash.c rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len) · line 99

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +4 -0
+{
+	const uint16_t *k1 = key1;
+	const uint16_t *k2 = key2;
+

What we do now is to require the keys are 16-bit aligned (which wasn't the case before).

Quoted 7 lines

You could

uint16_t k1;
memcpy(&k1, key1, sizeof(uint16_t));
instead.

Would generate the same code, but be safe from any future alignment issues.

Or use the unaligned types, e.g.:

	const unaligned_uint16_t *k1 = key1;
	const unaligned_uint16_t *k2 = key2;
Quoted 9 lines
Quoted 4 lines

Could you explain why that is safe? Doesn't __attribute__((__aligned__(1)))
just say specify the object doesn't have any alignment requirements, without asking the compiler to deal with it?

It is safe because the compiler does deal with it. Here's a simple example:
https://godbolt.org/z/r39zdeEcx

I see. Thanks!

Quoted 37 lines

I don't know how MSVC deals with it, but it doesn't support alignment sensitive architectures, so no real problem.

Quoted 14 lines
Quoted 14 lines
Quoted 4 lines

Anyway, maybe it's safe to assume the keys are aligned, so this is not an issue.

Lots of DPDK code ignores alignment issues.

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+	return k1[0] ^ k2[0];
+}

Haven't you implemented "neq" rather than "eq" here? If the keys are equal, the result is 0. Should be != 0.

Quoted 3 lines
Quoted 1 line

Not a bug.

Well, the function body doesn't do what the function name tells it. :)

Agree. They really should be renamed to _cmp_neq.
And _cmp_eq wrappers could be kept for backwards API compatibility. Possibly marked deprecated.

Quoted 14 lines
Quoted 3 lines

These hash compare functions are in fact "neq", not "eq". Having "_cmp_eq" in the function names is extremely unfortunate and

misleading.

Quoted 10 lines
Quoted 9 lines

Would it be worth adding a comment like "use XOR to make this branch-free"? It may not be obvious to all readers.

That said, I’m not sure this trick will actually change the generated object code - especially if the result of the eq function is still used in a conditional afterward. Anyway, keeping it seems like a good conservative approach.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

I wonder if any compiler is clever enough to use a different memcmp

implementation if we inform the compiler at build time that we don't care if key1 is less than or greater key2, only if they differ or not.

All what is needed is a constant-size length. (Only tested with the most recent GCC and clang.)

Yes. But that was not what I was thinking about... I was wondering if "memcmp()!=0" compiles to code that calls some other memcmp implementation that doesn't check which of the two strings is lower, but only tests if they differ.

I don't think so. On GCC 15.2 at least, for non-constant buffer lengths, glibc memcmp() will always be invoked.

Quoted 14 lines
Quoted 14 lines

At least GCC will emit a cmp instruction though (so not branch free), if that matters.

Quoted 1 line

If so, the OTHER_BYTES handler shouldn't call memcmp() directly, but a

wrapper around it:

Quoted 2 lines

rte_hash_k_cmp_eq(const void *key1, const void *key2, size_t key_len

__rte_unused)

Quoted 4 lines
{
	return memcmp(key1, key2, key_len) != 0;
}
Quoted 5 lines
Quoted 1 line

Should work. (Remove __rte_unused.)

I just tested it in Compiler Explorer, and the function calls the same memcmp(), regardless of the !=0. So no benefit compared to calling memcmp() directly from the jump table.

The rte_hash_k_cmp_eq() must be invoked from some other function, which provides a constant key_len, for the magic to happen.

I would keep this function (maybe as __rte_hash_cmp_neq()) for that purpose.

Acked-by on v2 3/4
#18 lore raw

RE: [PATCH v2 3/4] hash: reduce architecture special cases

In reply to Stephen Hemminger
Quoted 33 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Friday, 22 August 2025 20.20

Make comparison of sizes compatible across platforms. Keep the special case code for 16 bytes for x86 and arm64 but also add simple xor for others.

Need to keep rte_hash_k32_cmp_eq() exposed because ip_frag code poaches it.

---
 lib/hash/rte_cmp_arm64.h   | 56 +------------------------
 lib/hash/rte_cmp_generic.h | 35 ++++++++++++++++
 lib/hash/rte_cmp_x86.h     | 60 ++------------------------
 lib/hash/rte_cuckoo_hash.c | 86 +++++++++++++++++++++++++++++++++-----
 4 files changed, 116 insertions(+), 121 deletions(-)
 create mode 100644 lib/hash/rte_cmp_generic.h
lib/hash/rte_cmp_arm64.h +1 -1
index a3e85635eb..2b2a37ebd2 100644
@@ -2,7 +2,7 @@
  * Copyright(c) 2015 Cavium, Inc
  */
 
-/* Functions to compare multiple of 16 byte keys (up to 128 bytes) */
+/* Functions to compare multiple of 16 byte keys */
 static inline int
 rte_hash_k16_cmp_eq(const void *key1, const void *key2,
 		    size_t key_len __rte_unused)

lib/hash/rte_cmp_arm64.h rte_hash_k16_cmp_eq(const void *key1, const void · line 33

Quoted 9 lines
lib/hash/rte_cmp_arm64.h +1 -1
@@ -27,59 +27,7 @@ rte_hash_k16_cmp_eq(const void *key1, const void
 *key2,
 static inline int
 rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
 {
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
 		rte_hash_k16_cmp_eq((const char *) key1 + 16,
 				(const char *) key2 + 16, key_len);
 }

Agree with the change from "||" to "|", to compare in blocks of 32 bytes instead of 16.

Another potential improvement:
Consider passing the actual length, instead of passing key_len, i.e.:

	return rte_hash_k16_cmp_eq(key1, key2, 16) |
		rte_hash_k16_cmp_eq((const char *) key1 + 16,
				(const char *) key2 + 16, 16);

[...]

Quoted 69 lines
lib/hash/rte_cmp_x86.h +1 -1
index ddfbef462f..e7a38c8fcd 100644
@@ -4,7 +4,7 @@
 
 #include <rte_vect.h>
 
-/* Functions to compare multiple of 16 byte keys (up to 128 bytes) */
+/* Function to compare multiple of 16 byte keys */
 static inline int
 rte_hash_k16_cmp_eq(const void *key1, const void *key2, size_t key_len
 __rte_unused)

{

lib/hash/rte_cmp_x86.h +0 -49
@@ -18,59 +18,7 @@ rte_hash_k16_cmp_eq(const void *key1, const void
 *key2, size_t key_len __rte_unu
 static inline int
 rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
 {
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 16,
-				(const char *) key2 + 16, key_len);
-}
-
-static inline int
-rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 16,
-				(const char *) key2 + 16, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k32_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k80_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k96_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k112_cmp_eq(const void *key1, const void *key2, size_t
 key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 96,
-				(const char *) key2 + 96, key_len);
-}
-

lib/hash/rte_cmp_x86.h rte_hash_k16_cmp_eq(const void *key1, const void · line 23

Quoted 10 lines
lib/hash/rte_cmp_x86.h +3 -6
-static inline int
-rte_hash_k128_cmp_eq(const void *key1, const void *key2, size_t
 key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k64_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 16,
+				(const uint8_t *) key2 + 16, key_len);

You should use "const char *" (not "const uint8_t *") here too. Or add RTE_CONST_PTR_ADD() to rte_common.h:
/**
* add a byte-value offset to a const pointer
*/
#define RTE_CONST_PTR_ADD(ptr, x) ((const void*)((uintptr_t)(ptr) + (x)))

Quoted 63 lines

}

lib/hash/rte_cuckoo_hash.c +27 -9
index 619fe0c691..199cb62bf0 100644
@@ -42,13 +42,6 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
 #define RETURN_IF_TRUE(cond, retval)
 #endif
 
-#if defined(RTE_ARCH_X86)
-#include "rte_cmp_x86.h"
-#endif
-
-#if defined(RTE_ARCH_ARM64)
-#include "rte_cmp_arm64.h"
-#endif
 
 /*
  * All different options to select a key compare function,
@@ -57,7 +50,6 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
  */
 enum cmp_jump_table_case {
 	KEY_CUSTOM = 0,
-#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
 	KEY_16_BYTES,
 	KEY_32_BYTES,
 	KEY_48_BYTES,
@@ -66,11 +58,85 @@ enum cmp_jump_table_case {
 	KEY_96_BYTES,
 	KEY_112_BYTES,
 	KEY_128_BYTES,
-#endif
 	KEY_OTHER_BYTES,
 	NUM_KEY_CMP_CASES,
 };
 
+/*
+ * Comparison functions for different key sizes.
+ * Each function is only called with a specific fixed key size.
+ *
+ * Return value is 0 on equality to allow direct use of memcmp.
+ * Recommend using XOR and | operator to avoid branching
+ * as long as key is smaller than cache line size.
+ *
+ * Key1 always points to key[] in rte_hash_key which is aligned.
+ * Key2 is parameter to insert which might not be.
+ *
+ * Special case for 16 and 32 bytes to allow for architecture
+ * specific optimizations.
+ */
+
+#if defined(RTE_ARCH_X86)
+#include "rte_cmp_x86.h"
+#elif defined(RTE_ARCH_ARM64)
+#include "rte_cmp_arm64.h"
+#else
+#include "rte_cmp_generic.h"
+#endif
+
+static int
+rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |

lib/hash/rte_cuckoo_hash.c enum cmp_jump_table_case { · line 101

Quoted 10 lines
lib/hash/rte_cuckoo_hash.c +10 -0
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 16,
+				    (const uint8_t *) key2 + 16, key_len) ||
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 32,
+				    (const uint8_t *) key2 + 32, key_len);
+}
+
+static int
+rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k32_cmp_eq(key1, key2, key_len) |

Is the "|" instead of "||", to compare in blocks of 64 bytes instead of 32, intentional?

Quoted 64 lines
lib/hash/rte_cuckoo_hash.c +38 -0
+		rte_hash_k32_cmp_eq((const uint8_t *) key1 + 32,
+				    (const uint8_t *) key2 + 32, key_len);
+}
+
+static int
+rte_hash_k80_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 64,
+				    (const uint8_t *) key2 + 64, key_len);
+}
+
+static int
+rte_hash_k96_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
+		rte_hash_k32_cmp_eq((const uint8_t *) key1 + 64,
+				    (const uint8_t *) key2 + 64, key_len);
+}
+
+static int
+rte_hash_k112_cmp_eq(const void *key1, const void *key2, size_t
 key_len)
+{
+	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
+		rte_hash_k32_cmp_eq((const uint8_t *) key1 + 64,
+				    (const uint8_t *) key2 + 64, key_len) ||
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 96,
+				    (const uint8_t *) key2 + 96, key_len);
+}
+
+static int
+rte_hash_k128_cmp_eq(const void *key1, const void *key2, size_t
 key_len)
+{
+	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
+		rte_hash_k64_cmp_eq((const uint8_t *) key1 + 64,
+				(const uint8_t *) key2 + 64, key_len);
+}
+
 /* Enum used to select the implementation of the signature comparison

function to use
* eg: a system supporting SVE might want to use a NEON or scalar
implementation.
*/

lib/hash/rte_cuckoo_hash.c +0 -1
@@ -160,7 +226,6 @@ void rte_hash_set_cmp_func(struct rte_hash *h,
 rte_hash_cmp_eq_t func)
  */
 static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
 	[KEY_CUSTOM] = NULL,
-#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
 	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,
 	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
lib/hash/rte_cuckoo_hash.c +0 -2
 	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
@@ -169,7 +234,6 @@ static const rte_hash_cmp_eq_t
 cmp_jump_table[NUM_KEY_CMP_CASES] = {
 	[KEY_96_BYTES] = rte_hash_k96_cmp_eq,
 	[KEY_112_BYTES] = rte_hash_k112_cmp_eq,
 	[KEY_128_BYTES] = rte_hash_k128_cmp_eq,
-#endif
 	[KEY_OTHER_BYTES] = memcmp,
 };
 
--
Quoted 1 line

2.47.2

With or without suggested changes...

Acked-by on v2 4/4
#25 lore raw

RE: [PATCH v2 4/4] hash: add support for common small key sizes

In reply to Stephen Hemminger
Quoted 11 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Friday, 22 August 2025 20.20

Add new compare functions for common small key sizes.

Bugzilla ID: 1775

---

#19 lore raw

Re: [PATCH v2 3/4] hash: reduce architecture special cases

In reply to Morten Brørup
Quoted 6 lines · On Tue, 26 Aug 2025 08:55:23 +0200 Morten Brørup <[email protected]>…

lib/hash/rte_cuckoo_hash.c enum cmp_jump_table_case { · line 101

Quoted 4 lines
lib/hash/rte_cuckoo_hash.c +4 -0
+static int
+rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k32_cmp_eq(key1, key2, key_len) |  

Is the "|" instead of "||", to compare in blocks of 64 bytes instead of 32, intentional?

The cost of the conditional branch is usually higher than the cost of doing a few more instructions on cached data.

#20 lore raw

RE: [PATCH v2 3/4] hash: reduce architecture special cases

Quoted 13 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 26 August 2025 15.42

Quoted 2 lines · On Tue, 26 Aug 2025 08:55:23 +0200 Morten Brørup <[email protected]>…

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int
+rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t

key_len)

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+{
+	return rte_hash_k32_cmp_eq(key1, key2, key_len) |

Is the "|" instead of "||", to compare in blocks of 64 bytes instead

Quoted 5 lines

of 32, intentional?

The cost of the conditional branch is usually higher than the cost of doing
a few more instructions on cached data.

I agree the key being looked up is very likely cached.

But the key in the hash table might not be, and the 64 byte comparison might cross a cache line.
I think using half a cache line as the breakpoint (for using conditional branch instead of unconditional load and compare) seems like a better tradeoff than a full cache line. I have no data to back this up, just a hunch. In reality, it depends on the use case. If a cache hit is more likely than a cache miss, then the unconditional comparison is preferable.
No strong opinion. I mainly wanted to ensure this was intentional.

While looking into this, I noticed a few instances where your assumption about key1 being aligned is wrong, and the key1/key2 parameters should be swapped:
https://elixir.bootlin.com/dpdk/v25.07/source/lib/hash/rte_cuckoo_hash.c#L763
https://elixir.bootlin.com/dpdk/v25.07/source/lib/hash/rte_cuckoo_hash.c#L1319
https://elixir.bootlin.com/dpdk/v25.07/source/lib/hash/rte_cuckoo_hash.c#L1357
E.g. when calling rte_hash_add_key_with_hash(), the key parameter can be unaligned, and it ripples down and becomes key1:
https://elixir.bootlin.com/dpdk/v25.07/source/lib/hash/rte_cuckoo_hash.c#L1259

#21 lore raw

Re: [PATCH v2 3/4] hash: reduce architecture special cases

Quoted 27 lines · On Tue, 26 Aug 2025 16:13:29 +0200 Morten Brørup <[email protected]>…
Quoted 13 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 26 August 2025 15.42

Quoted 2 lines · On Tue, 26 Aug 2025 08:55:23 +0200 Morten Brørup <[email protected]>…

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int
+rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t  

key_len)

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+{
+	return rte_hash_k32_cmp_eq(key1, key2, key_len) |  

Is the "|" instead of "||", to compare in blocks of 64 bytes instead

Quoted 5 lines

of 32, intentional?

The cost of the conditional branch is usually higher than the cost of doing
a few more instructions on cached data.

I agree the key being looked up is very likely cached.

But the key in the hash table might not be, and the 64 byte comparison might cross a cache line.
I think using half a cache line as the breakpoint (for using conditional branch instead of unconditional load and compare) seems like a better tradeoff than a full cache line. I have no data to back this up, just a hunch. In reality, it depends on the use case. If a cache hit is more likely than a cache miss, then the unconditional comparison is preferable.
No strong opinion. I mainly wanted to ensure this was intentional.

Quoted 6 lines

While looking into this, I noticed a few instances where your assumption about key1 being aligned is wrong, and the key1/key2 parameters should be swapped:
https://elixir.bootlin.com/dpdk/v25.07/source/lib/hash/rte_cuckoo_hash.c#L763
https://elixir.bootlin.com/dpdk/v25.07/source/lib/hash/rte_cuckoo_hash.c#L1319
https://elixir.bootlin.com/dpdk/v25.07/source/lib/hash/rte_cuckoo_hash.c#L1357
E.g. when calling rte_hash_add_key_with_hash(), the key parameter can be unaligned, and it ripples down and becomes key1:
https://elixir.bootlin.com/dpdk/v25.07/source/lib/hash/rte_cuckoo_hash.c#L1259

Ok, lets make 32 byte the watershed as comprimise.
Good catch, best to mark all keys as unaligned, but it won't matter on x86 or Arm64

#22 lore raw

Re: [PATCH v2 3/4] hash: reduce architecture special cases

In reply to Stephen Hemminger
Quoted 102 lines · Stephen Hemminger

Make comparison of sizes compatible across platforms. Keep the special case code for 16 bytes for x86 and arm64 but also add simple xor for others.

Need to keep rte_hash_k32_cmp_eq() exposed because ip_frag code poaches it.

---
  lib/hash/rte_cmp_arm64.h   | 56 +------------------------
  lib/hash/rte_cmp_generic.h | 35 ++++++++++++++++
  lib/hash/rte_cmp_x86.h     | 60 ++------------------------
  lib/hash/rte_cuckoo_hash.c | 86 +++++++++++++++++++++++++++++++++-----
  4 files changed, 116 insertions(+), 121 deletions(-)

create mode 100644 lib/hash/rte_cmp_generic.h

lib/hash/rte_cmp_arm64.h +2 -54
index a3e85635eb..2b2a37ebd2 100644
@@ -2,7 +2,7 @@
   * Copyright(c) 2015 Cavium, Inc
   */
 
-/* Functions to compare multiple of 16 byte keys (up to 128 bytes) */
+/* Functions to compare multiple of 16 byte keys */
  static inline int
  rte_hash_k16_cmp_eq(const void *key1, const void *key2,
  		    size_t key_len __rte_unused)
@@ -27,59 +27,7 @@ rte_hash_k16_cmp_eq(const void *key1, const void *key2,
  static inline int
  rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
  {
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
  		rte_hash_k16_cmp_eq((const char *) key1 + 16,
  				(const char *) key2 + 16, key_len);
  }
-
-static inline int
-rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 16,
-				(const char *) key2 + 16, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k32_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k80_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k96_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k112_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 96,
-				(const char *) key2 + 96, key_len);
-}
-
-static inline int
-rte_hash_k128_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k64_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
lib/hash/rte_cmp_generic.h new file +6 -0
new file mode 100644
index 0000000000..f846d562e3
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Stephen Hemminger
+ */
+
+#ifndef _RTE_CMP_GENERIC_H_
+#define _RTE_CMP_GENERIC_H_

lib/hash/rte_cmp_generic.h line 16

Quoted 10 lines
lib/hash/rte_cmp_generic.h new file +10 -0
+
+/* Function to compare 16 byte keys */
+static inline int
+rte_hash_k16_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+#ifdef RTE_ARCH_64
+	const uint64_t *k1 = key1;
+	const unaligned_uint64_t *k2 = key2;
+
+	return ((k1[0] ^ k2[0]) | (k1[1] ^ k2[1])) != 0;

Remove '!= 0'.

lib/hash/rte_cmp_generic.h line 28

Quoted 12 lines
lib/hash/rte_cmp_generic.h new file +12 -0
+#else
+	const uint32_t *k1 = key1;
+	const unaligned_uint32_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) |
+	       (k1[2] ^ k2[2]) | (k1[3] ^ k2[3]);
+#endif
+}
+
+/* Function to compare 32 byte keys */
+static inline int
+rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)

eq -> neq not worth the trouble. Correct? Some other patch set.

Quoted 137 lines
lib/hash/rte_cmp_generic.h new file +7 -0
+{
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 16,
+				(const uint8_t *) key2 + 16, key_len);
+}
+
+#endif
lib/hash/rte_cmp_x86.h +4 -56
index ddfbef462f..e7a38c8fcd 100644
@@ -4,7 +4,7 @@
 
  #include <rte_vect.h>
 
-/* Functions to compare multiple of 16 byte keys (up to 128 bytes) */
+/* Function to compare multiple of 16 byte keys */
  static inline int
  rte_hash_k16_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
  {
@@ -18,59 +18,7 @@ rte_hash_k16_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unu
  static inline int
  rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
  {
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 16,
-				(const char *) key2 + 16, key_len);
-}
-
-static inline int
-rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 16,
-				(const char *) key2 + 16, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k32_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k80_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k96_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k112_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 96,
-				(const char *) key2 + 96, key_len);
-}
-
-static inline int
-rte_hash_k128_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k64_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 16,
+				(const uint8_t *) key2 + 16, key_len);
  }
lib/hash/rte_cuckoo_hash.c +19 -9
index 619fe0c691..199cb62bf0 100644
@@ -42,13 +42,6 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
  #define RETURN_IF_TRUE(cond, retval)
  #endif
 
-#if defined(RTE_ARCH_X86)
-#include "rte_cmp_x86.h"
-#endif
-
-#if defined(RTE_ARCH_ARM64)
-#include "rte_cmp_arm64.h"
-#endif
 
  /*
   * All different options to select a key compare function,
@@ -57,7 +50,6 @@ RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
   */
  enum cmp_jump_table_case {
  	KEY_CUSTOM = 0,
-#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
  	KEY_16_BYTES,
  	KEY_32_BYTES,
  	KEY_48_BYTES,
@@ -66,11 +58,85 @@ enum cmp_jump_table_case {
  	KEY_96_BYTES,
  	KEY_112_BYTES,
  	KEY_128_BYTES,
-#endif
  	KEY_OTHER_BYTES,
  	NUM_KEY_CMP_CASES,
  };
 
+/*
+ * Comparison functions for different key sizes.
+ * Each function is only called with a specific fixed key size.
+ *
+ * Return value is 0 on equality to allow direct use of memcmp.
+ * Recommend using XOR and | operator to avoid branching
+ * as long as key is smaller than cache line size.
+ *
+ * Key1 always points to key[] in rte_hash_key which is aligned.
+ * Key2 is parameter to insert which might not be.
+ *
+ * Special case for 16 and 32 bytes to allow for architecture
+ * specific optimizations.
+ */
+
+#if defined(RTE_ARCH_X86)
+#include "rte_cmp_x86.h"
+#elif defined(RTE_ARCH_ARM64)
+#include "rte_cmp_arm64.h"

lib/hash/rte_cuckoo_hash.c enum cmp_jump_table_case { · line 93

Quoted 10 lines
lib/hash/rte_cuckoo_hash.c +10 -0
+#else
+#include "rte_cmp_generic.h"
+#endif
+
+static int
+rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 16,
+				    (const uint8_t *) key2 + 16, key_len) ||

'||' -> '|'?

Quoted 6 lines
lib/hash/rte_cuckoo_hash.c +6 -0
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 32,
+				    (const uint8_t *) key2 + 32, key_len);
+}
+
+static int
+rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)

lib/hash/rte_cuckoo_hash.c enum cmp_jump_table_case { · line 109

Quoted 10 lines
lib/hash/rte_cuckoo_hash.c +10 -0
+{
+	return rte_hash_k32_cmp_eq(key1, key2, key_len) |
+		rte_hash_k32_cmp_eq((const uint8_t *) key1 + 32,
+				    (const uint8_t *) key2 + 32, key_len);
+}
+
+static int
+rte_hash_k80_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||

'||' -> '|'

Same in many of the below _eq functions.

Quoted 49 lines
lib/hash/rte_cuckoo_hash.c +30 -2
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 64,
+				    (const uint8_t *) key2 + 64, key_len);
+}
+
+static int
+rte_hash_k96_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
+		rte_hash_k32_cmp_eq((const uint8_t *) key1 + 64,
+				    (const uint8_t *) key2 + 64, key_len);
+}
+
+static int
+rte_hash_k112_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
+		rte_hash_k32_cmp_eq((const uint8_t *) key1 + 64,
+				    (const uint8_t *) key2 + 64, key_len) ||
+		rte_hash_k16_cmp_eq((const uint8_t *) key1 + 96,
+				    (const uint8_t *) key2 + 96, key_len);
+}
+
+static int
+rte_hash_k128_cmp_eq(const void *key1, const void *key2, size_t key_len)
+{
+	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
+		rte_hash_k64_cmp_eq((const uint8_t *) key1 + 64,
+				(const uint8_t *) key2 + 64, key_len);
+}
+
  /* Enum used to select the implementation of the signature comparison function to use
   * eg: a system supporting SVE might want to use a NEON or scalar implementation.
   */
@@ -160,7 +226,6 @@ void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
   */
  static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
  	[KEY_CUSTOM] = NULL,
-#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
  	[KEY_16_BYTES] = rte_hash_k16_cmp_eq,
  	[KEY_32_BYTES] = rte_hash_k32_cmp_eq,
  	[KEY_48_BYTES] = rte_hash_k48_cmp_eq,
@@ -169,7 +234,6 @@ static const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
  	[KEY_96_BYTES] = rte_hash_k96_cmp_eq,
  	[KEY_112_BYTES] = rte_hash_k112_cmp_eq,
  	[KEY_128_BYTES] = rte_hash_k128_cmp_eq,
-#endif
  	[KEY_OTHER_BYTES] = memcmp,
  };
 
#23 lore raw

Re: [PATCH v2 3/4] hash: reduce architecture special cases

Quoted 116 lines · On Tue, 26 Aug 2025 16:36:38 +0200 Mattias Rönnblom <[email protected]>…
Quoted 102 lines · Stephen Hemminger

Make comparison of sizes compatible across platforms. Keep the special case code for 16 bytes for x86 and arm64 but also add simple xor for others.

Need to keep rte_hash_k32_cmp_eq() exposed because ip_frag code poaches it.

---
  lib/hash/rte_cmp_arm64.h   | 56 +------------------------
  lib/hash/rte_cmp_generic.h | 35 ++++++++++++++++
  lib/hash/rte_cmp_x86.h     | 60 ++------------------------
  lib/hash/rte_cuckoo_hash.c | 86 +++++++++++++++++++++++++++++++++-----
  4 files changed, 116 insertions(+), 121 deletions(-)

create mode 100644 lib/hash/rte_cmp_generic.h

lib/hash/rte_cmp_arm64.h +2 -54
index a3e85635eb..2b2a37ebd2 100644
@@ -2,7 +2,7 @@
   * Copyright(c) 2015 Cavium, Inc
   */
 
-/* Functions to compare multiple of 16 byte keys (up to 128 bytes) */
+/* Functions to compare multiple of 16 byte keys */
  static inline int
  rte_hash_k16_cmp_eq(const void *key1, const void *key2,
  		    size_t key_len __rte_unused)
@@ -27,59 +27,7 @@ rte_hash_k16_cmp_eq(const void *key1, const void *key2,
  static inline int
  rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)
  {
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
+	return rte_hash_k16_cmp_eq(key1, key2, key_len) |
  		rte_hash_k16_cmp_eq((const char *) key1 + 16,
  				(const char *) key2 + 16, key_len);
  }
-
-static inline int
-rte_hash_k48_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k16_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 16,
-				(const char *) key2 + 16, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k64_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k32_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 32,
-				(const char *) key2 + 32, key_len);
-}
-
-static inline int
-rte_hash_k80_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k96_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
-
-static inline int
-rte_hash_k112_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k32_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len) ||
-		rte_hash_k16_cmp_eq((const char *) key1 + 96,
-				(const char *) key2 + 96, key_len);
-}
-
-static inline int
-rte_hash_k128_cmp_eq(const void *key1, const void *key2, size_t key_len)
-{
-	return rte_hash_k64_cmp_eq(key1, key2, key_len) ||
-		rte_hash_k64_cmp_eq((const char *) key1 + 64,
-				(const char *) key2 + 64, key_len);
-}
lib/hash/rte_cmp_generic.h new file +6 -0
new file mode 100644
index 0000000000..f846d562e3
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Stephen Hemminger
+ */
+
+#ifndef _RTE_CMP_GENERIC_H_
+#define _RTE_CMP_GENERIC_H_

lib/hash/rte_cmp_generic.h line 16

Quoted 10 lines
lib/hash/rte_cmp_generic.h new file +10 -0
+
+/* Function to compare 16 byte keys */
+static inline int
+rte_hash_k16_cmp_eq(const void *key1, const void *key2, size_t key_len __rte_unused)
+{
+#ifdef RTE_ARCH_64
+	const uint64_t *k1 = key1;
+	const unaligned_uint64_t *k2 = key2;
+
+	return ((k1[0] ^ k2[0]) | (k1[1] ^ k2[1])) != 0;  

Remove '!= 0'.

Quoted 14 lines

lib/hash/rte_cmp_generic.h line 28

Quoted 12 lines
lib/hash/rte_cmp_generic.h new file +12 -0
+#else
+	const uint32_t *k1 = key1;
+	const unaligned_uint32_t *k2 = key2;
+
+	return (k1[0] ^ k2[0]) | (k1[1] ^ k2[1]) |
+	       (k1[2] ^ k2[2]) | (k1[3] ^ k2[3]);
+#endif
+}
+
+/* Function to compare 32 byte keys */
+static inline int
+rte_hash_k32_cmp_eq(const void *key1, const void *key2, size_t key_len)  

eq -> neq not worth the trouble. Correct? Some other patch set.

ipfrag is still poaching on these, so not changing