Skip to content

perf: manual Ord for FixedBytes using integer chunk comparison - #1160

Merged
DaniPopes merged 2 commits into
alloy-rs:mainfrom
xzav3r:perf/fixed-bytes-ord
Aug 4, 2026
Merged

DaniPopes merged 2 commits into
alloy-rs:mainfrom
xzav3r:perf/fixed-bytes-ord

Conversation

@xzav3r

@xzav3r xzav3r commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

The derived Ord for FixedBytes<N> lowers to a memcmp libcall. For the
short fixed lengths used as map keys (Address = 20 bytes, B256 = 32
bytes), the call overhead dominates the comparison itself, and the libcall is
opaque to the inliner.

In a downstream application (an order-matching engine that keeps its state in
BTreeMap<Address, _>), memcmp reached 17.6% flat CPU in profiles.
Replacing the derived Ord with chunked comparison raised application
throughput by ~10% on a 16-core Xeon.

Solution

Implement Ord manually: compare big-endian integer chunks of decreasing
size (u64 / u32 / u16 / u8), written with split_first_chunk in the
same style as map::fixed::write_bytes_unrolled. Each chunk is one
byte-swapped load per side, and the whole function inlines into callers such
as BTreeMap node searches.

For N > 32 the implementation falls back to the plain slice comparison:
benchmarks over both random pairs (the first chunk decides) and equal pairs
(worst case: the full length is compared) show that 32 is the largest size
where the unrolled comparison wins the worst case on both x86-64 (glibc's
AVX2 memcmp takes over between 32 and 64) and aarch64 (between 128 and
256). The cutoff also caps the amount of inlined code.

The ordering is bit-for-bit identical to the derived (byte-lexicographic)
one. ord_matches_slice_ord pins cmp against the slice ordering across
many values of N — including sizes above the cutoff — with equal arrays and
single-byte differences at every position.

Benchmarks

fixed_bytes_ord compares the unrolled comparison against the slice-based
ordering (the previous derived impl) in the same binary.

Headline numbers (1024 random pairs / 100k-key BTreeMap<Address, _> hit
lookups):

benchmark unrolled memcmp change
x86-64: 20B cmp 936.7 ns 1605 ns −41.6%
x86-64: 32B cmp 938.5 ns 1601 ns −41.4%
x86-64: btreemap_get 91.1 ns 126.9 ns −28.2%
aarch64: 20B cmp 345.9 ns 532.4 ns −35.0%
aarch64: 32B cmp 517.9 ns 538.1 ns −3.8%
aarch64: btreemap_get 63.1 ns 69.7 ns −9.5%

Cutoff: equal pairs (worst case, full-length compare), 1024 pairs:

N x86-64 unrolled x86-64 memcmp aarch64 unrolled aarch64 memcmp
8 403 ns 1074 ns 397 ns 462 ns
20 674 ns 1335 ns 549 ns 784 ns
32 1.21 µs 1.34 µs 689 ns 895 ns
64 2.09 µs 1.61 µs 1.38 µs 1.53 µs
128 3.77 µs 2.55 µs 3.34 µs 3.38 µs
256 8.21 µs 4.35 µs 6.62 µs 5.47 µs

x86-64: Intel Xeon, Linux (glibc); aarch64: Apple M-series, macOS. Each
number is the criterion median, taken as the median across repeated runs.

Reproduce with:

cargo bench -p alloy-primitives --features rand --bench primitives -- fixed_bytes_ord

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

The derived Ord lowers to a memcmp libcall. For short fixed lengths
(Address, B256) the call overhead dominates the comparison itself,
which hurts hot paths such as BTreeMap node searches.

Compare big-endian u64 chunks instead (then u32/u16/u8 for the tail).
This preserves the exact byte-lexicographic ordering and inlines into
callers. An equivalence test pins cmp against the slice ordering for
many N, and a benchmark group compares against the old behavior.
@xzav3r
xzav3r force-pushed the perf/fixed-bytes-ord branch from dbe7dd4 to b8288ca Compare July 29, 2026 07:15
@xzav3r

xzav3r commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @mattsse and @DaniPopes, this is Xzavier from X Layer team. Just a friendly reminder 😊. I'd really appreciate it if you could take a look when you have a chance. This optimization is quite important for our project. Thanks!

Comment thread crates/primitives/src/bits/fixed.rs Resolved
Comment thread crates/primitives/benches/primitives.rs Resolved
@github-project-automation github-project-automation Bot moved this to In Progress in Alloy Aug 4, 2026
…lback above cutoff

- Rewrite the chunked comparison in the style of map::fixed::write_bytes_unrolled,
  using split_first_chunk over decreasing chunk sizes (u64/u32/u16/u8).
- Fall back to the slice comparison (memcmp) for N > 32. Benchmarks across
  N = 8/20/32/64/128/256 with random and equal (worst-case, full-length
  compare) pairs show the unrolled comparison wins the equal case up to
  N = 32 on both x86-64 (glibc's AVX2 memcmp takes over between 32 and 64)
  and aarch64 (between 128 and 256); the cutoff also caps inlined code size.
- Extend the Ord benches to cover the higher sizes and both input shapes,
  and the ord_matches_slice_ord test to cover the fallback path (64, 128).
@xzav3r

xzav3r commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @DaniPopes! Both points addressed in the latest commit:

  1. Rewrote the comparison with split_first_chunk over decreasing chunk
    sizes, in the same style as map::fixed::write_bytes_unrolled. As a nice
    side effect the 20-byte case got ~16% faster than the previous revision
    (better tail codegen).

  2. Benchmarked N = 8/20/32/64/128/256 over random pairs (first chunk decides)
    and equal pairs (worst case: full-length compare, where memcmp's
    vectorization pays off). The crossover is platform-dependent — between 32
    and 64 on x86-64 (glibc's AVX2 memcmp), between 128 and 256 on aarch64 —
    so cmp now falls back to the slice comparison for N > 32, the largest
    size that wins the worst case on both platforms. This also caps the
    inlined code size. Full tables in the updated PR description.

@xzav3r
xzav3r requested a review from DaniPopes August 4, 2026 14:55

@DaniPopes DaniPopes 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.

Thanks!

@github-project-automation github-project-automation Bot moved this from In Progress to Reviewed in Alloy Aug 4, 2026
@DaniPopes
DaniPopes merged commit fc86734 into alloy-rs:main Aug 4, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Aug 4, 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