Skip to content

perf(precompile): unroll blake2 portable rounds to const-fold the message schedule - #3840

Merged
rakita merged 4 commits into
bluealloy:mainfrom
4waan:perf/blake2-portable-const-schedule
Aug 14, 2026
Merged

rakita merged 4 commits into
bluealloy:mainfrom
4waan:perf/blake2-portable-const-schedule

Conversation

@4waan

@4waan 4waan commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3837

portable::round took the round index as a runtime usize, so SIGMA[r % 10] was resolved at runtime. It now takes the index as a const generic, and compress unrolls ten physical rounds with a remaining counter for EIP-152's variable round count.. the structure avx2.rs already 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 ldrb schedule loads per round and the umulh/madd modulo 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 dependent ldrb → index → ldr chain 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), existing bench/blake2.rs unchanged, baseline on main and comparison on this branch in the same job on the same VM:

benchmark main this PR change 95% CI
2 rounds 57.21 ns 49.63 ns −13.26% [−13.28%, −13.24%]
4 rounds 84.35 ns 72.50 ns −14.05% [−14.16%, −13.99%]
10 rounds 174.39 ns 139.34 ns −20.10% [−20.15%, −20.07%]
12 rounds 204.00 ns 160.39 ns −21.38% [−21.38%, −21.37%]
64 rounds 985.36 ns 736.77 ns −25.23% [−25.28%, −25.18%]
512 rounds 7.7102 µs 5.7014 µs −26.05% [−26.07%, −26.03%]
1024 rounds 15.3916 µs 11.3850 µs −26.03% [−26.04%, −26.02%]
100K rounds 1.5000 ms 1.1034 ms −26.44% [−26.48%, −26.39%]
200K rounds 2.9999 ms 2.2071 ms −26.43% [−26.45%, −26.40%]

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_compress vtable hop that cannot be inlined into run) 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:

  • a differential test against a reference implementation using the previous runtime round
    index, over round counts 0..=40 plus 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 at
    ten, and a full second tile. It calls portable::compress directly, so the portable path is
    exercised even on targets that would otherwise select AVX2.
  • EIP-152 vector 4 as an oracle independent of both implementations, checked against the
    portable path and the dispatcher.

Both were mutation-checked: mis-numbering one unrolled round (round::<9> → round::<8>) fails the differential test at exactly rounds = 10 and fails the EIP-152 vector too.

@codspeed

codspeed Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 20.47%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
❌ 2 regressed benchmarks
✅ 190 untouched benchmarks
⏩ 1 skipped benchmark1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

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)

Open in CodSpeed

Footnotes

  1. 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. ↩

@4waan

4waan commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

On CodSpeed:
bench.yml runs ubuntu-latest (x86_64), where compress dispatches to AVX2, so the code this PR changes is compiled but never executed.

also a note on a clippy cascade:

Unrolling makes portable::compress const-eligible, so it is now const fn. That in turn makes the blake2::compress dispatcher look const-eligible on targets where the cfgs collapse to the portable call alone. It cannot be on x86 it does runtime AVX2 detection and calls an unsafe implementation so clippy::missing_const_for_fn is allowed there with an explanation. Neither lint fires on main.

4waan added 2 commits August 7, 2026 19:15
…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.
@4waan
4waan force-pushed the perf/blake2-portable-const-schedule branch from cff032d to 548a3e2 Compare August 7, 2026 14:10
@rakita

rakita commented Aug 14, 2026

Copy link
Copy Markdown
Member

Nice! On my run's I am getting around 10%, but this is good. Will refactor the code slightly but idea lgtm

rakita added 2 commits August 14, 2026 17:39
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.
@rakita
rakita merged commit 730fe8f into bluealloy:main Aug 14, 2026
33 of 34 checks passed
This was referenced Aug 14, 2026
RealiCZ added a commit to megaeth-labs/revm that referenced this pull request Sep 18, 2026
…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]>
@RealiCZ RealiCZ mentioned this pull request Sep 18, 2026
30 tasks done
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.

perf(precompile): blake2 portable path loses upstream's const-folded message schedule

2 participants