From e0649048042d8cd3264ba0ae58ce2403c2e96e29 Mon Sep 17 00:00:00 2001 From: Moritz Date: Wed, 13 Jul 2022 17:39:31 +0200 Subject: [PATCH] perf: store code in `Bytes` --- core/Cargo.toml | 1 + core/src/lib.rs | 10 +++------- runtime/Cargo.toml | 1 + runtime/src/lib.rs | 8 ++------ src/executor/stack/executor.rs | 9 ++------- 5 files changed, 9 insertions(+), 20 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index f03ee0f9f..c8e394a6a 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -9,6 +9,7 @@ keywords = ["no_std", "ethereum"] edition = "2018" [dependencies] +bytes = { version = "1", default-features = false } primitive-types = { version = "0.10", default-features = false } codec = { package = "parity-scale-codec", version = "2.0", default-features = false, features = ["derive", "full"], optional = true } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } diff --git a/core/src/lib.rs b/core/src/lib.rs index 7806a6600..dc7fe4587 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -24,6 +24,7 @@ pub use crate::valids::Valids; use crate::eval::{eval, Control}; use alloc::rc::Rc; use alloc::vec::Vec; +use bytes::Bytes; use core::ops::Range; use primitive_types::{H160, U256}; @@ -32,7 +33,7 @@ pub struct Machine { /// Program data. data: Rc>, /// Program code. - code: Rc>, + code: Bytes, /// Program counter. position: Result, /// Return value. @@ -86,12 +87,7 @@ impl Machine { } /// Create a new machine with given code and data. - pub fn new( - code: Rc>, - data: Rc>, - stack_limit: usize, - memory_limit: usize, - ) -> Self { + pub fn new(code: Bytes, data: Rc>, stack_limit: usize, memory_limit: usize) -> Self { let valids = Valids::new(&code[..]); Self { diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 16fc15050..d0ef8b7bb 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -9,6 +9,7 @@ keywords = ["no_std", "ethereum"] edition = "2018" [dependencies] +bytes = { version = "1", default-features = false } evm-core = { version = "0.33", path = "../core", default-features = false } primitive-types = { version = "0.10", default-features = false } sha3 = { version = "0.8", default-features = false } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 74318456d..079650499 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -35,6 +35,7 @@ pub use crate::interrupt::{Resolve, ResolveCall, ResolveCreate}; use alloc::rc::Rc; use alloc::vec::Vec; +use bytes::Bytes; macro_rules! step { ( $self:expr, $handler:expr, $return:tt $($err:path)?; $($ok:path)? ) => ({ @@ -84,12 +85,7 @@ pub struct Runtime<'config> { impl<'config> Runtime<'config> { /// Create a new runtime with given code and data. - pub fn new( - code: Rc>, - data: Rc>, - context: Context, - config: &'config Config, - ) -> Self { + pub fn new(code: Bytes, data: Rc>, context: Context, config: &'config Config) -> Self { Self { machine: Machine::new(code, data, config.stack_limit, config.memory_limit), status: Ok(()), diff --git a/src/executor/stack/executor.rs b/src/executor/stack/executor.rs index 0a76b8fcb..a2f324b17 100644 --- a/src/executor/stack/executor.rs +++ b/src/executor/stack/executor.rs @@ -703,12 +703,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> self.state.inc_nonce(address); } - let mut runtime = Runtime::new( - Rc::new(init_code), - Rc::new(Vec::new()), - context, - self.config, - ); + let mut runtime = Runtime::new(init_code.into(), Rc::new(Vec::new()), context, self.config); let reason = self.execute(&mut runtime); log::debug!(target: "evm", "Create execution using address {}: {:?}", address, reason); @@ -905,7 +900,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> }; } - let mut runtime = Runtime::new(Rc::new(code), Rc::new(input), context, self.config); + let mut runtime = Runtime::new(code.into(), Rc::new(input), context, self.config); let reason = self.execute(&mut runtime); log::debug!(target: "evm", "Call execution using address {}: {:?}", code_address, reason);