Skip to content

Commit 0e7ccdc

Browse files
authored
Use arcswap to speed things up (#371)
1 parent b1e2c35 commit 0e7ccdc

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/flashblocks-rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ serde.workspace = true
5757
jsonrpsee = { version = "0.25.1" }
5858
futures-util = "0.3.31"
5959
brotli = "8.0.1"
60+
arc-swap = "1.7.1"
6061

6162
[[bin]]
6263
name = "flashblocks-rpc"

crates/flashblocks-rpc/src/cache.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloy_consensus::transaction::TransactionMeta;
44
use alloy_primitives::{Address, Sealable, TxHash, U256};
55
use alloy_rpc_types::Withdrawals;
66
use alloy_rpc_types::{BlockTransactions, Header, TransactionInfo};
7+
use arc_swap::ArcSwap;
78
use op_alloy_consensus::OpTxEnvelope;
89
use op_alloy_network::Optimism;
910
use op_alloy_rpc_types::OpTransactionReceipt;
@@ -20,11 +21,7 @@ use rollup_boost::{
2021
FlashblockBuilder, FlashblocksPayloadV1, OpExecutionPayloadEnvelope, PayloadVersion,
2122
};
2223
use serde::{Deserialize, Serialize};
23-
use std::{
24-
collections::HashMap,
25-
str::FromStr,
26-
sync::{Arc, Mutex},
27-
};
24+
use std::{collections::HashMap, str::FromStr, sync::Arc};
2825

2926
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
3027
pub struct Metadata {
@@ -35,37 +32,44 @@ pub struct Metadata {
3532

3633
#[derive(Clone)]
3734
pub struct FlashblocksCache {
38-
inner: Arc<Mutex<FlashblocksCacheInner>>,
35+
inner: Arc<ArcSwap<FlashblocksCacheInner>>,
36+
// TODO: add arc_swap::Cache to speed it up even more
3937
}
4038

4139
impl FlashblocksCache {
4240
pub fn new(chain_spec: Arc<OpChainSpec>) -> Self {
4341
Self {
44-
inner: Arc::new(Mutex::new(FlashblocksCacheInner::new(chain_spec))),
42+
inner: Arc::new(ArcSwap::from_pointee(FlashblocksCacheInner::new(
43+
chain_spec,
44+
))),
4545
}
4646
}
4747

4848
pub fn get_block(&self, full: bool) -> Option<RpcBlock<Optimism>> {
49-
self.inner.lock().unwrap().get_block(full)
49+
ArcSwap::load(&self.inner).get_block(full)
5050
}
5151

5252
pub fn get_transaction_count(&self, address: Address) -> Option<u64> {
53-
self.inner.lock().unwrap().get_nonce(address)
53+
ArcSwap::load(&self.inner).get_nonce(address)
5454
}
5555

5656
pub fn get_balance(&self, address: Address) -> Option<U256> {
57-
self.inner.lock().unwrap().get_balance(address)
57+
ArcSwap::load(&self.inner).get_balance(address)
5858
}
5959

6060
pub fn get_receipt(&self, tx_hash: &TxHash) -> Option<RpcReceipt<Optimism>> {
61-
self.inner.lock().unwrap().get_receipt(tx_hash)
61+
ArcSwap::load(&self.inner).get_receipt(tx_hash)
6262
}
6363

6464
pub fn process_payload(&self, payload: FlashblocksPayloadV1) -> eyre::Result<()> {
65-
self.inner.lock().unwrap().process_payload(payload)
65+
let mut new_state = FlashblocksCacheInner::clone(&self.inner.load_full());
66+
new_state.process_payload(payload)?;
67+
self.inner.store(Arc::new(new_state));
68+
Ok(())
6669
}
6770
}
6871

72+
#[derive(Clone)]
6973
struct FlashblocksCacheInner {
7074
chain_spec: Arc<OpChainSpec>,
7175
builder: FlashblockBuilder,

crates/rollup-boost/src/flashblocks/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum FlashblocksEngineMessage {
5555
FlashblocksPayloadV1(FlashblocksPayloadV1),
5656
}
5757

58-
#[derive(Debug, Default)]
58+
#[derive(Clone, Debug, Default)]
5959
pub struct FlashblockBuilder {
6060
base: Option<ExecutionPayloadBaseV1>,
6161
flashblocks: Vec<ExecutionPayloadFlashblockDeltaV1>,

0 commit comments

Comments
 (0)