Skip to content

chore: cherry-pick upstream perf fixes #3539 #3775 #3784 #3823 #3840 - #57

Merged
RealiCZ merged 5 commits into
mainfrom
cz/chore/upstream-perf-batch
Sep 18, 2026
Merged

RealiCZ merged 5 commits into
mainfrom
cz/chore/upstream-perf-batch

Conversation

@RealiCZ

@RealiCZ RealiCZ commented Sep 18, 2026

Copy link
Copy Markdown

Summary

Cherry-pick five upstream bluealloy/revm performance improvements whose results are unchanged. The fork has no benchmark job; the consumer's instruction-count CI measures the effect.

Suggested title: chore: cherry-pick upstream perf fixes #3539 #3775 #3784 #3823 #3840

Excluded

#3732 (perf: avoid reallocations and cloning during commit) is excluded as API-breaking: CacheAccount::newly_created and CacheAccount::change go from 2 parameters to 1 (semver major on revm-database), and OnStateHook::on_state changes from &EvmState to owned EvmState. The node's reth implements OnStateHook, so this breaks a consumer under the patch. Candidate for the next base-line move.

Per pick

bluealloy#3539 fe966f4d6421e26af151268526521dc98c4e991a — perf(database): avoid double cloning in take_n_reverts

  • Speeds up: BundleState::take_n_reverts — split_off + mem::replace instead of split_at plus two to_vec.
  • Upstream numbers (PR body): none. Body: “Replace split_at plus double to_vec with move-based split_off.” CodSpeed: “will not alter performance” (177 untouched).
  • Fork paths: crates/database/src/states/bundle_state.rs.
  • Fork hook next to it: no.
  • patch-id: equal. -x trailer present.

bluealloy#3775 ae95b77643504500cc11bfa47bfe6b4ec8e3d768 — perf: avoid intermediate BAL account allocation

  • Speeds up: Bal::try_from_alloy / clone_from_alloy — preallocated AddressIndexMap instead of collect::<Vec<_>>() then from_iter.
  • Upstream numbers (PR body): none. Body: “This avoids an extra allocation during fallible BAL conversion while preserving existing duplicate-account behavior through IndexMap::insert.” CodSpeed: “will not alter performance” (181 untouched).
  • Fork paths: crates/state/src/bal/alloy.rs.
  • Fork hook next to it: no.
  • Conflict (recorded in the commit body): clone_from_alloy still takes &AlloyBal here (upstream #3762 changed it to &[AlloyAccountChanges] and was not taken). The same insert loop was applied to the fork signature. try_from_alloy applied cleanly. git patch-id --stable still matches.
  • patch-id: equal. -x trailer present.

bluealloy#3784 deca1e64f2999d1ab095376a917c1138354aac05 — perf(precompile): reduce bounds checks in precompile parsing

  • Speeds up: BLS12-381 pairing/MSM and BN254 pair parsing (chunks_exact + split_at); BN254 arkworks readers take fixed-size arrays.
  • Upstream numbers (PR body): none in the body. The body describes the four parsing commits and cargo test -p revm-precompile --lib. (A review comment, not the body, posted cargo-asm deltas: pairing 480→415, run_pair 313→278, and removal of slice_index_fail on those parsers.)
  • Fork paths: crates/precompile/src/bls12_381/{pairing,g1_msm,g2_msm}.rs, crates/precompile/src/bn254.rs, crates/precompile/src/bn254/arkworks.rs.
  • Fork hook next to it: no.
  • patch-id: equal. -x trailer present.

bluealloy#3823 3cd8b85321cf99aba6c66c9a335266624bd43700 — perf(precompile): use fused multi-miller loop for BLS12-381 pairing

  • Speeds up: blst pairing_check — one blst_miller_loop_n instead of per-pair miller + fp12 mul. Infinity pairs are skipped before the fused loop (KZG constant-polynomial proofs).

  • Upstream numbers (PR body): “Bit-identical output … Benchmark via the existing eip2537 pairing bench, end-to-end through the precompile:”

    pairs change
    1 ~0% (nothing to fuse)
    2 ~6%
    8 ~21%
    16 ~30%
  • Fork paths: crates/precompile/src/bls12_381/blst.rs, crates/precompile/src/kzg_point_evaluation.rs, crates/precompile/src/kzg_point_evaluation/blst.rs.

  • Fork hook next to it: no.

  • patch-id: equal. -x trailer present.

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

  • Speeds up: portable BLAKE2b compression (EIP-152). Round index is a const generic; ten physical rounds tile any count. Loop body 157→120 instructions/round on aarch64.

  • Upstream numbers (PR body): native aarch64, existing bench/blake2.rs:

    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%]
  • Fork paths: crates/precompile/src/blake2/{mod,portable}.rs.

  • Fork hook next to it: no.

  • Conflict (recorded in the commit body): did not take bench-only #3848 (compress_portable and the portable blake2 benches). The unroll, the clippy allow on compress, and the compression tests applied. compress_portable is omitted; this fork has no bench job for it. patch-id differs for that reason.

  • patch-id: differs (recorded). -x trailer present.

API exceptions

none. Public API stays a superset of the PR base. Labels: mega:cherry-pick, api:superset.

Test plan

Results unchanged. Path coverage and what ran:

