Optimize check_short_circuit with early-exit bit scanning#22580
Open
telleroutlook wants to merge 2 commits into
Open
Optimize check_short_circuit with early-exit bit scanning#22580telleroutlook wants to merge 2 commits into
telleroutlook wants to merge 2 commits into
Conversation
Replace unconditional count_set_bits() with any_bit_set/any_bit_unset checks that exit on the first non-trivial word. For AND/OR short-circuit paths where the answer is obvious from a few words, this avoids a full buffer scan. Falls back to count_set_bits() only when the exact count is needed for the pre-selection threshold. Benchmarks (8192 rows): - and/one_true_first: 544µs → 450µs (-17%) - and/one_true_last: 469µs → 401µs (-15%)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #15631
What changes
Added
any_bit_set()andany_bit_unset()helpers incheck_short_circuit()that scan the BooleanBuffer word-by-word and exit on the first word that determines the answer, instead of always doing a fullcount_set_bits()popcount scan.For AND: check
any_bit_setfirst (no true values → ReturnLeft), thenany_bit_unset(all true → ReturnRight). Only fall through tocount_set_bits()when the exact count is needed for the pre-selection threshold.For OR: check
any_bit_unsetfirst (no false values → ReturnLeft), thenany_bit_set(all false → ReturnRight).Why
count_set_bits()scans the entire buffer regardless of the data. For cases where the first few words already tell us the answer (all zeros, all ones, or even just "has at least one set bit"), we can skip the full scan.Benchmark results
8192 rows, existing
binary_opbench:The biggest wins are in the mixed cases where early exit avoids a full buffer scan before falling through to the pre-selection path. The "all same" cases were already fast and see no significant change.
Test plan
cargo bench --bench binary_op -- short_circuit