Skip to content

perf(levm): use a stack pool #3386

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

Merged
merged 11 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Perf

### 2025-06-30

- Use a stack pool [#3386](https://github.com/lambdaclass/ethrex/pull/3386)

### 2025-06-27

- Reduce handle_debug runtime cost [#3356](https://github.com/lambdaclass/ethrex/pull/3356)
Expand Down
6 changes: 6 additions & 0 deletions crates/vm/levm/src/call_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl Stack {
self.values.swap(self.offset, index);
Ok(())
}

pub fn clear(&mut self) {
self.offset = STACK_LIMIT;
}
}

impl Default for Stack {
Expand Down Expand Up @@ -231,6 +235,7 @@ impl CallFrame {
is_create: bool,
ret_offset: U256,
ret_size: usize,
stack: Stack,
) -> Self {
let invalid_jump_destinations =
get_invalid_jump_destinations(&bytecode).unwrap_or_default();
Expand All @@ -250,6 +255,7 @@ impl CallFrame {
is_create,
ret_offset,
ret_size,
stack,
..Default::default()
}
}
Expand Down
18 changes: 18 additions & 0 deletions crates/vm/levm/src/opcode_handlers/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ impl<'a> VM<'a> {
return Ok(OpcodeResult::Continue { pc_increment: 1 });
}

let mut stack = self.stack_pool.pop().unwrap_or_default();
stack.clear();

let new_call_frame = CallFrame::new(
deployer,
new_address,
Expand All @@ -698,6 +701,7 @@ impl<'a> VM<'a> {
true,
U256::zero(),
0,
stack,
);
self.call_frames.push(new_call_frame);

Expand Down Expand Up @@ -752,6 +756,9 @@ impl<'a> VM<'a> {
return Ok(OpcodeResult::Continue { pc_increment: 1 });
}

let mut stack = self.stack_pool.pop().unwrap_or_default();
stack.clear();

let new_call_frame = CallFrame::new(
msg_sender,
to,
Expand All @@ -766,6 +773,7 @@ impl<'a> VM<'a> {
false,
ret_offset,
ret_size,
stack,
);
self.call_frames.push(new_call_frame);

Expand Down Expand Up @@ -859,6 +867,11 @@ impl<'a> VM<'a> {
};

self.tracer.exit_context(ctx_result, false)?;

let mut stack = executed_call_frame.stack;
stack.clear();
self.stack_pool.push(stack);

Ok(())
}

Expand Down Expand Up @@ -901,6 +914,11 @@ impl<'a> VM<'a> {
};

self.tracer.exit_context(ctx_result, false)?;

let mut stack = executed_call_frame.stack;
stack.clear();
self.stack_pool.push(stack);

Ok(())
}

Expand Down
6 changes: 5 additions & 1 deletion crates/vm/levm/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
TransientStorage,
call_frame::CallFrame,
call_frame::{CallFrame, Stack},
db::gen_db::GeneralizedDatabase,
debug::DebugMode,
environment::Environment,
Expand Down Expand Up @@ -59,6 +59,8 @@ pub struct VM<'a> {
pub tracer: LevmCallTracer,
/// Mode for printing some useful stuff, only used in development!
pub debug_mode: DebugMode,
/// A pool of stacks to avoid reallocating too much when creating new call frames.
pub stack_pool: Vec<Stack>,
pub vm_type: VMType,
}

Expand All @@ -83,6 +85,7 @@ impl<'a> VM<'a> {
storage_original_values: HashMap::new(),
tracer,
debug_mode: DebugMode::disabled(),
stack_pool: Vec::new(),
vm_type,
}
}
Expand Down Expand Up @@ -111,6 +114,7 @@ impl<'a> VM<'a> {
is_create,
U256::zero(),
0,
Stack::default(),
);

self.call_frames.push(initial_call_frame);
Expand Down
Loading