From 46e84c4782e2a1291430be4a7d4651de7f387608 Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@postgresql.org>
Date: Sun, 21 Jan 2024 19:19:14 +0700
Subject: [PATCH v15 1/4] Initialization of incremental hashing no longer uses
 length

When the incremental interface was written, care was taken to make
sure the answer always matched upstream when the length was known
ahead of time.

When that is not known ahead of time, callers that create a hash
function using the incremental interface were already advised to
incorporate length into the finalizer once it is known e.g. after
hashing a C string. Experimentation has shown that this also works
well when the length is known ahead of time, so there's no advantage
to having two places to pass the length.

Further, if the length is a compile-time constant in this case,
it can't possibly be needed as a tiebreaker for this caller, so
there's not much point in using it to affect the internal seed upon
initialization.

It's worthwhile that the standalone functions fasthash{32,64} can still
give the same answer as the original namesakes, but it's trivial for
them to reset the seed after initialization. Hence, do that and remove
"len" from fasthash_init, as well as the macro for unknown length.

TODO: comment updates
---
 src/backend/catalog/namespace.c      | 2 +-
 src/include/common/hashfn_unstable.h | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index b610aa6242..8df30b2440 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -256,7 +256,7 @@ spcachekey_hash(SearchPathCacheKey key)
 	fasthash_state hs;
 	int			sp_len;
 
-	fasthash_init(&hs, FH_UNKNOWN_LENGTH, 0);
+	fasthash_init(&hs, 0);
 
 	hs.accum = key.roleid;
 	fasthash_combine(&hs);
diff --git a/src/include/common/hashfn_unstable.h b/src/include/common/hashfn_unstable.h
index 3d927e1fb1..8e829297fd 100644
--- a/src/include/common/hashfn_unstable.h
+++ b/src/include/common/hashfn_unstable.h
@@ -89,7 +89,6 @@ typedef struct fasthash_state
 
 #define FH_SIZEOF_ACCUM sizeof(uint64)
 
-#define FH_UNKNOWN_LENGTH 1
 
 /*
  * Initialize the hash state.
@@ -99,10 +98,10 @@ typedef struct fasthash_state
  * 'seed' can be zero.
  */
 static inline void
-fasthash_init(fasthash_state *hs, int len, uint64 seed)
+fasthash_init(fasthash_state *hs, uint64 seed)
 {
 	memset(hs, 0, sizeof(fasthash_state));
-	hs->hash = seed ^ (len * 0x880355f21e6d1965);
+	hs->hash = seed ^ 0x880355f21e6d1965;
 }
 
 /* both the finalizer and part of the combining step */
@@ -328,7 +327,8 @@ fasthash64(const char *k, int len, uint64 seed)
 {
 	fasthash_state hs;
 
-	fasthash_init(&hs, len, seed);
+	fasthash_init(&hs, 0);
+	hs.hash = seed ^ (len * 0x880355f21e6d1965);
 
 	while (len >= FH_SIZEOF_ACCUM)
 	{
-- 
2.43.0

