From bc5a4751be5b535b3414fc12cdde4a7cb5c325e0 Mon Sep 17 00:00:00 2001
From: ross39 <Rheaney26@gmail.com>
Date: Thu, 3 Jul 2025 12:21:38 +0100
Subject: [PATCH 2/2] update to use rational hashing. Still working out some
 performance details

---
 src/backend/lib/bloomfilter.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/backend/lib/bloomfilter.c b/src/backend/lib/bloomfilter.c
index 9875ec9aed8..0e79a07e284 100644
--- a/src/backend/lib/bloomfilter.c
+++ b/src/backend/lib/bloomfilter.c
@@ -400,8 +400,8 @@ lossless_bloom_create(bloom_config *config)
 	filter->blocks[0].size = filter->total_bits;
 	filter->blocks[0].offset = 0;
 	filter->blocks[0].k_rational = log(2.0) * filter->total_bits / config->total_elems;
-	filter->blocks[0].k_floor = Max(1, Min((int)rint(filter->blocks[0].k_rational), MAX_HASH_FUNCS));
-	filter->blocks[0].k_fractional = 0.0;  /* Disable fractional hashing for speed */
+	filter->blocks[0].k_floor = Max(1, Min((int)floor(filter->blocks[0].k_rational), MAX_HASH_FUNCS));
+	filter->blocks[0].k_fractional = filter->blocks[0].k_rational - filter->blocks[0].k_floor;  /* Enable fractional hashing */
 	
 	/* Allocate bitset */
 	bitset_bytes = (filter->total_bits + 7) / 8;
@@ -498,6 +498,17 @@ lossless_bloom_add_element(lossless_bloom_filter *filter, unsigned char *elem, s
 		hashes[i] = x;
 	}
 	
+	/* RATIONAL HASHING: Apply fractional hash function probabilistically */
+	if (filter->blocks[0].k_fractional > 0.0 && k_hash_funcs < MAX_HASH_FUNCS)
+	{
+		if (apply_rational_hashing(filter->blocks[0].k_fractional, base_hash))
+		{
+			x += y;
+			hashes[k_hash_funcs] = x;
+			k_hash_funcs++;
+		}
+	}
+	
 	/* OPTIMIZATION: Fast bit setting with bitwise operations */
 	for (i = 0; i < k_hash_funcs; i++)
 	{
@@ -551,6 +562,17 @@ lossless_bloom_contains_element(lossless_bloom_filter *filter, unsigned char *el
 		hashes[i] = x;
 	}
 	
+	/* RATIONAL HASHING: Apply fractional hash function probabilistically */
+	if (filter->blocks[0].k_fractional > 0.0 && k_hash_funcs < MAX_HASH_FUNCS)
+	{
+		if (apply_rational_hashing(filter->blocks[0].k_fractional, base_hash))
+		{
+			x += y;
+			hashes[k_hash_funcs] = x;
+			k_hash_funcs++;
+		}
+	}
+	
 	/* OPTIMIZATION: Fast bit checking with bitwise operations */
 	for (i = 0; i < k_hash_funcs; i++)
 	{
-- 
2.39.5 (Apple Git-154)

