Skip to content

Commit 6555fe1

Browse files
committed
Revert "Speed up tail processing when hashing aligned C strings, take two"
This reverts commit a365d9e. Older versions of Valgrind raise an error, so go back to the bytewise loop for the final word in the input. Reported-by: Anton A. Melnikov <a.melnikov@postgrespro.ru> Discussion: https://postgr.es/m/a3a959f6-14b8-4819-ac04-eaf2aa2e868d@postgrespro.ru Backpatch-through: 17
1 parent f4af451 commit 6555fe1

File tree

1 file changed

+10
-36
lines changed

1 file changed

+10
-36
lines changed

src/include/common/hashfn_unstable.h

+10-36
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,6 @@ fasthash_accum(fasthash_state *hs, const char *k, size_t len)
219219
#define haszero64(v) \
220220
(((v) - 0x0101010101010101) & ~(v) & 0x8080808080808080)
221221

222-
/* get first byte in memory order */
223-
#ifdef WORDS_BIGENDIAN
224-
#define firstbyte64(v) ((v) >> 56)
225-
#else
226-
#define firstbyte64(v) ((v) & 0xFF)
227-
#endif
228-
229222
/*
230223
* all-purpose workhorse for fasthash_accum_cstring
231224
*/
@@ -262,7 +255,7 @@ static inline size_t
262255
fasthash_accum_cstring_aligned(fasthash_state *hs, const char *str)
263256
{
264257
const char *const start = str;
265-
uint64 chunk;
258+
size_t remainder;
266259
uint64 zero_byte_low;
267260

268261
Assert(PointerIsAligned(start, uint64));
@@ -282,7 +275,7 @@ fasthash_accum_cstring_aligned(fasthash_state *hs, const char *str)
282275
*/
283276
for (;;)
284277
{
285-
chunk = *(uint64 *) str;
278+
uint64 chunk = *(uint64 *) str;
286279

287280
#ifdef WORDS_BIGENDIAN
288281
zero_byte_low = haszero64(pg_bswap64(chunk));
@@ -297,33 +290,14 @@ fasthash_accum_cstring_aligned(fasthash_state *hs, const char *str)
297290
str += FH_SIZEOF_ACCUM;
298291
}
299292

300-
if (firstbyte64(chunk) != 0)
301-
{
302-
size_t remainder;
303-
uint64 mask;
304-
305-
/*
306-
* The byte corresponding to the NUL will be 0x80, so the rightmost
307-
* bit position will be in the range 15, 23, ..., 63. Turn this into
308-
* byte position by dividing by 8.
309-
*/
310-
remainder = pg_rightmost_one_pos64(zero_byte_low) / BITS_PER_BYTE;
311-
312-
/*
313-
* Create a mask for the remaining bytes so we can combine them into
314-
* the hash. This must have the same result as mixing the remaining
315-
* bytes with fasthash_accum().
316-
*/
317-
#ifdef WORDS_BIGENDIAN
318-
mask = ~UINT64CONST(0) << BITS_PER_BYTE * (FH_SIZEOF_ACCUM - remainder);
319-
#else
320-
mask = ~UINT64CONST(0) >> BITS_PER_BYTE * (FH_SIZEOF_ACCUM - remainder);
321-
#endif
322-
hs->accum = chunk & mask;
323-
fasthash_combine(hs);
324-
325-
str += remainder;
326-
}
293+
/*
294+
* The byte corresponding to the NUL will be 0x80, so the rightmost bit
295+
* position will be in the range 7, 15, ..., 63. Turn this into byte
296+
* position by dividing by 8.
297+
*/
298+
remainder = pg_rightmost_one_pos64(zero_byte_low) / BITS_PER_BYTE;
299+
fasthash_accum(hs, str, remainder);
300+
str += remainder;
327301

328302
return str - start;
329303
}

0 commit comments

Comments
 (0)