Skip to content

Commit 9ed3ee5

Browse files
committed
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
1 parent 1f61680 commit 9ed3ee5

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/backend/catalog/namespace.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ spcachekey_hash(SearchPathCacheKey key)
256256
fasthash_state hs;
257257
int sp_len;
258258

259-
fasthash_init(&hs, FH_UNKNOWN_LENGTH, 0);
259+
fasthash_init(&hs, 0);
260260

261261
hs.accum = key.roleid;
262262
fasthash_combine(&hs);

src/include/common/hashfn_unstable.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,12 @@
6565
* in fasthash_accum_cstring() :
6666
*
6767
* fasthash_state hs;
68-
* fasthash_init(&hs, FH_UNKNOWN_LENGTH, 0);
68+
* fasthash_init(&hs, 0);
6969
* len = fasthash_accum_cstring(&hs, *str);
7070
* ...
7171
* return fasthash_final32(&hs, len);
7272
*
73-
* Here we pass FH_UNKNOWN_LENGTH as a convention, since passing zero
74-
* would zero out the internal seed as well. fasthash_accum_cstring()
75-
* returns the length of the string, which is computed on-the-fly while
76-
* mixing the string into the hash. Experimentation has found that
73+
* The length is computed on-the-fly. Experimentation has found that
7774
* SMHasher fails unless we incorporate the length, so it is passed to
7875
* the finalizer as a tweak.
7976
*/
@@ -89,20 +86,17 @@ typedef struct fasthash_state
8986

9087
#define FH_SIZEOF_ACCUM sizeof(uint64)
9188

92-
#define FH_UNKNOWN_LENGTH 1
9389

9490
/*
9591
* Initialize the hash state.
9692
*
97-
* 'len' is the length of the input, if known ahead of time.
98-
* If that is not known, pass FH_UNKNOWN_LENGTH.
9993
* 'seed' can be zero.
10094
*/
10195
static inline void
102-
fasthash_init(fasthash_state *hs, int len, uint64 seed)
96+
fasthash_init(fasthash_state *hs, uint64 seed)
10397
{
10498
memset(hs, 0, sizeof(fasthash_state));
105-
hs->hash = seed ^ (len * 0x880355f21e6d1965);
99+
hs->hash = seed ^ 0x880355f21e6d1965;
106100
}
107101

108102
/* both the finalizer and part of the combining step */
@@ -328,7 +322,10 @@ fasthash64(const char *k, int len, uint64 seed)
328322
{
329323
fasthash_state hs;
330324

331-
fasthash_init(&hs, len, seed);
325+
fasthash_init(&hs, 0);
326+
327+
/* re-initialize the seed according to input length */
328+
hs.hash = seed ^ (len * 0x880355f21e6d1965);
332329

333330
while (len >= FH_SIZEOF_ACCUM)
334331
{

0 commit comments

Comments
 (0)