Skip to content

Xuliang0317 patch 1#8287

Open
xuliang0317 wants to merge 2 commits into
apache:masterfrom
xuliang0317:xuliang0317-patch-1
Open

Xuliang0317 patch 1#8287
xuliang0317 wants to merge 2 commits into
apache:masterfrom
xuliang0317:xuliang0317-patch-1

Conversation

@xuliang0317
Copy link
Copy Markdown

@xuliang0317 xuliang0317 commented May 30, 2026

Optimize Bloom Filter bit operations by replacing array lookup with bitwise shift

While this is functional, it introduces unnecessary overhead in the read path, which is highly performance-sensitive:

Array Bounds Checking: The JVM must perform bounds checking for every array access to prevent ArrayIndexOutOfBoundsException, which adds minor but cumulative overhead.
Memory Access: Even though bitvals is likely cached in L1/L2 CPU cache, it still requires a memory load instruction.
Division/Modulo Overhead: In BloomFilterChunk#set(long pos), the code uses / 8 and % 8 which compiles to relatively expensive division instructions.
Solution
This PR optimizes the Bloom Filter bit operations by replacing the array lookup with direct bitwise shifts and optimizing the division/modulo operations:

Direct Bitwise Shift: Replaced BloomFilterUtil.bitvals[bitPos] with (1 << bitPos).
This eliminates array bounds checks completely.
The JIT compiler can optimize 1 << bitPos into a single, extremely fast CPU instruction (SHL), executing entirely within CPU registers with zero memory access latency.
Remove Unused Array: Removed the BloomFilterUtil.bitvals array completely to clean up the code.
Division to Shift Optimization: In BloomFilterChunk#set(long pos), replaced pos / 8 and pos % 8 with pos >> 3 and pos & 7. Since pos is always non-negative in this context, this is a safe and much faster alternative to division instructions.
Proposed Changes(去掉了不必要的数组,改成了位运算)

Removed the static bit-value lookup array and updated the checkBit method to use bitwise operations directly.
bit operations replace array
@xuliang0317
Copy link
Copy Markdown
Author

@Apache9 Could you review this PR when you get a chance?

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.

1 participant