summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Naylor2024-01-21 12:19:14 +0000
committerJohn Naylor2024-02-06 07:39:36 +0000
commit9ed3ee5001b6a2d4cb0166eb8f12a457f30aaca4 (patch)
tree67ea542a24cea71cad13cc64ca36bd425d7685f7 /src
parent1f61680327c962d5bafdbf6a04ad9bb0f7232f92 (diff)
Simplify initialization of incremental hash state
The standalone functions fasthash{32,64} use length for two purposes: how many bytes to hash, and how to perturb the internal seed. Developers using the incremental interface may not know the length ahead of time (e.g. for C strings). In this case, it's advised to pass length to the finalizer, but initialization still needed some length up front, in the form of a placeholder macro. Separate the concerns by having the standalone functions perturb the internal seed themselves from their own length parameter, allowing to remove "len" from fasthash_init(), as well as the placeholder macro. Discussion: https://postgr.es/m/CANWCAZbTUk2LOyhsFo33gjLyLAHZ7ucXCi5K9u%3D%2BPtnTShDKtw%40mail.gmail.com
Diffstat (limited to 'src')
-rw-r--r--src/backend/catalog/namespace.c2
-rw-r--r--src/include/common/hashfn_unstable.h19
2 files changed, 9 insertions, 12 deletions
diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index b610aa62423..8df30b24401 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 3d927e1fb18..63ebe295c5f 100644
--- a/src/include/common/hashfn_unstable.h
+++ b/src/include/common/hashfn_unstable.h
@@ -65,15 +65,12 @@
* in fasthash_accum_cstring() :
*
* fasthash_state hs;
- * fasthash_init(&hs, FH_UNKNOWN_LENGTH, 0);
+ * fasthash_init(&hs, 0);
* len = fasthash_accum_cstring(&hs, *str);
* ...
* return fasthash_final32(&hs, len);
*
- * Here we pass FH_UNKNOWN_LENGTH as a convention, since passing zero
- * would zero out the internal seed as well. fasthash_accum_cstring()
- * returns the length of the string, which is computed on-the-fly while
- * mixing the string into the hash. Experimentation has found that
+ * The length is computed on-the-fly. Experimentation has found that
* SMHasher fails unless we incorporate the length, so it is passed to
* the finalizer as a tweak.
*/
@@ -89,20 +86,17 @@ typedef struct fasthash_state
#define FH_SIZEOF_ACCUM sizeof(uint64)
-#define FH_UNKNOWN_LENGTH 1
/*
* Initialize the hash state.
*
- * 'len' is the length of the input, if known ahead of time.
- * If that is not known, pass FH_UNKNOWN_LENGTH.
* '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 +322,10 @@ fasthash64(const char *k, int len, uint64 seed)
{
fasthash_state hs;
- fasthash_init(&hs, len, seed);
+ fasthash_init(&hs, 0);
+
+ /* re-initialize the seed according to input length */
+ hs.hash = seed ^ (len * 0x880355f21e6d1965);
while (len >= FH_SIZEOF_ACCUM)
{