Repository navigation
perf: manual Ord for FixedBytes using integer chunk comparison - #1160
Conversation
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.
dbe7dd4 to
b8288ca
Compare
|
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! |
…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).
|
Thanks for the review @DaniPopes! Both points addressed in the latest commit:
|
Motivation
The derived
OrdforFixedBytes<N>lowers to amemcmplibcall. For theshort fixed lengths used as map keys (
Address= 20 bytes,B256= 32bytes), 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, _>),memcmpreached 17.6% flat CPU in profiles.Replacing the derived
Ordwith chunked comparison raised applicationthroughput by ~10% on a 16-core Xeon.
Solution
Implement
Ordmanually: compare big-endian integer chunks of decreasingsize (
u64/u32/u16/u8), written withsplit_first_chunkin thesame style as
map::fixed::write_bytes_unrolled. Each chunk is onebyte-swapped load per side, and the whole function inlines into callers such
as
BTreeMapnode searches.For
N > 32the 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
memcmptakes over between 32 and 64) and aarch64 (between 128 and256). 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_ordpinscmpagainst the slice ordering acrossmany values of
N— including sizes above the cutoff — with equal arrays andsingle-byte differences at every position.
Benchmarks
fixed_bytes_ordcompares the unrolled comparison against the slice-basedordering (the previous derived impl) in the same binary.
Headline numbers (1024 random pairs / 100k-key
BTreeMap<Address, _>hitlookups):
cmpcmpbtreemap_getcmpcmpbtreemap_getCutoff: equal pairs (worst case, full-length compare), 1024 pairs:
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:
PR Checklist