-
Notifications
You must be signed in to change notification settings - Fork 117
perf(levm): reuse stack pool #5179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ use ethrex_common::{ | |||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
| use ethrex_levm::EVMConfig; | ||||||||||||||||||||||||||||||||
| use ethrex_levm::call_frame::Stack; | ||||||||||||||||||||||||||||||||
| use ethrex_levm::constants::{SYS_CALL_GAS_LIMIT, TX_BASE_COST}; | ||||||||||||||||||||||||||||||||
| use ethrex_levm::db::gen_db::GeneralizedDatabase; | ||||||||||||||||||||||||||||||||
| use ethrex_levm::errors::{InternalError, TxValidationError}; | ||||||||||||||||||||||||||||||||
|
|
@@ -99,6 +100,8 @@ impl LEVM { | |||||||||||||||||||||||||||||||
| ) -> Result<BlockExecutionResult, EvmError> { | ||||||||||||||||||||||||||||||||
| Self::prepare_block(block, db, vm_type)?; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| let mut shared_stack_pool = Vec::with_capacity(1024); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| let mut receipts = Vec::new(); | ||||||||||||||||||||||||||||||||
| let mut cumulative_gas_used = 0; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -113,7 +116,14 @@ impl LEVM { | |||||||||||||||||||||||||||||||
| ))); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| let report = Self::execute_tx(tx, tx_sender, &block.header, db, vm_type)?; | ||||||||||||||||||||||||||||||||
| let report = Self::execute_tx_in_block( | ||||||||||||||||||||||||||||||||
| tx, | ||||||||||||||||||||||||||||||||
| tx_sender, | ||||||||||||||||||||||||||||||||
| &block.header, | ||||||||||||||||||||||||||||||||
| db, | ||||||||||||||||||||||||||||||||
| vm_type, | ||||||||||||||||||||||||||||||||
| &mut shared_stack_pool, | ||||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||||
| LEVM::send_state_transitions_tx(&merkleizer, db, queue_length)?; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| cumulative_gas_used += report.gas_used; | ||||||||||||||||||||||||||||||||
|
|
@@ -231,6 +241,26 @@ impl LEVM { | |||||||||||||||||||||||||||||||
| vm.execute().map_err(VMError::into) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| pub fn execute_tx_in_block( | ||||||||||||||||||||||||||||||||
| // The transaction to execute. | ||||||||||||||||||||||||||||||||
| tx: &Transaction, | ||||||||||||||||||||||||||||||||
| // The transactions recovered address | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| tx_sender: Address, | ||||||||||||||||||||||||||||||||
| // The block header for the current block. | ||||||||||||||||||||||||||||||||
| block_header: &BlockHeader, | ||||||||||||||||||||||||||||||||
| db: &mut GeneralizedDatabase, | ||||||||||||||||||||||||||||||||
| vm_type: VMType, | ||||||||||||||||||||||||||||||||
| stack_pool: &mut Vec<Stack>, | ||||||||||||||||||||||||||||||||
| ) -> Result<ExecutionReport, EvmError> { | ||||||||||||||||||||||||||||||||
|
Comment on lines
+244
to
+254
|
||||||||||||||||||||||||||||||||
| let env = Self::setup_env(tx, tx_sender, block_header, db, vm_type)?; | ||||||||||||||||||||||||||||||||
| let mut vm = VM::new(env, db, tx, LevmCallTracer::disabled(), vm_type)?; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| std::mem::swap(&mut vm.stack_pool, stack_pool); | ||||||||||||||||||||||||||||||||
| let result = vm.execute().map_err(VMError::into); | ||||||||||||||||||||||||||||||||
| std::mem::swap(&mut vm.stack_pool, stack_pool); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+258
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to use
Comment on lines
+258
to
+260
|
||||||||||||||||||||||||||||||||
| std::mem::swap(&mut vm.stack_pool, stack_pool); | |
| let result = vm.execute().map_err(VMError::into); | |
| std::mem::swap(&mut vm.stack_pool, stack_pool); | |
| struct StackPoolGuard<'a> { | |
| a: &'a mut Vec<Stack>, | |
| b: &'a mut Vec<Stack>, | |
| } | |
| impl<'a> Drop for StackPoolGuard<'a> { | |
| fn drop(&mut self) { | |
| std::mem::swap(self.a, self.b); | |
| } | |
| } | |
| std::mem::swap(&mut vm.stack_pool, stack_pool); | |
| let _guard = StackPoolGuard { a: &mut vm.stack_pool, b: stack_pool }; | |
| let result = vm.execute().map_err(VMError::into); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number
1024for the stack pool capacity is not explained. Consider either adding a comment explaining why this specific capacity was chosen, or extracting it as a named constant (e.g.,STACK_POOL_CAPACITY) to improve code readability and maintainability.