dwarf_loader: use a better hashing function
PATCH 1/1 dwarf_loader: use a better hashing function
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits.
The new hashing funciton gives a modest performance boost.
Original New
0:11.41 0:11.38
0:11.36 0:11.34
0:11.35 0:11.26
-----------------------Avg: 0:11.373 0:11.327
for a performance improvement of 0.4%.
[1] From Numerical Recipes, 3rd Ed. 7.1.4 Random Hashes and Random Bytes
--- hash.h | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-)
hash.h +10 -15
@@ -33,22 +33,17 @@static inline uint64_t hash_64 (const uint64_t val,const unsigned int bits) { -uint64_t hash = val; +uint64_t hash = val *0x369DEA0F31A53F85UL +0x255992D382208B61UL ; -/* Sigh, gcc can't optimise this alone like it does for 32 bits. */ -uint64_t n = hash; - n <<=18 ; - hash -= n; - n <<=33 ; - hash -= n; - n <<=3 ; - hash += n; - n <<=3 ; - hash -= n; - n <<=4 ; - hash += n; - n <<=2 ; - hash += n; + hash ^= hash >>21 ; + hash ^= hash <<37 ; + hash ^= hash >>4 ; + + hash *=0x422E19E1D95D2F0DUL ; + + hash ^= hash <<20 ; + hash ^= hash >>41 ; + hash ^= hash <<5 ;/* High bits are more random, so use them. */ return hash >> (64 - bits);
Signature · 1 line
2.30.0.478.g8a0d178c01-goog
Quoted 12 lines · Bill Wendling
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits.
The new hashing funciton gives a modest performance boost.
Original New 0:11.41 0:11.38 0:11.36 0:11.34 0:11.35 0:11.26 -----------------------
Quoted 6 lines
Avg: 0:11.373 0:11.327
for a performance improvement of 0.4%.
[1] From Numerical Recipes, 3rd Ed. 7.1.4 Random Hashes and Random Bytes
Can you please also test with the one libbpf uses internally:
return (val * 11400714819323198485llu) >> (64 - bits);
?
Thanks!
Quoted 45 lines
Signed-off-by Bill Wendling <[email protected]>--- hash.h | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-)hash.h +10
-16@@ -33,22 +33,17 @@static inline uint64_t hash_64 (const uint64_t val,const unsigned int bits) { -uint64_t hash = val; +uint64_t hash = val *0x369DEA0F31A53F85UL +0x255992D382208B61UL ; -/* Sigh, gcc can't optimise this alone like it does for 32 bits. */ -uint64_t n = hash; - n <<=18 ; - hash -= n; - n <<=33 ; - hash -= n; - n <<=3 ; - hash += n; - n <<=3 ; - hash -= n; - n <<=4 ; - hash += n; - n <<=2 ; - hash += n; + hash ^= hash >>21 ; + hash ^= hash <<37 ; + hash ^= hash >>4 ; + + hash *=0x422E19E1D95D2F0DUL ; + + hash ^= hash <<20 ; + hash ^= hash >>41 ; + hash ^= hash <<5 ;/* High bits are more random, so use them. */ return hash >> (64 - bits); --2.30.0.478.g8a0d178c01-goog
Quoted 21 lines · Andrii Nakryiko
Quoted 12 lines · Bill Wendling
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits.
The new hashing funciton gives a modest performance boost.
Original New 0:11.41 0:11.38 0:11.36 0:11.34 0:11.35 0:11.26 -----------------------Quoted 6 lines
Avg: 0:11.373 0:11.327
for a performance improvement of 0.4%.
[1] From Numerical Recipes, 3rd Ed. 7.1.4 Random Hashes and Random Bytes
Quoted 8 lines
Can you please also test with the one libbpf uses internally:
return (val * 11400714819323198485llu) >> (64 - bits);
?
Thanks!
It's giving me a running time of ~11.11s, which is even better. Would you like me to submit a patch?
-bw
Quoted 26 lines · Bill Wendling
Quoted 23 lines · Andrii Nakryiko
Quoted 12 lines · Bill Wendling
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits.
The new hashing funciton gives a modest performance boost.
Original New 0:11.41 0:11.38 0:11.36 0:11.34 0:11.35 0:11.26 -----------------------Quoted 6 lines
Avg: 0:11.373 0:11.327
for a performance improvement of 0.4%.
[1] From Numerical Recipes, 3rd Ed. 7.1.4 Random Hashes and Random Bytes
Can you please also test with the one libbpf uses internally:
Quoted 8 lines
Quoted 6 lines
return (val * 11400714819323198485llu) >> (64 - bits);
?
Thanks!
It's giving me a running time of ~11.11s, which is even better. Would you like me to submit a patch?
faster is better, so yeah, why not? :)
Quoted 2 lines
-bw
Em Wed, Feb 10, 2021 at 05:31:48PM -0800, Andrii Nakryiko escreveu:
Quoted 22 lines · Bill Wendling
Quoted 20 lines · Andrii Nakryiko
Quoted 19 lines · Bill Wendling
Quoted 11 lines
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits.
The new hashing funciton gives a modest performance boost.
Original New 0:11.41 0:11.38 0:11.36 0:11.34 0:11.35 0:11.26 -----------------------Quoted 5 lines
Avg: 0:11.373 0:11.327
for a performance improvement of 0.4%.
[1] From Numerical Recipes, 3rd Ed. 7.1.4 Random Hashes and Random Bytes
Can you please also test with the one libbpf uses internally:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
return (val * 11400714819323198485llu) >> (64 - bits);
?
It's giving me a running time of ~11.11s, which is even better. Would you like me to submit a patch?
faster is better, so yeah, why not? :)
Yeah, I agree, faster is better, please make it so :-)
- Arnaldo
Quoted 26 lines · Arnaldo Carvalho de Melo
Em Wed, Feb 10, 2021 at 05:31:48PM -0800, Andrii Nakryiko escreveu:
Quoted 23 lines · Bill Wendling
Quoted 22 lines · Andrii Nakryiko
Quoted 21 lines · Bill Wendling
Quoted 11 lines
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits.
The new hashing funciton gives a modest performance boost.
Original New 0:11.41 0:11.38 0:11.36 0:11.34 0:11.35 0:11.26 -----------------------Quoted 5 lines
Avg: 0:11.373 0:11.327
for a performance improvement of 0.4%.
[1] From Numerical Recipes, 3rd Ed. 7.1.4 Random Hashes and Random Bytes
Can you please also test with the one libbpf uses internally:
return (val * 11400714819323198485llu) >> (64 - bits);
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
?
It's giving me a running time of ~11.11s, which is even better. Would you like me to submit a patch?
faster is better, so yeah, why not? :)
Yeah, I agree, faster is better, please make it so :-)
Your wish is my command! :-) Done.
-bw
Em Thu, Feb 11, 2021 at 10:55:32PM -0800, Bill Wendling escreveu:
Quoted 30 lines
Quoted 28 lines · Arnaldo Carvalho de Melo
Em Wed, Feb 10, 2021 at 05:31:48PM -0800, Andrii Nakryiko escreveu:
Quoted 25 lines · Bill Wendling
Quoted 24 lines · Andrii Nakryiko
Quoted 23 lines · Bill Wendling
Quoted 11 lines
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits.
The new hashing funciton gives a modest performance boost.
Original New 0:11.41 0:11.38 0:11.36 0:11.34 0:11.35 0:11.26 -----------------------Quoted 5 lines
Avg: 0:11.373 0:11.327
for a performance improvement of 0.4%.
[1] From Numerical Recipes, 3rd Ed. 7.1.4 Random Hashes and Random Bytes
Can you please also test with the one libbpf uses internally:
return (val * 11400714819323198485llu) >> (64 - bits);
?
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
It's giving me a running time of ~11.11s, which is even better. Would you like me to submit a patch?
faster is better, so yeah, why not? :)
Yeah, I agree, faster is better, please make it so :-)
Your wish is my command! :-) Done.
Thanks, looking for the patch and applying!
No go think about something else to make it faster 8-)
- Arnaldo
PATCH v2 1/1 dwarf_loader: use a better hashing function
In reply to Bill WendlingThis hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits. The new hashing function gives a modest performance boost:
Original: 0:11.373s
New: 0:11.110s
for a performance improvement of ~2%.
[1] From the hash function used in libbpf.
--- hash.h | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-)
hash.h +1 -19
@@ -33,25 +33,7 @@static inline uint64_t hash_64 (const uint64_t val,const unsigned int bits) { -uint64_t hash = val; - -/* Sigh, gcc can't optimise this alone like it does for 32 bits. */ -uint64_t n = hash; - n <<=18 ; - hash -= n; - n <<=33 ; - hash -= n; - n <<=3 ; - hash += n; - n <<=3 ; - hash -= n; - n <<=4 ; - hash += n; - n <<=2 ; - hash += n; - -/* High bits are more random, so use them. */ -return hash >> (64 - bits); +return (val *11400714819323198485LLU ) >> (64 - bits); }static inline uint32_t hash_32 (uint32_t val,unsigned int bits)
Signature · 1 line
2.30.0.478.g8a0d178c01-goog
Em Fri, Feb 12, 2021 at 12:01:04AM -0800, Bill Wendling escreveu:
Quoted 4 lines
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits. The new hashing function gives a modest performance boost:
Some tidbits:
You forgot to CC Andrii and also to add this, which I'm doing now:
:-)
- Arnaldo
Quoted 46 lines
Original: 0:11.373s
New: 0:11.110sfor a performance improvement of ~2%.
[1] From the hash function used in libbpf.
Signed-off-by Bill Wendling <[email protected]>--- hash.h | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-)hash.h +1
-19@@ -33,25 +33,7 @@static inline uint64_t hash_64 (const uint64_t val,const unsigned int bits) { -uint64_t hash = val; - -/* Sigh, gcc can't optimise this alone like it does for 32 bits. */ -uint64_t n = hash; - n <<=18 ; - hash -= n; - n <<=33 ; - hash -= n; - n <<=3 ; - hash += n; - n <<=3 ; - hash -= n; - n <<=4 ; - hash += n; - n <<=2 ; - hash += n; - -/* High bits are more random, so use them. */ -return hash >> (64 - bits); +return (val *11400714819323198485LLU ) >> (64 - bits); }static inline uint32_t hash_32 (uint32_t val,unsigned int bits)Signature · 1 line
2.30.0.478.g8a0d178c01-goog
Signature · 1 line
- Arnaldo
Em Fri, Feb 12, 2021 at 09:37:22AM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 6 lines
Em Fri, Feb 12, 2021 at 12:01:04AM -0800, Bill Wendling escreveu:
Quoted 4 lines
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits. The new hashing function gives a modest performance boost:
Quoted 7 lines
Some tidbits:
You forgot to CC Andrii and also to add this, which I'm doing now:
Suggested-by Andrii Nakryiko <[email protected]>:-)
See below the full cset, that will go public after some more tests here.
- Arnaldo
commit 9fecc77ed82d429fd3fe49ba275465813228e617 (HEAD -> master)
Author: Bill Wendling <[email protected]> Date: Fri Feb 12 00:01:04 2021 -0800
dwarf_loader: Use a better hashing function, from libbpf
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits. The new hashing function gives a modest performance boost:
Original: 0:11.373s
New: 0:11.110s
for a performance improvement of ~2%.
[1] From the hash function used in libbpf.
Committer notes:
Bill found the suboptimality of the hash function being used, Andrii suggested using the libbpf one, which ended up being better.
Quoted 7 lines · Arnaldo Carvalho de Melo
Em Fri, Feb 12, 2021 at 12:01:04AM -0800, Bill Wendling escreveu:
Quoted 4 lines
This hashing function[1] produces better hash table bucket distributions. The original hashing function always produced zeros in the three least significant bits. The new hashing function gives a modest performance boost:
Quoted 8 lines
Some tidbits:
You forgot to CC Andrii and also to add this, which I'm doing now:
Suggested-by Andrii Nakryiko <[email protected]>:-)
Doh! You're right. Sorry about that. Thanks for catching it!
-bw