enable inline optimizations when creating hash keys from literals

Wed, 24 Dec 2025 12:13:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 24 Dec 2025 12:13:59 +0100
changeset 1665
b79405fbf91d
parent 1664
e5a8c41ecb58
child 1666
1ac3c150ec56

enable inline optimizations when creating hash keys from literals

src/cx/hash_key.h file | annotate | diff | comparison | revisions
src/hash_key.c file | annotate | diff | comparison | revisions
--- a/src/cx/hash_key.h	Wed Dec 24 12:00:33 2025 +0100
+++ b/src/cx/hash_key.h	Wed Dec 24 12:13:59 2025 +0100
@@ -89,7 +89,12 @@
  * @param x the integer
  * @return the hash
  */
-CX_EXPORT uint32_t cx_hash_u32(uint32_t x);
+CX_INLINE uint32_t cx_hash_u32(uint32_t x) {
+    x = ((x >> 16) ^ x) * 0x45d9f3bu;
+    x = ((x >> 16) ^ x) * 0x45d9f3bu;
+    x = (x >> 16) ^ x;
+    return x;
+}
 
 /**
  * Mixes up a 64-bit integer to be used as a hash.
@@ -99,60 +104,12 @@
  * @param x the integer
  * @return the hash
  */
-CX_EXPORT uint64_t cx_hash_u64(uint64_t x);
-
-/**
- * Computes a hash key from a 32-bit integer.
- *
- * @param x the integer
- * @return the hash key
- */
-cx_attr_nodiscard
-CX_EXPORT CxHashKey cx_hash_key_u32(uint32_t x);
-
-/**
- * Computes a hash key from a 64-bit integer.
- *
- * @param x the integer
- * @return the hash key
- */
-cx_attr_nodiscard
-CX_EXPORT CxHashKey cx_hash_key_u64(uint64_t x);
-
-/**
- * Computes a hash key from a string.
- *
- * The string needs to be zero-terminated.
- *
- * @param str the string
- * @return the hash key
- */
-cx_attr_nodiscard cx_attr_cstr_arg(1)
-CX_EXPORT CxHashKey cx_hash_key_str(const char *str);
-
-/**
- * Computes a hash key from a string.
- *
- * Use this function when the string is represented
- * as an unsigned char array.
- *
- * The string needs to be zero-terminated.
- *
- * @param str the string
- * @return the hash key
- */
-cx_attr_nodiscard cx_attr_cstr_arg(1)
-CX_EXPORT CxHashKey cx_hash_key_ustr(const unsigned char *str);
-
-/**
- * Computes a hash key from a byte array.
- *
- * @param bytes the array
- * @param len the length
- * @return the hash key
- */
-cx_attr_nodiscard cx_attr_access_r(1, 2)
-CX_EXPORT CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len);
+CX_INLINE uint64_t cx_hash_u64(uint64_t x){
+    x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
+    x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
+    x = x ^ (x >> 31);
+    return x;
+}
 
 /**
  * Computes a hash key for an arbitrary object.
@@ -170,13 +127,75 @@
 CX_EXPORT CxHashKey cx_hash_key(const void *obj, size_t len);
 
 /**
- * Computes a hash key from a UCX string.
+ * Computes a hash key from a 32-bit integer.
+ *
+ * @param x the integer
+ * @return the hash key
+ */
+cx_attr_nodiscard
+CX_INLINE CxHashKey cx_hash_key_u32(uint32_t x) {
+    CxHashKey key;
+    key.data = NULL;
+    key.len = 0;
+    key.hash = cx_hash_u32(x);
+    return key;
+}
+
+/**
+ * Computes a hash key from a 64-bit integer.
+ *
+ * @param x the integer
+ * @return the hash key
+ */
+cx_attr_nodiscard
+CX_INLINE CxHashKey cx_hash_key_u64(uint64_t x) {
+    CxHashKey key;
+    key.data = NULL;
+    key.len = 0;
+    key.hash = cx_hash_u64(x);
+    return key;
+}
+
+/**
+ * Computes a hash key from a string.
+ *
+ * The string needs to be zero-terminated.
  *
  * @param str the string
  * @return the hash key
  */