Pick Covers the changed path Ran
bluealloy#3539 bundle_state::tests::take_reverts cargo nextest run --locked -p revm-database
bluealloy#3775 revm-state bal::tests::{try_from_alloy_*, clone_from_alloy_*} -p revm-state
bluealloy#3784 revm-precompile bn254::tests::test_bn254_pair, BLS MSM unit tests, pairing input-length errors -p revm-precompile
bluealloy#3823 kzg_point_evaluation::tests::{test_constant_polynomial_valid_proof, test_infinity_proof_invalid, test_valid_input, basic_test} plus the crate's pairing path full -p revm-precompile
bluealloy#3840 blake2::tests::{portable_matches_runtime_index_reference, eip152_vector_4} (portable path, not only the AVX2 dispatcher) full -p revm-precompile

Also: workspace nextest (--locked, --all-features, --no-default-features), clippy, fmt, riscv32/64 no_std check, docs, doctest. revm-ee-tests ran with the targeted nextest (no precompile-specific fixtures there).

scripts/run-tests.sh wires the full EEST suite (develop state tests, glamsterdam-devnet state tests, legacy Cancun/Constantinople, blockchain tests). There is no precompile-only filter. Fixtures were not in the worktree; the suite was not downloaded or run here. CI ethereum-tests is the gated EEST job.

Labels checklist

saylor-mik87786 and others added 5 commits September 18, 2026 09:25
(cherry picked from commit ae95b77)

Conflict: `clone_from_alloy` still takes `&AlloyBal` in this fork (upstream
bluealloy#3762 changed it to `&[AlloyAccountChanges]` and was not taken). Applied the
same preallocated `AddressIndexMap` + insert loop to the fork signature.
`try_from_alloy` applied cleanly.
…oy#3784)

* perf(precompile): chunk BLS pairing inputs

* perf(precompile): chunk BLS MSM inputs

* perf(precompile): chunk BN254 pair inputs

* perf(precompile): type BN254 reader slices

(cherry picked from commit deca1e6)
…luealloy#3823)

* perf(precompile): use fused multi-miller loop for BLS12-381 pairing

Replace the per-pair miller loop and fp12 multiply accumulation in the blst `pairing_check` with a single `blst_miller_loop_n` call, sharing the per-bit fp12 squaring across all pairs. This mirrors the arkworks backend, which already fuses via `multi_pairing`.

Uses the raw FFI rather than blst's `miller_loop_n` Rust wrapper, which fans out to a thread pool (undesirable inside a precompile and unavailable on no_std targets), keeping the computation single-threaded and deterministic. The result is bit-identical; only faster.

Bench (eip2537, end-to-end precompile): ~6% at 2 pairs, ~21% at 8, ~30% at 16.

* fix(precompile): skip infinity pairs in blst KZG pairing check

blst_miller_loop_n does not special-case points at infinity, so filter
such pairs out before the fused pairing check (e.g. the proof of a
constant polynomial is the point at infinity). Add tests for the valid
constant-polynomial proof and the invalid mixed-infinity case, and
simplify pair collection with unzip/filter.

---------

Co-authored-by: rakita <[email protected]>
(cherry picked from commit 3cd8b85)
…sage schedule (bluealloy#3840)

* perf(precompile): unroll blake2 portable rounds to const-fold the schedule

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.

* test(precompile): add blake2 compression tests

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.

* perf(precompile): condense unrolled blake2 rounds with a macro

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.

---------

Co-authored-by: rakita <[email protected]>

(cherry picked from commit 730fe8f)

Conflict: did not take bench-only bluealloy#3848 (`compress_portable` and the portable
blake2 benches). The unroll, clippy allow on `compress`, and the compression
unit tests applied; `compress_portable` is omitted because this fork has no
bench job for it.
@RealiCZ RealiCZ added mega:cherry-pick Upstream commits brought in unchanged apart from recorded conflicts api:superset Public API stays a superset of the baseline labels Sep 18, 2026
@mega-maxwell

mega-maxwell Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Claude review status

Living comment — rewritten in place. The review workflow keeps this single comment up to date instead of posting a new one each round, so it always describes the latest reviewed commit and the earlier text is intentionally gone. No reply is needed here; reply to a finding in its own review thread, and answer an open question in a reply on this PR. The next review round reconciles your answer.

🛠️ Review did not finish

Attempted head 6c824f4b · updated 2026-09-18T01:40:45+00:00

This round did not publish: MODEL_ACTION_FAILED in phase review_retry. Anything listed below is from the last round that did. Re-run the workflow or push a new commit to try again.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-18T01:43:46.440284Z 6c824f4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@RealiCZ
RealiCZ merged commit 43485bf into main Sep 18, 2026
37 of 43 checks passed
@RealiCZ
RealiCZ deleted the cz/chore/upstream-perf-batch branch September 18, 2026 02:05
RealiCZ added a commit that referenced this pull request Sep 18, 2026
…backend (#66)

## Summary

Two rules for upstream picks, learned from the first batches (#54, #57)
and from how alloy-rs/evm2 backports revm precompile work:

- picks are batched by crate or subsystem, one pull request per batch,
so one reviewer can read a whole batch;
- a pick that touches a precompile is tested under mutually exclusive
feature sets (the `std`-only fallback run, the default set,
`--all-features`, and the single backend feature it changes), because
`--all-features` selects only the preferred backend in each `cfg_if!`
block.

`REVIEW.md`'s `mega:cherry-pick` row names both.

## API exceptions

None (documentation only).

## Test plan

The mutually exclusive feature sets were run on `main` at 43485bf for
the perf picks in #57: `--no-default-features --features std` (every
fallback) 57/57, `std,blst` 57/57, `std,c-kzg` 57/57, the default set
57/57, `--all-features` 57/57.

## Labels checklist

`mega:ci`, `api:superset`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api:superset Public API stays a superset of the baseline mega:cherry-pick Upstream commits brought in unchanged apart from recorded conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants