Skip to content
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

Feat: optimizations with smallvec #65

Merged
merged 2 commits into from
Oct 15, 2024
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ethereum = { version = "0.15", default-features = false }
log = { version = "0.4", default-features = false }
primitive-types = { workspace = true, features = ["rlp"] }
rlp = { version = "0.5", default-features = false }
smallvec = "1.13"

# Optional dependencies
environmental = { version = "1.1.2", default-features = false, optional = true }
Expand Down
33 changes: 17 additions & 16 deletions src/executor/stack/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use evm_core::{ExitFatal, InterpreterHandler, Machine, Trap};
use evm_runtime::Resolve;
use primitive_types::{H160, H256, U256};
use sha3::{Digest, Keccak256};
use smallvec::{smallvec, SmallVec};

macro_rules! emit_exit {
($reason:expr) => {{
Expand Down Expand Up @@ -377,19 +378,19 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>

/// Execute the runtime until it returns.
pub fn execute(&mut self, runtime: &mut Runtime) -> ExitReason {
let mut call_stack = Vec::with_capacity(DEFAULT_CALL_STACK_CAPACITY);
call_stack.push(TaggedRuntime {
kind: RuntimeKind::Execute,
inner: MaybeBorrowed::Borrowed(runtime),
});
let mut call_stack: SmallVec<[TaggedRuntime; DEFAULT_CALL_STACK_CAPACITY]> =
smallvec!(TaggedRuntime {
kind: RuntimeKind::Execute,
inner: MaybeBorrowed::Borrowed(runtime),
});
let (reason, _, _) = self.execute_with_call_stack(&mut call_stack);
reason
}

/// Execute using Runtimes on the `call_stack` until it returns.
fn execute_with_call_stack(
&mut self,
call_stack: &mut Vec<TaggedRuntime<'_>>,
call_stack: &mut SmallVec<[TaggedRuntime<'_>; DEFAULT_CALL_STACK_CAPACITY]>,
) -> (ExitReason, Option<H160>, Vec<u8>) {
// This `interrupt_runtime` is used to pass the runtime obtained from the
// `Capture::Trap` branch in the match below back to the top of the call stack.
Expand Down Expand Up @@ -544,8 +545,8 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
) {
Capture::Exit((s, _, v)) => emit_exit!(s, v),
Capture::Trap(rt) => {
let mut cs = Vec::with_capacity(DEFAULT_CALL_STACK_CAPACITY);
cs.push(rt.0);
let mut cs: SmallVec<[TaggedRuntime<'_>; DEFAULT_CALL_STACK_CAPACITY]> =
smallvec!(rt.0);
let (s, _, v) = self.execute_with_call_stack(&mut cs);
emit_exit!(s, v)
}
Expand Down Expand Up @@ -590,8 +591,8 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
) {
Capture::Exit((s, _, v)) => emit_exit!(s, v),
Capture::Trap(rt) => {
let mut cs = Vec::with_capacity(DEFAULT_CALL_STACK_CAPACITY);
cs.push(rt.0);
let mut cs: SmallVec<[TaggedRuntime<'_>; DEFAULT_CALL_STACK_CAPACITY]> =
smallvec!(rt.0);
let (s, _, v) = self.execute_with_call_stack(&mut cs);
emit_exit!(s, v)
}
Expand Down Expand Up @@ -651,8 +652,8 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
) {
Capture::Exit((s, _, v)) => emit_exit!(s, v),
Capture::Trap(rt) => {
let mut cs = Vec::with_capacity(DEFAULT_CALL_STACK_CAPACITY);
cs.push(rt.0);
let mut cs: SmallVec<[TaggedRuntime<'_>; DEFAULT_CALL_STACK_CAPACITY]> =
smallvec!(rt.0);
let (s, _, v) = self.execute_with_call_stack(&mut cs);
emit_exit!(s, v)
}
Expand Down Expand Up @@ -722,8 +723,8 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
) {
Capture::Exit((s, v)) => emit_exit!(s, v),
Capture::Trap(rt) => {
let mut cs = Vec::with_capacity(DEFAULT_CALL_STACK_CAPACITY);
cs.push(rt.0);
let mut cs: SmallVec<[TaggedRuntime<'_>; DEFAULT_CALL_STACK_CAPACITY]> =
smallvec!(rt.0);
let (s, _, v) = self.execute_with_call_stack(&mut cs);
emit_exit!(s, v)
}
Expand Down Expand Up @@ -1640,8 +1641,8 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
// not allow it. For now we'll make a recursive call instead of making a breaking
// change to the precompile API. But this means a custom precompile could still
// potentially cause a stack overflow if you're not careful.
let mut call_stack = Vec::with_capacity(DEFAULT_CALL_STACK_CAPACITY);
call_stack.push(rt.0);
let mut call_stack: SmallVec<[TaggedRuntime; DEFAULT_CALL_STACK_CAPACITY]> =
smallvec!(rt.0);
let (reason, _, return_data) =
self.executor.execute_with_call_stack(&mut call_stack);
emit_exit!(reason, return_data)
Expand Down
Loading