-cx_attr_nodiscard
-CX_EXPORT CxHashKey cx_hash_key_cxstr(cxstring str);
+cx_attr_nodiscard cx_attr_cstr_arg(1)
+CX_INLINE CxHashKey cx_hash_key_str(const char *str) {
+    return cx_hash_key((const void*)str, str == NULL ? 0 : strlen(str));
+}
+
+/**
+ * Computes a hash key from a string.
+ *
+ * Use this function when the string is represented
+ * as an unsigned char array.
+ *
+ * The string needs to be zero-terminated.
+ *
+ * @param str the string
+ * @return the hash key
+ */
+cx_attr_nodiscard cx_attr_cstr_arg(1)
+CX_INLINE CxHashKey cx_hash_key_ustr(const unsigned char *str) {
+    return cx_hash_key((const void*)str, str == NULL ? 0 : strlen((const char*)str));
+}
+
+/**
+ * Computes a hash key from a byte array.
+ *
+ * @param bytes the array
+ * @param len the length
+ * @return the hash key
+ */
+cx_attr_nodiscard cx_attr_access_r(1, 2)
+CX_INLINE CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len) {
+    return cx_hash_key((const void*)bytes, len);
+}
 
 /**
  * Computes a hash key from a UCX string.
@@ -185,7 +204,20 @@
  * @return the hash key
  */
 cx_attr_nodiscard
-CX_EXPORT CxHashKey cx_hash_key_mutstr(cxmutstr str);
+CX_INLINE CxHashKey cx_hash_key_cxstr(cxstring str) {
+    return cx_hash_key((void*)str.ptr, str.length);
+}
+
+/**
+ * Computes a hash key from a UCX string.
+ *
+ * @param str the string
+ * @return the hash key
+ */
+cx_attr_nodiscard
+CX_INLINE CxHashKey cx_hash_key_mutstr(cxmutstr str) {
+    return cx_hash_key((void*)str.ptr, str.length);
+}
 
 /**
  * The identity function for the CX_HASH_KEY() macro.
--- a/src/hash_key.c	Wed Dec 24 12:00:33 2025 +0100
+++ b/src/hash_key.c	Wed Dec 24 12:13:59 2025 +0100
@@ -82,56 +82,6 @@
     key->hash = h;
 }
 
-
-uint32_t cx_hash_u32(uint32_t x) {
-    x = ((x >> 16) ^ x) * 0x45d9f3bu;
-    x = ((x >> 16) ^ x) * 0x45d9f3bu;
-    x = (x >> 16) ^ x;
-    return x;
-}
-
-uint64_t cx_hash_u64(uint64_t x) {
-    x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
-    x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
-    x = x ^ (x >> 31);
-    return x;
-}
-
-CxHashKey cx_hash_key_str(const char *str) {
-    CxHashKey key;
-    key.data = str;
-    key.len = str == NULL ? 0 : strlen(str);
-    cx_hash_murmur(&key);
-    return key;
-}
-
-CxHashKey cx_hash_key_ustr(unsigned const char *str) {
-    CxHashKey key;
-    key.data = str;
-    key.len = str == NULL ? 0 : strlen((const char*)str);
-    cx_hash_murmur(&key);
-    return key;
-}
-
-CxHashKey cx_hash_key_cxstr(cxstring str) {
-    return cx_hash_key(str.ptr, str.length);
-}
-
-CxHashKey cx_hash_key_mutstr(cxmutstr str) {
-    return cx_hash_key(str.ptr, str.length);
-}
-
-CxHashKey cx_hash_key_bytes(
-        const unsigned char *bytes,
-        size_t len
-) {
-    CxHashKey key;
-    key.data = bytes;
-    key.len = len;
-    cx_hash_murmur(&key);
-    return key;
-}
-
 CxHashKey cx_hash_key(
         const void *obj,
         size_t len
@@ -143,22 +93,6 @@
     return key;
 }
 
-CxHashKey cx_hash_key_u32(uint32_t x) {
-    CxHashKey key;
-    key.data = NULL;
-    key.len = 0;
-    key.hash = cx_hash_u32(x);
-    return key;
-}
-
-CxHashKey cx_hash_key_u64(uint64_t x) {
-    CxHashKey key;
-    key.data = NULL;
-    key.len = 0;
-    key.hash = cx_hash_u64(x);
-    return key;
-}
-
 int cx_hash_key_cmp(const void *l, const void *r) {
     const CxHashKey *left = l;
     const CxHashKey *right = r;

mercurial