Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Fix Bloom filter bit addressing past 2^32 bits - #823

Merged
leerho merged 1 commit into
apache:mainfrom
jaideeppyne:fix/bloom-bit-index-above-2-32
Dec 3, 2026
Merged

leerho merged 1 commit into
apache:mainfrom
jaideeppyne:fix/bloom-bit-index-above-2-32

Conversation

@jaideeppyne

Copy link
Copy Markdown
Contributor

Fixes #822.

HeapBitArray and both direct bit arrays computed the word or byte index with (int) index >>> n. The cast binds tighter than the shift, so the index is narrowed to 32 bits first. Every bit at or above 2^32 then aliases a bit in the low 32-bit range.

A filter built with createByAccuracy for more than 2^32 bits (about 512MiB) therefore only uses the low 2^32 bits. Inserts into the upper half collide with the lower half, and the observed false positive rate is higher than the one requested. Filters at or below 2^32 bits are unchanged.

The shift now happens first. The on-heap word index is cast to int only after >>> 6 (it still fits, because MAX_BITS / 64 is Integer.MAX_VALUE). The direct byte offset stays a long.

Tests:

  • HeapBitArrayTest.wordIndexKeepsBitsAtOrAbove2To32Distinct checks the index math: bit 2^32 is word 2^26, not word 0.
  • HeapBitArrayTest.bitAtOrAbove2To32DoesNotAliasLowBits checks that an out-of-range index no longer writes bit 0, and that an in-range bit at 2^32+5 does not alias bit 5. The in-range case allocates a 512MiB filter and is skipped only when the heap is under 1.2GB.
  • DirectBitArrayTest.bitAtOrAbove2To32DoesNotWrapToTheFirstByte checks the writable and read-only direct arrays.

On unpatched main those two behavior tests fail (Expected ArrayIndexOutOfBoundsException / Expected IndexOutOfBoundsException, because the wrapped index hits byte 0). With this patch, mvn test -Dtest=HeapBitArrayTest,DirectBitArrayTest,DirectBitArrayRTest is 36 tests, 0 failures. The in-range 512MiB case ran (it was not skipped).

(int) index >>> n casts before the shift, so every bit index at or above
2^32 aliases a bit in the low 32-bit range. A filter larger than 512MiB
then behaves as a 2^32-bit filter and its false positive rate is higher
than requested.

Shift first. The on-heap word index is narrowed after the shift, and the
direct byte offset stays a long.

@leerho leerho left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@leerho
leerho merged commit 6fbe165 into apache:main Dec 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bloom filters larger than 2^32 bits seem to have a higher false positive rate than requested

2 participants