Skip to content

Commit

Permalink
Never inline the prepare functions (bluealloy#712)
Browse files Browse the repository at this point in the history
These functions are used for keeping the stack memory clean on the
host recursive code paths. EVM supports up to 1024 levels of recursion,
so if one is not careful with the stack memory allocations, the stack
memory can blow up.

Benchmarks show dramatic stack memory improvements when ont inlining
these functions. A recursive bomb uses around 1.1MB of stack when
these functions are not inlined. If inlined the recursive bombs blow
up the stack.
  • Loading branch information
valo authored and Evalir committed Sep 14, 2023
1 parent 5fa030a commit ec1c5e9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
7 changes: 1 addition & 6 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,7 @@ pub fn run(
let console_bar = console_bar.clone();
let elapsed = elapsed.clone();

let mut thread = std::thread::Builder::new();

// Allow bigger stack in debug mode to prevent stack overflow errors
//if cfg!(debug_assertions) {
thread = thread.stack_size(4 * 1024 * 1024);
//}
let thread: std::thread::Builder = std::thread::Builder::new();

joins.push(
thread
Expand Down
2 changes: 2 additions & 0 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ pub fn selfdestruct<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Ho
interpreter.instruction_result = InstructionResult::SelfDestruct;
}

#[inline(never)]
pub fn prepare_create_inputs<const IS_CREATE2: bool, SPEC: Spec>(
interpreter: &mut Interpreter,
host: &mut dyn Host,
Expand Down Expand Up @@ -368,6 +369,7 @@ pub fn static_call<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Hos
call_inner::<SPEC>(interpreter, CallScheme::StaticCall, host);
}

#[inline(never)]
fn prepare_call_inputs<SPEC: Spec>(
interpreter: &mut Interpreter,
scheme: CallScheme,
Expand Down
2 changes: 2 additions & 0 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
(new_state, logs, gas_used, gas_refunded)
}

#[inline(never)]
fn prepare_create(&mut self, inputs: &CreateInputs) -> Result<PreparedCreate, CreateResult> {
let gas = Gas::new(inputs.gas_limit);

Expand Down Expand Up @@ -632,6 +633,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
}
}

#[inline(never)]
fn prepare_call(&mut self, inputs: &CallInputs) -> Result<PreparedCall, CallResult> {
let gas = Gas::new(inputs.gas_limit);
let account = match self
Expand Down

0 comments on commit ec1c5e9

Please sign in to comment.