Skip to content

Commit

Permalink
perf: avoid cloning precompiles twice
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jun 4, 2024
1 parent b7b92ae commit aed1176
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
34 changes: 22 additions & 12 deletions crates/revm/src/context/context_precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,22 +36,32 @@ pub struct ContextPrecompiles<DB: Database> {
inner: HashMap<Address, ContextPrecompile<DB>>,
}

impl<DB: Database> Extend<(Address, ContextPrecompile<DB>)> for ContextPrecompiles<DB> {
fn extend<T: IntoIterator<Item = (Address, ContextPrecompile<DB>)>>(&mut self, iter: T) {
self.inner.extend(iter.into_iter().map(Into::into))
}
}

impl<DB: Database> Extend<PrecompileWithAddress> for ContextPrecompiles<DB> {
fn extend<T: IntoIterator<Item = PrecompileWithAddress>>(&mut self, iter: T) {
self.inner.extend(iter.into_iter().map(|precompile| {
let (address, precompile) = precompile.into();
(address, precompile.into())
}));
}
}

impl<DB: Database> ContextPrecompiles<DB> {
/// Returns precompiles addresses.
/// Creates a new precompiles at the given spec ID.
#[inline]
pub fn addresses(&self) -> impl Iterator<Item = &Address> {
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<Item = impl Into<(Address, ContextPrecompile<DB>)>>,
) {
self.inner.extend(other.into_iter().map(Into::into));
pub fn addresses(&self) -> impl Iterator<Item = &Address> {
self.inner.keys()
}

/// Call precompile and executes it. Returns the result of the precompile execution.
Expand Down
6 changes: 2 additions & 4 deletions crates/revm/src/handler/mainnet/pre_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,9 +16,7 @@ use crate::{
/// Main precompile load
#[inline]
pub fn load_precompiles<SPEC: Spec, DB: Database>() -> ContextPrecompiles<DB> {
Precompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID))
.clone()
.into()
ContextPrecompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID))
}

/// Main load handle
Expand Down
6 changes: 3 additions & 3 deletions crates/revm/src/optimism/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn last_frame_return<SPEC: Spec, EXT, DB: Database>(
/// Load precompiles for Optimism chain.
#[inline]
pub fn load_precompiles<SPEC: Spec, EXT, DB: Database>() -> ContextPrecompiles<DB> {
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([
Expand All @@ -152,7 +152,7 @@ pub fn load_precompiles<SPEC: Spec, EXT, DB: Database>() -> ContextPrecompiles<D
])
}

precompiles.into()
precompiles
}

/// Load account (make them warm) and l1 data from database.
Expand Down

0 comments on commit aed1176

Please sign in to comment.