From feda40cd4fd1b3f13f4d3621ad3e5e4923c42c0e Mon Sep 17 00:00:00 2001 From: Nickita Khylkouski <90287684+nickita-khylkouski@users.noreply.github.com> Date: Wed, 4 Feb 2026 11:36:47 -0800 Subject: [PATCH] Make BloomFilter.bitSize() public This change makes the existing bitSize() method public (previously package-private with @VisibleForTesting) to allow users to determine the serialization size of a BloomFilter without actually serializing it. This is useful when pre-allocating space in file formats that embed Bloom filters. The serialization size can be calculated as: bitSize() / 8 + 6 bytes (for the header). The method is documented with: - Javadoc explaining its purpose and the serialization size formula - @since 35.0 annotation Also adds testBitSizeMatchesSerializationSize() test to verify that bitSize() correctly predicts the writeTo() output size. Fixes #6866 --- .../google/common/hash/BloomFilterTest.java | 41 +++++++++++++++++++ .../com/google/common/hash/BloomFilter.java | 14 +++++-- .../google/common/hash/BloomFilterTest.java | 41 +++++++++++++++++++ .../com/google/common/hash/BloomFilter.java | 14 +++++-- 4 files changed, 104 insertions(+), 6 deletions(-) diff --git a/android/guava-tests/test/com/google/common/hash/BloomFilterTest.java b/android/guava-tests/test/com/google/common/hash/BloomFilterTest.java index 80e80e7860b0..e71003b273cc 100644 --- a/android/guava-tests/test/com/google/common/hash/BloomFilterTest.java +++ b/android/guava-tests/test/com/google/common/hash/BloomFilterTest.java @@ -353,6 +353,47 @@ public void testBitSize() { } } + /** + * Tests that bitSize() can be used to predict the serialization size produced by writeTo(). + * + *
The serialization format consists of a 6-byte header (1 byte strategy, 1 byte hash
+ * functions, 4 bytes array length) followed by the bit array data (bitSize / 8 bytes).
+ */
+ public void testBitSizeMatchesSerializationSize() throws Exception {
+ int[] expectedInsertionValues = {1, 10, 100, 1000, 10000};
+ double[] fppValues = {0.01, 0.03, 0.1};
+
+ for (int expectedInsertions : expectedInsertionValues) {
+ for (double fpp : fppValues) {
+ BloomFilter This can be useful when pre-allocating space for serialization. The number of bytes written
+ * by {@link #writeTo(OutputStream)} is {@code bitSize() / 8 + 6} (6 bytes for the header: 1 byte
+ * for the strategy, 1 byte for the number of hash functions, and 4 bytes for the array length).
+ *
+ * @return the number of bits in this Bloom filter's underlying bit array
+ * @since 35.0
+ */
+ public long bitSize() {
return bits.bitSize();
}
diff --git a/guava-tests/test/com/google/common/hash/BloomFilterTest.java b/guava-tests/test/com/google/common/hash/BloomFilterTest.java
index 665612d6db34..f7780fe2652a 100644
--- a/guava-tests/test/com/google/common/hash/BloomFilterTest.java
+++ b/guava-tests/test/com/google/common/hash/BloomFilterTest.java
@@ -355,6 +355,47 @@ public void testBitSize() {
}
}
+ /**
+ * Tests that bitSize() can be used to predict the serialization size produced by writeTo().
+ *
+ * The serialization format consists of a 6-byte header (1 byte strategy, 1 byte hash
+ * functions, 4 bytes array length) followed by the bit array data (bitSize / 8 bytes).
+ */
+ public void testBitSizeMatchesSerializationSize() throws Exception {
+ int[] expectedInsertionValues = {1, 10, 100, 1000, 10000};
+ double[] fppValues = {0.01, 0.03, 0.1};
+
+ for (int expectedInsertions : expectedInsertionValues) {
+ for (double fpp : fppValues) {
+ BloomFilter This can be useful when pre-allocating space for serialization. The number of bytes written
+ * by {@link #writeTo(OutputStream)} is {@code bitSize() / 8 + 6} (6 bytes for the header: 1 byte
+ * for the strategy, 1 byte for the number of hash functions, and 4 bytes for the array length).
+ *
+ * @return the number of bits in this Bloom filter's underlying bit array
+ * @since 35.0
+ */
+ public long bitSize() {
return bits.bitSize();
}