Repository navigation
perf(levm): unchecked memory hot path - #7119
Conversation
Squashed from the zkevm autoresearch branch perf/levm-memory-hotpath (6 commits), deltas measured on the zisk guest benchmark: - MSTORE rounds the new memory size once (real blocks -0.03..-0.05%) - u256_to_usize: OR the high limbs into one branch (ecrecover -0.02%) - MLOAD prerounded resize (stress_memory -1.74%) - MLOAD set_top_unchecked (stress_memory -0.61%) - unchecked buffer access, skipping the RefCell borrow flag (stress_memory -5.55%, total -2.17%) - same on resize/MCOPY/store_zeros (total -0.10%) Fixups on top of the source branch: rustfmt, drop the unnecessary `mut` on the `buf_mut()` bindings, keep the `unsafe_code` expectation on the `set_top_unchecked` declaration, and restore `Memory::len`'s doc comment and `#[inline]` (the helpers had absorbed them).
|
🤖 Kimi Code ReviewThis PR introduces well-justified performance optimizations for hot EVM paths (MLOAD/MSTORE). The unsafe code is appropriately documented, but there are soundness and correctness issues to address. Critical Issues1. Integer overflow in
|
🤖 Codex Code ReviewFindings
Notes The I did not run the full test suite. Automated review by OpenAI Codex · gpt-5.4 · custom prompt |
Greptile SummaryThe PR optimizes LEVM’s MLOAD and MSTORE hot paths by reusing pre-rounded memory sizes and eliminating redundant stack, buffer, and conversion checks.
Confidence Score: 5/5The PR appears safe to merge; no concrete correctness, memory-safety, or gas-accounting failure remains on the changed paths. The new unchecked accesses are preceded by equivalent bounds and stack validation, the pre-rounded size covers each 32-byte operation with checked arithmetic, and inspected buffer users do not overlap shared and mutable accesses.
|
| Filename | Overview |
|---|---|
| crates/vm/levm/src/memory.rs | Adds unchecked RefCell-backed buffer access and pre-rounded word operations; inspected callers and arithmetic preserve the required ownership and bounds invariants. |
| crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs | Reuses the checked rounded memory size for MLOAD/MSTORE gas and access while preserving opcode semantics. |
| crates/vm/levm/src/call_frame.rs | Adds an unchecked stack-top setter whose MLOAD caller validates the top and performs no intervening depth mutation. |
| crates/vm/levm/src/utils.rs | Rewrites the oversized-U256 check as a high-limb OR without changing accepted values. |
Reviews (1): Last reviewed commit: "perf(levm): unchecked memory hot path" | Re-trigger Greptile
Lines of code reportTotal lines added: Detailed view |
🤖 Claude Code ReviewReview:
|
Benchmark Results ComparisonNo significant difference was registered for any benchmark run. Detailed ResultsBenchmark Results: BubbleSort
Benchmark Results: ERC20Approval
Benchmark Results: ERC20Mint
Benchmark Results: ERC20Transfer
Benchmark Results: Factorial
Benchmark Results: FactorialRecursive
Benchmark Results: Fibonacci
Benchmark Results: FibonacciRecursive
Benchmark Results: ManyHashes
Benchmark Results: MstoreBench
Benchmark Results: Push
Benchmark Results: SstoreBench_no_opt
|
Benchmark Block Execution Results Comparison Against Main
|
# Conflicts: # CHANGELOG.md
Motivation
Currently some of the bounds checks done could be elided, since we have guarantees the compiler is unaware of.
Description
Uses unchecked memory accesses to reduce such checks. Gives a 1.58% reduction of ZisK AIR cycles averaged across our test set, with the biggest improvement being on stress_memory_150M (-7.87%). No performance regressions on any of the test cases.