Skip to content

feat: add precompile cache for execution #15928

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

Merged
merged 54 commits into from
May 9, 2025

Conversation

fgimenez
Copy link
Member

@fgimenez fgimenez commented Apr 25, 2025

Closes #13984

@fgimenez fgimenez requested a review from gakonst as a code owner April 25, 2025 12:44
@github-project-automation github-project-automation bot moved this to Backlog in Reth Tracker Apr 25, 2025
@fgimenez fgimenez marked this pull request as draft April 25, 2025 12:44
Copy link

codspeed-hq bot commented Apr 25, 2025

CodSpeed Performance Report

Merging #15928 will not alter performance

Comparing fgimenez/precompile-cache-hotswap (948c04a) with main (a3c067c)

Summary

✅ 77 untouched benchmarks

@kevaundray
Copy link
Contributor

ref: #13984

@jenpaff jenpaff moved this from Backlog to In Progress in Reth Tracker Apr 29, 2025
@jenpaff jenpaff linked an issue Apr 29, 2025 that may be closed by this pull request
@jenpaff jenpaff removed this from Reth Tracker Apr 29, 2025
@github-project-automation github-project-automation bot moved this to Backlog in Reth Tracker Apr 29, 2025
@fgimenez fgimenez added the C-perf A change motivated by improving speed, memory usage or disk footprint label Apr 29, 2025
@fgimenez fgimenez moved this from Backlog to In Progress in Reth Tracker Apr 29, 2025
@fgimenez fgimenez force-pushed the fgimenez/precompile-cache-hotswap branch 2 times, most recently from 82a7a16 to d0b358a Compare April 30, 2025 14:27
@fgimenez fgimenez changed the base branch from main to fgimenez/remove-block-executor-provider April 30, 2025 14:28
@fgimenez fgimenez force-pushed the fgimenez/remove-block-executor-provider branch from 4946af7 to 324a283 Compare April 30, 2025 17:54
@fgimenez fgimenez force-pushed the fgimenez/precompile-cache-hotswap branch from 90b76d8 to dda55da Compare April 30, 2025 17:55
Comment on lines 57 to 60
|data: &Bytes, gas_limit: u64| -> PrecompileResult {
Self::new(precompile, cache).call(data, gas_limit)
}
.into()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a helper constructor to DynPrecompile that accepts a closure, this is easier than doing from conversions which suck a bit for closures

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rn there's no way to construct DynPrecompile from just a impl Precompile, so we'd need a helper for it in alloy-evm first

Comment on lines 57 to 59
|data: &Bytes, gas_limit: u64| -> PrecompileResult {
Self::new(precompile, cache).call(data, gas_limit)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't quite right, because this is the precompile fn, which internally now creates a new CachedPrecompile and invokes it

the conversion we need here Self::new(precompile, cache).into()

Comment on lines +152 to +156
authorization_list: authorization_list
.unwrap_or_default()
.into_iter()
.map(Either::Left)
.collect(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, this is a bit horrible,

I don't think it's useful to do this for auths individually, but this is a revm api discussion

Comment on lines 2478 to 2486
if self.config.precompile_cache_enabled() {
let mut evm = self
.evm_config
.evm_for_block(StateProviderDatabase::new(&state_provider), block.header());
let precompiles = evm.precompiles_mut();
precompiles.map_precompiles(|_, precompile| {
CachedPrecompile::wrap(precompile, self.precompile_cache.clone())
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not yet have effect right? because this evm does not belong to the executor we're instantiating below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes exactly, we should obtain the evm from the executor, with the current local block executor provider trait we could extend it to get an evm config

@klkvr klkvr changed the base branch from fgimenez/remove-block-executor-provider to main May 2, 2025 16:56
@klkvr klkvr force-pushed the fgimenez/precompile-cache-hotswap branch from d5fe418 to 4b85afa Compare May 2, 2025 16:56
@fgimenez fgimenez force-pushed the fgimenez/precompile-cache-hotswap branch 5 times, most recently from fc8b054 to dbee913 Compare May 6, 2025 14:46
@fgimenez fgimenez marked this pull request as ready for review May 6, 2025 14:48
@fgimenez fgimenez requested a review from Rjected as a code owner May 6, 2025 14:48
@fgimenez fgimenez force-pushed the fgimenez/precompile-cache-hotswap branch from ad37727 to 2beaf94 Compare May 7, 2025 10:01

impl Precompile for CachedPrecompile {
fn call(&self, data: &[u8], gas_limit: u64) -> PrecompileResult {
let key = CacheKey(Bytes::copy_from_slice(data));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can solve this more efficiently because we only need Borrow here, but we can take care of this seperately.

}

impl Precompile for CachedPrecompile {
fn call(&self, data: &[u8], gas_limit: u64) -> PrecompileResult {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a lot simpler now compared to what we previously had :D

@@ -581,6 +581,7 @@ impl From<InvalidTransaction> for RpcInvalidTransactionError {
InvalidTransaction::Eip1559NotSupported |
InvalidTransaction::Eip4844NotSupported |
InvalidTransaction::Eip7702NotSupported => Self::TxTypeNotSupported,
_ => todo!(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cant have a todo here

this must be exhaustive and must not panic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, done

Comment on lines +63 to +64
if spec == SpecId::PRAGUE {
evm = evm.with_precompiles(PrecompilesMap::from_static(prague_custom()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check I dont understand

why do we need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it comes from the current example, the custom precompiles come from Prague's https://github.com/paradigmxyz/reth/blob/main/examples/custom-evm/src/main.rs#L110-L124

@klkvr klkvr added this pull request to the merge queue May 8, 2025
@mattsse mattsse removed this pull request from the merge queue due to a manual request May 8, 2025
@mattsse mattsse added this pull request to the merge queue May 9, 2025
Merged via the queue into main with commit 2054a37 May 9, 2025
74 of 78 checks passed
@mattsse mattsse deleted the fgimenez/precompile-cache-hotswap branch May 9, 2025 10:04
@github-project-automation github-project-automation bot moved this from In Progress to Done in Reth Tracker May 9, 2025
@mattsse mattsse restored the fgimenez/precompile-cache-hotswap branch May 9, 2025 13:08
@jenpaff jenpaff moved this from Done to Completed in Reth Tracker May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-perf A change motivated by improving speed, memory usage or disk footprint
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

Precompile Caching
6 participants