Repository navigation
perf(precompile): unroll blake2 portable rounds to const-fold the message schedule - #3840
Conversation
Merging this PR will degrade performance by 20.47%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Simulation | blake2 portable/10_rounds |
2.1 µs | 3.3 µs | -36.16% |
| ❌ | Simulation | blake2 portable/12_rounds |
2.3 µs | 3.4 µs | -32.96% |
| ⚡ | Simulation | blake2 portable/64_rounds |
6.4 µs | 5.4 µs | +17.52% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing 4waan:perf/blake2-portable-const-schedule (6b1d8e4) with main (fac897f)
Footnotes
-
1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩
|
On CodSpeed: also a note on a clippy cascade: Unrolling makes |
…edule The portable compression function indexed SIGMA with a runtime round index, which cost the const-folding upstream blake2b_simd gets from calling round() with a constant. Per round on aarch64 that meant 16 ldrb loads of the schedule row, 16 message loads at addresses dependent on those bytes, and an r % 10 reciprocal multiply. Take the round index as a const generic and unroll ten physical rounds, using a remaining counter for EIP-152's variable round count. SIGMA has spec period 10, so ten rounds tile any count and re-entering at round 0 is how standard BLAKE2b's rounds 10 and 11 reuse rows 0 and 1. This mirrors what avx2.rs already does. The schedule loads and the modulo disappear from the emitted code, message words stay in registers across rounds, and the loop body drops from 157 to about 120 instructions per round at the cost of a larger function. compress becomes const-eligible as a result. The dispatcher then also looks const-eligible on targets where the cfgs collapse to the portable call alone, but it cannot be, so that lint is allowed with an explanation.
The compression function had no unit tests; it was only covered indirectly through the execution-spec state tests. Add a differential test against a reference implementation written with a runtime round index, over round counts 0..=40 plus larger values, with pseudorandom state, message, counter and final-block inputs. The round counts cover an empty run, a partial tile, the wraparound at ten and a full second tile, which is where an unrolled schedule can go wrong. This calls portable::compress directly rather than the dispatcher, so the portable path is exercised on targets that would otherwise select AVX2. Also add EIP-152 test vector 4 as an oracle independent of both implementations, checked against the portable path and the dispatcher.
cff032d to
548a3e2
Compare
|
Nice! On my run's I am getting around 10%, but this is good. Will refactor the code slightly but idea lgtm |
Fold the ten copies of the countdown guard and round call into a local rounds! macro; the expansion is identical to the longhand version. Also make compress_portable const now that portable::compress is const, so clippy's missing_const_for_fn stays clean after merging with main.
…luealloy#3784 bluealloy#3823 bluealloy#3840 (#57) Co-authored-by: Saylor Mike <[email protected]> Co-authored-by: Matthias Seitz <[email protected]> Co-authored-by: alpharush <[email protected]> Co-authored-by: a <[email protected]> Co-authored-by: rakita <[email protected]>
Closes #3837
portable::roundtook the round index as a runtimeusize, soSIGMA[r % 10]was resolved at runtime. It now takes the index as aconstgeneric, andcompressunrolls ten physical rounds with aremainingcounter for EIP-152's variable round count.. the structureavx2.rsalready uses. SIGMA has spec period 10 (RFC 7693 §2.7), so ten rounds tile any count, and re-entering the loop at round 0 is how standard BLAKE2b's rounds 10 and 11 reuse rows 0 and 1.(No public API change.)
In the emitted code (aarch64, same function, same flags): the 16
ldrbschedule loads per round and theumulh/maddmodulo are gone entirely, message loads drop from 16 per round to ~6 across all ten, and the loop goes from 157 to ~120 instructions per round. The dependentldrb → index → ldrchain disappears because constant indices let the compiler keep most message words in registers. Cost is a ~5x larger function (252 → 1309 instructions), worth noting for icache pressure.In wall time native aarch64 runner (
ubuntu-24.04-arm), existingbench/blake2.rsunchanged, baseline onmainand comparison on this branch in the same job on the same VM:All p < 0.05.
Two things stand out. The gain scales with round count and plateaus near −26%: at 2–12 rounds the fixed costs (213-byte input parse, plus the
crypto()->dyn Crypto::blake2_compressvtable hop that cannot be inlined intorun) dilute it; from 512 rounds up the loop dominates and the measurement converges on the loop's own improvement. And that asymptote agrees with the static instruction count (−23.6% per round), which owes nothing to the machine that ran it.. the measurement landing slightly ahead is what you would expect when the removed instructions are serialized dependent loads rather than average ones.Tests
The compression function had no unit tests.. it was covered only indirectly through the execution-spec state tests. This PR adds:
index, over round counts
0..=40plus 100/101/109/110/111/1000/4096, 64 pseudorandom(h, m, t, f)inputs each. Those counts cover an empty run, a partial tile, the wraparound atten, and a full second tile. It calls
portable::compressdirectly, so the portable path isexercised even on targets that would otherwise select AVX2.
portable path and the dispatcher.
Both were mutation-checked: mis-numbering one unrolled round (
round::<9>→round::<8>) fails the differential test at exactlyrounds = 10and fails the EIP-152 vector too.