fix(context): persist resume_at in call_with_interpreter#85
Open
SipengXie2024 wants to merge 1 commit intoparadigmxyz:mainfrom
Open
fix(context): persist resume_at in call_with_interpreter#85SipengXie2024 wants to merge 1 commit intoparadigmxyz:mainfrom
SipengXie2024 wants to merge 1 commit intoparadigmxyz:mainfrom
Conversation
`call_with_interpreter` creates a temporary `EvmContext` from the interpreter on each invocation. When JIT code suspends at a CALL or CREATE, it writes the resume point to `ecx.resume_at`. However, this value was never written back to the interpreter's bytecode PC before `ecx` was dropped. On the next call, `ResumeAt::load(pc, code)` would see `pc < code.len()` (since the PC was never updated) and return 0, causing the JIT function to restart from the beginning instead of resuming at the correct point. This bug is invisible for simple contracts without nested calls, but causes catastrophic performance degradation for real-world contracts with multiple CALL/CREATE operations (e.g., Uniswap swaps). In our benchmarks, fixing this improved JIT performance from ~921µs to ~29µs for a Uniswap V2 swap (a ~31x speedup). The fix saves `ecx.resume_at` before the borrow ends, then persists it via `interpreter.bytecode.absolute_jump(resume_at)`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
call_with_interpretercreates a temporaryEvmContextfrom the interpreter on each invocation. When JIT code suspends at a CALL or CREATE, it writes the resume point toecx.resume_at. However, this value was never written back to the interpreter's bytecode PC beforeecxwas dropped.On the next call,
ResumeAt::load(pc, code)seespc < code.len()(since PC was never updated) and returns0, causing the JIT function to restart from the beginning instead of resuming at the correct point.Impact
bench.rsbenchmarks)Uniswap V2 swap benchmark results:
Fix
Save
ecx.resume_atbefore the borrow ends, then persist it viainterpreter.bytecode.absolute_jump(resume_at)(+8 lines).Root Cause Analysis
ecx.resume_atcall_with_interpreterreturns →ecxis dropped,resume_atis lostEvmContext::from_interpreter_with_stack→ResumeAt::load(pc, code)→pc < code.len()→ returns0Note:
call()(low-level API used inbench.rs) is not affected because the caller managesEvmContextlifetime andecx.resume_atpersists across calls.