From 18c5efb6395b8d324617832a6a114019db735a3d Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 4 Jun 2024 12:37:15 +0200 Subject: [PATCH] perf: avoid cloning precompiles twice --- .../revm/src/context/context_precompiles.rs | 34 ++++++++++++------- .../revm/src/handler/mainnet/pre_execution.rs | 6 ++-- crates/revm/src/optimism/handler_register.rs | 6 ++-- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/crates/revm/src/context/context_precompiles.rs b/crates/revm/src/context/context_precompiles.rs index a6fdc5e67e..6f18a9a37f 100644 --- a/crates/revm/src/context/context_precompiles.rs +++ b/crates/revm/src/context/context_precompiles.rs @@ -4,7 +4,7 @@ use crate::{ }; use core::ops::{Deref, DerefMut}; use dyn_clone::DynClone; -use revm_precompile::Precompiles; +use revm_precompile::{PrecompileSpecId, PrecompileWithAddress, Precompiles}; use std::{boxed::Box, sync::Arc}; use super::InnerEvmContext; @@ -36,22 +36,32 @@ pub struct ContextPrecompiles { inner: HashMap>, } +impl Extend<(Address, ContextPrecompile)> for ContextPrecompiles { + fn extend)>>(&mut self, iter: T) { + self.inner.extend(iter.into_iter().map(Into::into)) + } +} + +impl Extend for ContextPrecompiles { + fn extend>(&mut self, iter: T) { + self.inner.extend(iter.into_iter().map(|precompile| { + let (address, precompile) = precompile.into(); + (address, precompile.into()) + })); + } +} + impl ContextPrecompiles { - /// Returns precompiles addresses. + /// Creates a new precompiles at the given spec ID. #[inline] - pub fn addresses(&self) -> impl Iterator { - self.inner.keys() + pub fn new(spec_id: PrecompileSpecId) -> Self { + Precompiles::new(spec_id).into() } - /// Extends the precompiles with the given precompiles. - /// - /// Other precompiles with overwrite existing precompiles. + /// Returns precompiles addresses. #[inline] - pub fn extend( - &mut self, - other: impl IntoIterator)>>, - ) { - self.inner.extend(other.into_iter().map(Into::into)); + pub fn addresses(&self) -> impl Iterator { + self.inner.keys() } /// Call precompile and executes it. Returns the result of the precompile execution. diff --git a/crates/revm/src/handler/mainnet/pre_execution.rs b/crates/revm/src/handler/mainnet/pre_execution.rs index c0f4fe7bde..d3b9759dcf 100644 --- a/crates/revm/src/handler/mainnet/pre_execution.rs +++ b/crates/revm/src/handler/mainnet/pre_execution.rs @@ -3,7 +3,7 @@ //! They handle initial setup of the EVM, call loop and the final return of the EVM use crate::{ - precompile::{PrecompileSpecId, Precompiles}, + precompile::PrecompileSpecId, primitives::{ db::Database, Account, EVMError, Env, Spec, @@ -16,9 +16,7 @@ use crate::{ /// Main precompile load #[inline] pub fn load_precompiles() -> ContextPrecompiles { - Precompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)) - .clone() - .into() + ContextPrecompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)) } /// Main load handle diff --git a/crates/revm/src/optimism/handler_register.rs b/crates/revm/src/optimism/handler_register.rs index d4996cca21..899ba41279 100644 --- a/crates/revm/src/optimism/handler_register.rs +++ b/crates/revm/src/optimism/handler_register.rs @@ -14,7 +14,7 @@ use crate::{ Context, ContextPrecompiles, FrameResult, }; use core::ops::Mul; -use revm_precompile::{secp256r1, PrecompileSpecId, Precompiles}; +use revm_precompile::{secp256r1, PrecompileSpecId}; use std::string::ToString; use std::sync::Arc; @@ -143,7 +143,7 @@ pub fn last_frame_return( /// Load precompiles for Optimism chain. #[inline] pub fn load_precompiles() -> ContextPrecompiles { - let mut precompiles = Precompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)).clone(); + let mut precompiles = ContextPrecompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)); if SPEC::enabled(SpecId::FJORD) { precompiles.extend([ @@ -152,7 +152,7 @@ pub fn load_precompiles() -> ContextPrecompiles