blockifier: avoid redundant memory reads in read_felt_array - #15018
blockifier: avoid redundant memory reads in read_felt_array#15018gkaempfer wants to merge 2 commits into
Conversation
read_felt_array's empty-span check already reads both the start and end pointer cells via get_maybe; the non-empty path then re-read the same two memory cells via get_relocatable. Reuse the values already fetched instead of doing the memory lookup twice, since this function runs on every array-typed syscall argument (calldata, event keys/data, etc.) across a block.
Rename the helper to relocatable_from_memory_value (expect_* implies panicking in Rust), move it below its public caller, and rephrase the comment to state why the second read is skipped. Add tests pinning the exact error variant and address for the missing-cell and wrong-type cases, since preserving get_relocatable's error semantics is the main correctness risk of this refactor.
PR SummaryLow Risk Overview A small helper, Tests now assert exact error type and address, and cover felt-typed end pointers and unwritten cells. Reviewed by Cursor Bugbot for commit ecdf1be. Bugbot is set up for automated code reviews on this repo. Configure here. |
|
Artifacts upload workflows: |
Summary
Follow-up perf optimization on #15008 ("blockifier: handle null empty spans"), which added an empty-span check to
read_felt_arrayincrates/blockifier/src/execution/syscalls/hint_processor.rs.That check reads the array's start and end pointer cells via
vm.get_maybe(...)to detect the null-empty-span case. In the non-empty (common) path, the function then re-read the same two memory cells viavm.get_relocatable(...)— a redundantMemory::get()lookup for addresses already fetched a few lines earlier.read_felt_arrayruns on every array-typed syscall argument (calldata,emit_eventkeys/data, etc.), i.e. roughly once per syscall across a block, so the extra lookup is pure overhead in a hot path.This PR reuses the already-fetched
Option<MaybeRelocatable>values instead of re-reading memory, via a small helper (relocatable_from_memory_value) that reproducesVirtualMachine::get_relocatable's exact error semantics (MemoryError::UnknownMemoryCellvsMemoryError::ExpectedRelocatable) so behavior is unchanged in every case — empty span (null or real), non-empty span, missing memory cell, wrong-type memory cell.Also strengthens test coverage: the existing "rejects mixed null/pointer span" test now asserts the exact error variant and address (previously just
is_err()), and two new tests cover the felt-typed-end-pointer and unwritten-cell error paths, since preservingget_relocatable's error semantics is the main correctness risk of this kind of refactor.Expected impact
Cuts the memory-lookup work of
read_felt_arrayroughly in half for the non-empty-array case (2 lookups instead of 4), on a function invoked for essentially every array-typed syscall argument in every transaction.Test plan
SEED=0 cargo test -p blockifier read_felt_array— 8 passed, 0 failedSEED=0 cargo test -p blockifier --lib— 985 passed, 0 failedcargo clippy -p blockifier --lib— cleanscripts/rust_fmt.sh— no changes neededMemory::get/get_relocatable) and style; findings applied.Generated by Claude Code