Skip to content

EliasFano: Build() optimize#19555

Closed
AskAlexSharov wants to merge 3 commits intomainfrom
alex/ef_build_optimize_34_dbg
Closed

EliasFano: Build() optimize#19555
AskAlexSharov wants to merge 3 commits intomainfrom
alex/ef_build_optimize_34_dbg

Conversation

@AskAlexSharov
Copy link
Collaborator

Idea: instead of iterating over bits, iterate over words and get first-non-zero bit in word

Details: all Build() functions use naive O(64) bit scanning per word. Replace with bits.TrailingZeros64 + word &= word-1 to iterate only over set bits directly. This compiles to a single TZCNT instruction and eliminates all the skipped-zero-bit iterations.

Result: 2.75x speedup. Also speedup scales with sparsity of upperBits — at ~34% density, the inner loop runs ~3x fewer iterations

cpu: AMD EPYC 4344P 8-Core Processor
100 elements sequence: 233.0 ns/op -> 81.98 ns/op
1M elements sequence: 2344235 ns/op -> 824938 ns/op

@AskAlexSharov AskAlexSharov marked this pull request as ready for review March 2, 2026 05:07
@AskAlexSharov AskAlexSharov mentioned this pull request Mar 2, 2026
16 tasks
Copy link
Collaborator

@Giulio2002 Giulio2002 left a comment

Choose a reason for hiding this comment

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

LGTM — clean optimization: iterating only set bits via TrailingZeros64 + clear-lowest-bit idiom instead of scanning all 64 bits. Well-known pattern, correctly applied across all EliasFano Build methods.

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.

2 participants