Skip to content

perf: optimize bloom membership checks - #1134

Merged
DaniPopes merged 2 commits into
alloy-rs:mainfrom
0xalpharush:perf/bloom-optimizations
Jun 30, 2026
Merged

DaniPopes merged 2 commits into
alloy-rs:mainfrom
0xalpharush:perf/bloom-optimizations

Conversation

@0xalpharush

@0xalpharush 0xalpharush commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR contains two small performance optimizations in alloy-primitives focused on bloom membership checks and bitset containment.

Commits

590dcc1 perf: avoid temporary bitset in FixedBytes covers

Rewrites FixedBytes::covers to reduce directly over paired bytes instead of computing (*self & *other) == *other.

This avoids materializing an intermediate fixed-byte value and lets optimized builds lower bloom containment to a tighter byte/vector reduction.

1ed997c perf: avoid temporary blooms in membership checks

Updates bloom membership helpers to test the three Ethereum bloom bits directly.

This avoids constructing temporary Bloom values in:

  • Bloom::contains_input
  • Bloom::contains_raw_log

It also routes Bloom::accrue through the existing pre-hashed m3_2048_hashed helper, adds concise documentation for the direct bit-index extraction, and replaces the raw-log containment example with a proptest that compares the direct path against the old temporary-bloom behavior for arbitrary blooms, addresses, and topic lists.

Verification

  • cargo +nightly fmt --all --check
  • RUSTDOCFLAGS="--cfg docsrs -D warnings -Zunstable-options --show-type-layout --generate-link-to-definition" cargo +nightly doc --workspace --all-features --no-deps --document-private-items
  • cargo test -p alloy-primitives bits::bloom --lib
  • cargo test -p alloy-primitives --no-default-features bits::bloom --lib
  • cargo +nightly test -p alloy-primitives --all-features contains_raw_log_matches_bloom_contains --lib
  • RUSTFLAGS=-Dwarnings cargo +nightly clippy -p alloy-primitives --all-targets --all-features

Earlier full-branch verification before the test/doc amend also passed:

  • cargo test --workspace
  • cargo test --workspace --no-default-features
  • cargo +nightly test --workspace --all-features

@0xalpharush

Copy link
Copy Markdown
Contributor Author

Cargo asm before/after from this machine (cargo asm 0.2.62, --release --rust --intel, aarch64 Apple target):

Bloom::contains

Before (origin/main): materializes a 256-byte temporary on the stack, ANDs into it, then calls _memcmp.

sub sp, sp, #288
...
and.16b v0, v4, v0
and.16b v1, v5, v1
and.16b v2, v6, v2
and.16b v3, v7, v3
stp q0, q1, [sp, #192]
stp q2, q3, [sp, #224]
...
mov x0, sp
mov w2, #256
bl _memcmp
cmp w0, #0
cset w0, eq

After (PR): no stack temporary and no _memcmp; lowers to direct other & !self vector reduction.

ldp q0, q1, [x0]
ldp q4, q5, [x1]
bic.16b v0, v4, v0
bic.16b v1, v5, v1
...
orr.16b v0, v1, v0
orr.16b v0, v2, v0
orr.16b v0, v3, v0
...
tst w8, #0xff
cset w0, eq
ret

Bloom::contains_raw_log

Before (origin/main): builds a temporary bloom, calls accrue_raw_log, then performs the same temporary-bitset plus _memcmp containment check.

sub sp, sp, #512
movi.2d v0, #0000000000000000
stp q0, q0, [sp, #224]
...
mov x0, sp
bl alloy_primitives::bits::bloom::Bloom::accrue_raw_log
...
add x0, sp, #256
mov x1, sp
mov w2, #256
bl _memcmp
cmp w0, #0
cset w0, eq

After (PR): tests the three bloom bits from each hash directly. The printed function body is larger because the hash/check loop is now inlined instead of hidden behind accrue_raw_log, but the temporary bloom construction and final _memcmp are gone, and failed bit checks can return early.

ldr x8, [sp, #336]
lsr x9, x8, #8
and x10, x9, #0xf8
bfi x10, x8, #8, #3
lsr x10, x10, #3
eor x10, x10, #0xff
ldrb w10, [x20, x10]
ubfx x11, x8, #8, #3
lsr w10, w10, w11
tbz w10, #0, LBB22_8
...
tbnz w8, #0, LBB22_4

Takeaway: the FixedBytes::covers change is a clear codegen win for bloom containment: it removes the 256-byte temporary and the _memcmp call. The contains_raw_log change removes the temporary bloom path and checks membership directly; raw inputs remain Keccak-bound, but the non-hash work is reduced and can short-circuit.

@0xalpharush
0xalpharush force-pushed the perf/bloom-optimizations branch from af97716 to 8c67a87 Compare June 30, 2026 17:37
@0xalpharush
0xalpharush force-pushed the perf/bloom-optimizations branch from 8c67a87 to 1ed997c Compare June 30, 2026 18:17
@0xalpharush
0xalpharush marked this pull request as ready for review June 30, 2026 18:18
@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy Jun 30, 2026
@DaniPopes
DaniPopes enabled auto-merge (squash) June 30, 2026 18:32
@DaniPopes
DaniPopes merged commit b98d12b into alloy-rs:main Jun 30, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants