Skip to contentSkip to conversation

dwarf_loader: use a better hashing function

PATCH Superseded Bill Wendling 3 participants 11 messages Feb 10, 2021 – Feb 12, 2021 BPF

lore
Share & tools
lore
#0 lore raw

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
index d3aa416..ea201ab 100644
@@ -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
#1 lore raw
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
---
 hash.h | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
hash.h +10 -16
index d3aa416..ea201ab 100644
@@ -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

#2 lore raw
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

#3 lore raw
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

#4 lore raw

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

#5 lore raw
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

#6 lore raw

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

#7 lore raw

PATCH v2 1/1 dwarf_loader: use a better hashing function

In reply to 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 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
index d3aa416..6f952c7 100644
@@ -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
#8 lore raw

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.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
index d3aa416..6f952c7 100644
@@ -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
#9 lore raw

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:

:-)

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.

#10 lore raw
In reply to Arnaldo Carvalho de Melo
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:

:-)

Doh! You're right. Sorry about that. Thanks for catching it!

-bw