From f217c117b3a759b11ac3665c368e6353d9b0f5d9 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Sat, 7 Nov 2020 20:36:38 +0100
Subject: [PATCH 6/8] one-hash tweaks

---
 src/backend/access/brin/brin_bloom.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/backend/access/brin/brin_bloom.c b/src/backend/access/brin/brin_bloom.c
index 73f8478b4c..f6e65c0157 100644
--- a/src/backend/access/brin/brin_bloom.c
+++ b/src/backend/access/brin/brin_bloom.c
@@ -292,7 +292,7 @@ static BloomFilter *bloom_switch_to_hashing(BloomFilter *filter);
  *
  * WIP: very naive prime sieve; could be optimized using segmented ranges
  */
-static uint16 *
+static uint32 *
 generate_primes(int limit)
 {
 	/* upper bound of number of primes below limit */
@@ -300,7 +300,7 @@ generate_primes(int limit)
 	int numprimes = 1.26 * limit / log(limit);
 
 	bool *is_composite = (bool *) palloc0(limit * sizeof(bool));
-	uint16 *primes = (uint16 *) palloc0(numprimes * sizeof(uint16));
+	uint32 *primes = (uint32 *) palloc0(numprimes * sizeof(uint32));
 
 	int maxfactor = floor(sqrt(limit));
 	int factor = 2;	/* first prime */
@@ -344,13 +344,18 @@ set_bloom_partitions(int nbits, uint8 nhashes, uint16 *partlens)
 	int		pidx = 0;
 	int		sum = 0;
 	int		target_partlen = nbits / nhashes;
+	uint32 *primes;
+
+	elog(LOG, "generating primes nbits %u nhashes %u target_partlen %d", nbits, nhashes, target_partlen);
 
 	/*
 	 * Increase the limit to ensure we have some primes higher than
 	 * the target partition length. The 100 value is arbitrary, but
 	 * should be well over what we need.
 	 */
-	uint16 *primes = generate_primes(target_partlen + 100);
+	primes = generate_primes(target_partlen + 100);
+
+	elog(LOG, "primes generated");
 
 	/*
 	 * In our array of primes, find a sequence of length nhashes, whose
@@ -358,12 +363,14 @@ set_bloom_partitions(int nbits, uint8 nhashes, uint16 *partlens)
 	 * array will be filled with zeros, so we need to guard against that.
 	 */
 	while (primes[pidx + nhashes - 1] <= target_partlen &&
-		   primes[pidx] > 0)
+		   primes[pidx + nhashes] > 0)
 		pidx++;
 
 	for (int i = 0; i < nhashes; i++)
 		sum += primes[pidx + i];
 
+	elog(LOG, "nbits %d sum %d", nbits, sum);
+
 	/*
 	 * Since all the primes are less than or equal the desired partition
 	 * length, the sum is somewhat less than nbits. Increment the starting
@@ -383,6 +390,8 @@ set_bloom_partitions(int nbits, uint8 nhashes, uint16 *partlens)
 		pidx++;
 	}
 
+	elog(LOG, "nbits %d sum %d", nbits, sum);
+
 	memcpy(partlens, &primes[pidx], nhashes * sizeof(uint16));
 
 	/* WIP: assuming it's not important to pfree primes */
-- 
2.26.2

