Skip to content

perf(precompile): reduce bounds checks in precompile parsing - #3784

Merged
rakita merged 4 commits into
bluealloy:mainfrom
0xalpharush:perf/bounds-check-optimizations
Jul 6, 2026
Merged

rakita merged 4 commits into
bluealloy:mainfrom
0xalpharush:perf/bounds-check-optimizations

Conversation

@0xalpharush

@0xalpharush 0xalpharush commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR applies a small set of assembly-checked precompile parsing optimizations. The current stack is limited to exact chunk/split handling for BLS/BN254 pair-like inputs and fixed-size byte readers in the BN254 arkworks backend.

The earlier BLS G2 padding read rewrite was removed after review; remove_g2_padding now keeps the previous loop implementation.

Commits

  • perf(precompile): chunk BLS pairing inputs

    • Replaces manual i * PAIRING_INPUT_LENGTH range construction with chunks_exact(PAIRING_INPUT_LENGTH) plus split_at.
    • Keeps the existing input-length validation while giving LLVM a clearer fixed-width chunk shape and removing redundant slice-index failure paths in the pairing parser.
  • perf(precompile): chunk BLS MSM inputs

    • Updates BLS12-381 G1/G2 MSM parsing to iterate over exact input chunks and split each chunk into point/scalar components.
    • Avoids recomputing byte offsets for each pair and removes the MSM iterator closure slice-index failure paths.
  • perf(precompile): chunk BN254 pair inputs

    • Replaces manual BN254 pair element offset arithmetic with chunks_exact(PAIR_ELEMENT_LEN) and split_at(G1_LEN).
    • Keeps the existing multiple-of-length validation and removes redundant pair parsing bounds paths.
  • perf(precompile): type BN254 reader slices

    • Changes BN254 Fq reads to accept fixed-size array references after the surrounding point/scalar readers establish the relevant length.
    • Reduces repeated length proof work in the arkworks reader path while preserving field-membership errors as PrecompileHalt.
    • Uses expect for documented precondition panics so the panic reason mirrors the function contracts.

Validation

  • cargo fmt --all --check
  • git diff --check
  • cargo test -p revm-precompile --lib
  • cargo clippy -p revm-precompile --all-targets --all-features
  • typos crates/precompile/src

@0xalpharush

0xalpharush commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor Author

cargo-asm before/after

Measured locally with cargo-show-asm on AArch64. Baseline was local main at 392ddb77; after was this PR branch at e22e5502.

Update after review: the earlier BLS G2 padding read rewrite was dropped, so these numbers reflect the remaining four commits only.

Commands used:

  • cargo asm -p revm-precompile --lib pairing --asm --no-color
  • cargo asm -p revm-precompile --lib bn254 --asm --no-color
  • cargo asm -p revm-precompile --lib bls12_381::g1_msm --asm --no-color
  • cargo asm -p revm-precompile --lib bls12_381::g2_msm --asm --no-color
  • cargo asm -p revm-precompile --lib "Iterator>::next" --asm --no-color
  • cargo asm -p revm-precompile --lib --everything --asm --no-color | awk '/^([^[:space:]L.]|<).*:$/ { fn=$0 } /slice_index_fail|panic_bounds_check|slice_index_order_fail/ { print fn; print NR ":" $0 }'

Instruction counts below are the bracketed counts reported by cargo-show-asm's symbol selection output.

Symbol / path Before After Delta
revm_precompile::bls12_381::pairing::pairing 480 415 -65
revm_precompile::bn254::run_pair 313 278 -35
revm_precompile::bn254::arkworks::read_g1_point 409 392 -17
revm_precompile::interface::Crypto::bn254_g1_mul 1423 1405 -18
revm_precompile::interface::Crypto::bn254_pairing_check 1541 1510 -31
revm_precompile::bls12_381::g1_msm::g1_msm 269 272 +3
revm_precompile::bls12_381::g2_msm::g2_msm 311 307 -4
MSM Map<I, F>::next closure symbols 239, 196 242, 193 net 0
MSM public wrappers + map closures 1015 1014 -1

Bounds/panic-path scan for touched areas:

Path Before After
MSM map closure slice_index_fail calls 4 0
BLS pairing slice_index_fail calls 2 0
BN254 run_pair slice_index_fail calls 2 0
BN254 read_g1_point slice_index_fail calls 2 1
BN254 bn254_pairing_check slice_index_fail calls 2 1

The BLS MSM individual symbol counts are mixed, but the exact chunk iteration removes the four emitted MSM map-closure slice_index_fail paths and leaves the combined public-wrapper + closure count slightly smaller.

The bn254_g1_mul after count includes the follow-up switch from unwrap to expect for documented precondition panics. That changed the reported total symbol count by one instruction in the cold panic path while keeping the successful conversion path equivalent.

@0xalpharush
0xalpharush force-pushed the perf/bounds-check-optimizations branch from 33051e1 to 6b4701f Compare June 30, 2026 17:35
@codspeed

codspeed Bot commented Jul 1, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing 0xalpharush:perf/bounds-check-optimizations (e22e550) with main (0602152)

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

@rakita

rakita commented Jul 1, 2026

Copy link
Copy Markdown
Member

@0xalpharush this all looks good, should I merge?

Comment on lines +39 to +43
let x_0 = remove_fp_padding(&input[..PADDED_FP_LENGTH])?;
let x_1 = remove_fp_padding(&input[PADDED_FP_LENGTH..2 * PADDED_FP_LENGTH])?;
let y_0 = remove_fp_padding(&input[2 * PADDED_FP_LENGTH..3 * PADDED_FP_LENGTH])?;
let y_1 = remove_fp_padding(&input[3 * PADDED_FP_LENGTH..])?;
Ok([x_0, x_1, y_0, y_1])

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.

Previous is probably better as the loop would unwind.

@0xalpharush 0xalpharush Jul 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this change by dropping the perf(precompile): simplify BLS G2 padding reads commit. remove_g2_padding now keeps the previous loop implementation, and the PR description/cargo-asm note have been refreshed for the remaining four commits.

@0xalpharush
0xalpharush force-pushed the perf/bounds-check-optimizations branch from 6b4701f to e22e550 Compare July 1, 2026 12:49
@0xalpharush
0xalpharush marked this pull request as ready for review July 1, 2026 14:21
@rakita
rakita merged commit deca1e6 into bluealloy:main Jul 6, 2026
32 of 34 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 1, 2026
This was referenced Jul 22, 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
43 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.

2 participants