Skip to content

Commit 9752d0a

Browse files
committed
Add metrics for bundle state clone cost
Track duration and size (number of accounts) when cloning BundleState to help identify if this becomes a performance bottleneck.
1 parent d8c7184 commit 9752d0a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

crates/flashblocks-rpc/src/metrics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,10 @@ pub struct Metrics {
6666

6767
#[metric(describe = "Total number of WebSocket reconnection attempts")]
6868
pub reconnect_attempts: Counter,
69+
70+
#[metric(describe = "Time taken to clone bundle state")]
71+
pub bundle_state_clone_duration: Histogram,
72+
73+
#[metric(describe = "Size of bundle state being cloned (number of accounts)")]
74+
pub bundle_state_clone_size: Histogram,
6975
}

crates/flashblocks-rpc/src/pending_blocks.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{sync::Arc, time::Instant};
22

33
use alloy_consensus::{Header, Sealed};
44
use alloy_eips::BlockNumberOrTag;
@@ -20,7 +20,7 @@ use reth::revm::{
2020
use reth_rpc_convert::RpcTransaction;
2121
use reth_rpc_eth_api::{RpcBlock, RpcReceipt};
2222

23-
use crate::{rpc::PendingBlocksAPI, subscription::Flashblock};
23+
use crate::{metrics::Metrics, rpc::PendingBlocksAPI, subscription::Flashblock};
2424

2525
pub struct PendingBlocksBuilder {
2626
flashblocks: Vec<Flashblock>,
@@ -208,8 +208,20 @@ impl PendingBlocks {
208208
self.db_cache.clone()
209209
}
210210

211+
/// Returns a clone of the bundle state.
212+
///
213+
/// NOTE: This clones the entire BundleState, which contains a HashMap of all touched
214+
/// accounts and their storage slots. The cost scales with the number of accounts and
215+
/// storage slots modified in the flashblock. Monitor `bundle_state_clone_duration` and
216+
/// `bundle_state_clone_size` metrics to track if this becomes a bottleneck.
211217
pub fn get_bundle_state(&self) -> BundleState {
212-
self.bundle_state.clone()
218+
let metrics = Metrics::default();
219+
let size = self.bundle_state.state.len();
220+
let start = Instant::now();
221+
let cloned = self.bundle_state.clone();
222+
metrics.bundle_state_clone_duration.record(start.elapsed());
223+
metrics.bundle_state_clone_size.record(size as f64);
224+
cloned
213225
}
214226

215227
pub fn get_transactions_for_block(&self, block_number: BlockNumber) -> Vec<Transaction> {

0 commit comments

Comments
 (0)