Skip to content

perf: anvil_mine is much slower than hardhat_mine #5499

Open
@fp-crypto

Description

@fp-crypto

I attempted to move my testing from hardhat node to anvil. Some of my testing requires mining a large number of blocks. anvil_mine execution time seems to scale linearly relative to block count, whereas hardhat_mine with hardhat node is nearly constant. See the following benchmarks:

Anvil

In [1]: import timeit

In [2]: [timeit.timeit(stmt=(f'chain.mine({i})'), globals=globals(), number=50) for i in [1, 1
   ...: 0, 100, 1000]]
Out[2]:
[0.07101033299113624,
 0.39050891700026114,
 6.034164124997915,
 276.3122650830046]

Hardhat

In [1]: import timeit

In [2]: [timeit.timeit(stmt=(f'chain.mine({i})'), globals=globals(), number=50) for i in [1, 1
   ...: 0, 100, 1000]]
Out[2]:
[0.10906625000643544,
 0.09370395800215192,
 0.0902477499912493,
 0.08129970800655428]

I'm using:

  • anvil 0.1.0 (0e33b3e 2023-07-26T00:26:08.161934000Z)
  • hardhat 9.6.7
  • ape 0.6.14 (for testing)

foundry/anvil/src/eth/api.rs

Lines 1421 to 1440 in 41bae8e

pub async fn anvil_mine(&self, num_blocks: Option<U256>, interval: Option<U256>) -> Result<()> {
node_info!("anvil_mine");
let interval = interval.map(|i| i.as_u64());
let blocks = num_blocks.unwrap_or_else(U256::one);
if blocks == U256::zero() {
return Ok(())
}
// mine all the blocks
for _ in 0..blocks.as_u64() {
self.mine_one().await;
// If we have an interval, jump forwards in time to the "next" timestamp
if let Some(interval) = interval {
self.backend.time().increase_time(interval);
}
}
Ok(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions