v5-0002-Inline-string_hash-and-call-out-to-fasthash-inste.patch

text/x-patch

Filename: v5-0002-Inline-string_hash-and-call-out-to-fasthash-inste.patch
Type: text/x-patch
Part: 3
Message: Re: Change GUC hashtable to use simplehash?

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v5-0002
Subject: Inline string_hash and call out to fasthash instead of hash_bytes
File+
src/backend/utils/hash/dynahash.c 20 0
src/common/hashfn.c 0 19
src/include/common/hashfn.h 0 1
src/include/common/hashfn_unstable.h 8 1
From 0b29afbee9cf795136b722841c8fc4ee4667ee0d Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@postgresql.org>
Date: Tue, 28 Nov 2023 18:09:00 +0700
Subject: [PATCH v5 2/4] Inline string_hash and call out to fasthash instead of
 hash_bytes

---
 src/backend/utils/hash/dynahash.c    | 20 ++++++++++++++++++++
 src/common/hashfn.c                  | 19 -------------------
 src/include/common/hashfn.h          |  1 -
 src/include/common/hashfn_unstable.h |  9 ++++++++-
 4 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c
index 012d4a0b1f..6ca1442647 100644
--- a/src/backend/utils/hash/dynahash.c
+++ b/src/backend/utils/hash/dynahash.c
@@ -98,6 +98,7 @@
 
 #include "access/xact.h"
 #include "common/hashfn.h"
+#include "common/hashfn_unstable.h"
 #include "port/pg_bitutils.h"
 #include "storage/shmem.h"
 #include "storage/spin.h"
@@ -307,6 +308,25 @@ string_compare(const char *key1, const char *key2, Size keysize)
 	return strncmp(key1, key2, keysize - 1);
 }
 
+/*
+ * string_hash: hash function for keys that are NUL-terminated strings.
+ *
+ * NOTE: this is the default hash function if none is specified.
+ */
+static inline uint32
+string_hash(const void *key, Size keysize)
+{
+	/*
+	 * If the string exceeds keysize-1 bytes, we want to hash only that many,
+	 * because when it is copied into the hash table it will be truncated at
+	 * that length.
+	 */
+	size_t		s_len = strlen((const char *) key);
+
+	s_len = Min(s_len, keysize - 1);
+	return fasthash32(key, s_len, 0);
+}
+
 
 /************************** CREATE ROUTINES **********************/
 
diff --git a/src/common/hashfn.c b/src/common/hashfn.c
index 2490607eea..65e4dd07ba 100644
--- a/src/common/hashfn.c
+++ b/src/common/hashfn.c
@@ -651,25 +651,6 @@ hash_bytes_uint32_extended(uint32 k, uint64 seed)
 	return ((uint64) b << 32) | c;
 }
 
-/*
- * string_hash: hash function for keys that are NUL-terminated strings.
- *
- * NOTE: this is the default hash function if none is specified.
- */
-uint32
-string_hash(const void *key, Size keysize)
-{
-	/*
-	 * If the string exceeds keysize-1 bytes, we want to hash only that many,
-	 * because when it is copied into the hash table it will be truncated at
-	 * that length.
-	 */
-	Size		s_len = strlen((const char *) key);
-
-	s_len = Min(s_len, keysize - 1);
-	return hash_bytes((const unsigned char *) key, (int) s_len);
-}
-
 /*
  * tag_hash: hash function for fixed-size tag values
  */
diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h
index adc1dc1de8..54ab616ba4 100644
--- a/src/include/common/hashfn.h
+++ b/src/include/common/hashfn.h
@@ -52,7 +52,6 @@ hash_uint32_extended(uint32 k, uint64 seed)
 }
 #endif
 
-extern uint32 string_hash(const void *key, Size keysize);
 extern uint32 tag_hash(const void *key, Size keysize);
 extern uint32 uint32_hash(const void *key, Size keysize);
 
diff --git a/src/include/common/hashfn_unstable.h b/src/include/common/hashfn_unstable.h
index 76ed27c0a0..04a24934bc 100644
--- a/src/include/common/hashfn_unstable.h
+++ b/src/include/common/hashfn_unstable.h
@@ -23,7 +23,6 @@
    SOFTWARE.
 */
 
-#include "fasthash.h"
 
 // Compression function for Merkle-Damgard construction.
 // This function is generated using the framework provided.
@@ -36,6 +35,7 @@ static inline uint64_t mix(uint64_t h) {
 
 // security: if the system allows empty keys (len=3) the seed is exposed, the reverse of mix.
 // objsize: 0-1fd: 509
+static inline
 uint64_t fasthash64(const void *buf, size_t len, uint64_t seed)
 {
 	const uint64_t    m = 0x880355f21e6d1965ULL;
@@ -56,11 +56,17 @@ uint64_t fasthash64(const void *buf, size_t len, uint64_t seed)
 
 	switch (len & 7) {
 	case 7: v ^= (uint64_t)pos2[6] << 48;
+		/* FALLTHROUGH */
 	case 6: v ^= (uint64_t)pos2[5] << 40;
+		/* FALLTHROUGH */
 	case 5: v ^= (uint64_t)pos2[4] << 32;
+		/* FALLTHROUGH */
 	case 4: v ^= (uint64_t)pos2[3] << 24;
+		/* FALLTHROUGH */
 	case 3: v ^= (uint64_t)pos2[2] << 16;
+		/* FALLTHROUGH */
 	case 2: v ^= (uint64_t)pos2[1] << 8;
+		/* FALLTHROUGH */
 	case 1: v ^= (uint64_t)pos2[0];
 		h ^= mix(v);
 		h *= m;
@@ -70,6 +76,7 @@ uint64_t fasthash64(const void *buf, size_t len, uint64_t seed)
 } 
 
 // objsize: 0-236: 566
+static inline
 uint32_t fasthash32(const void *buf, size_t len, uint32_t seed)
 {
 	// the following trick converts the 64-bit hashcode to Fermat
-- 
2.42.0