From 6461a80163d7744e1aa55315b09d9a329fd34014 Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Thu, 17 Dec 2020 00:46:43 +0100 Subject: [PATCH 5/8] use two independent hashes --- src/backend/access/brin/brin_bloom.c | 45 ++++++++++------------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/backend/access/brin/brin_bloom.c b/src/backend/access/brin/brin_bloom.c index 0cd6899a55..f7b405f76f 100644 --- a/src/backend/access/brin/brin_bloom.c +++ b/src/backend/access/brin/brin_bloom.c @@ -463,7 +463,7 @@ static BloomFilter * bloom_add_value(BloomFilter *filter, uint32 value, bool *updated) { int i; - uint32 big_h, h, d; + uint32 h1, h2; /* assume 'not updated' by default */ Assert(filter); @@ -533,16 +533,16 @@ bloom_add_value(BloomFilter *filter, uint32 value, bool *updated) Assert(BLOOM_IS_HASHED(filter)); /* compute the hashes, used for the bloom filter */ - big_h = ((uint32) DatumGetInt64(hash_uint32(value))); - - h = big_h % filter->nbits; - d = big_h % (filter->nbits - 1); + h1 = hash_uint32_extended(value, 0x71d924af) % filter->nbits; + h2 = hash_uint32_extended(value, 0xba48b314) % filter->nbits; /* compute the requested number of hashes */ for (i = 0; i < filter->nhashes; i++) { - int byte = (h / 8); - int bit = (h % 8); + /* h1 + h2 + f(i) */ + uint32 h = (h1 + i * h2) % filter->nbits; + uint32 byte = (h / 8); + uint32 bit = (h % 8); /* if the bit is not set, set it and remember we did that */ if (! (filter->data[byte] & (0x01 << bit))) @@ -552,14 +552,6 @@ bloom_add_value(BloomFilter *filter, uint32 value, bool *updated) if (updated) *updated = true; } - - /* next bit */ - h += d++; - if (h >= filter->nbits) - h -= filter->nbits; - - if (d == filter->nbits) - d = 0; } return filter; @@ -613,7 +605,7 @@ static bool bloom_contains_value(BloomFilter *filter, uint32 value) { int i; - uint32 big_h, h, d; + uint32 h1, h2; Assert(filter); @@ -642,28 +634,21 @@ bloom_contains_value(BloomFilter *filter, uint32 value) /* now the regular hashing mode */ Assert(BLOOM_IS_HASHED(filter)); - big_h = ((uint32) DatumGetInt64(hash_uint32(value))); - - h = big_h % filter->nbits; - d = big_h % (filter->nbits - 1); + /* calculate the two hashes */ + h1 = hash_uint32_extended(value, 0x71d924af) % filter->nbits; + h2 = hash_uint32_extended(value, 0xba48b314) % filter->nbits; /* compute the requested number of hashes */ for (i = 0; i < filter->nhashes; i++) { - int byte = (h / 8); - int bit = (h % 8); + /* h1 + h2 + f(i) */ + uint32 h = (h1 + i * h2) % filter->nbits; + uint32 byte = (h / 8); + uint32 bit = (h % 8); /* if the bit is not set, the value is not there */ if (! (filter->data[byte] & (0x01 << bit))) return false; - - /* next bit */ - h += d++; - if (h >= filter->nbits) - h -= filter->nbits; - - if (d == filter->nbits) - d = 0; } /* all hashes found in bloom filter */ -- 2.26.2