From 090d99dd2da676074145f64bce940f3867c613f1 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 6 Nov 2022 12:13:21 -0500 Subject: [PATCH] * src/hash.c (jhash_string): Help the compiler optimize the hash Invoke memcpy() with a constant length, where possible. Copyright-paperwork-exempt: yes --- src/hash.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/hash.c b/src/hash.c index 5d7ea81b..d1652f84 100644 --- a/src/hash.c +++ b/src/hash.c @@ -361,7 +361,7 @@ round_up_2 (unsigned long n) #define sum_get_unaligned_32(r, p) \ do { \ unsigned int val; \ - memcpy(&val, (p), 4); \ + memcpy (&val, (p), 4); \ r += val; \ } while(0); @@ -413,13 +413,16 @@ jhash(unsigned const char *k, int length) #define UINTSZ sizeof (unsigned int) #ifdef WORDS_BIGENDIAN -/* The ifs are ordered from the first byte in memory to the last. */ +/* The ifs are ordered from the first byte in memory to the last. + Help the compiler optimize by using static memcpy length. */ #define sum_up_to_nul(r, p, plen, flag) \ do { \ unsigned int val = 0; \ size_t pn = (plen); \ - size_t n = pn < UINTSZ ? pn : UINTSZ; \ - memcpy (&val, (p), n); \ + if (pn >= UINTSZ) \ + memcpy (&val, (p), UINTSZ); \ + else \ + memcpy (&val, (p), pn); \ if ((val & 0xFF000000) == 0) \ flag = 1; \ else if ((val & 0xFF0000) == 0) \ @@ -432,13 +435,16 @@ jhash(unsigned const char *k, int length) #else /* First detect the presence of zeroes. If there is none, we can sum the 4 bytes directly. Otherwise, the ifs are ordered as in the - big endian case, from the first byte in memory to the last. */ + big endian case, from the first byte in memory to the last. + Help the compiler optimize by using static memcpy length. */ #define sum_up_to_nul(r, p, plen, flag) \ do { \ unsigned int val = 0; \ size_t pn = (plen); \ - size_t n = pn < UINTSZ ? pn : UINTSZ; \ - memcpy (&val, (p), n); \ + if (pn >= UINTSZ) \ + memcpy (&val, (p), UINTSZ); \ + else \ + memcpy (&val, (p), pn); \ flag = ((val - 0x01010101) & ~val) & 0x80808080; \ if (!flag) \ r += val; \