Skip to content

Commit

Permalink
Merge pull request #212 from darkforestry/0xkitsune/state-space-manager
Browse files Browse the repository at this point in the history
chore(state-space): Update default capacity for `StateSpaceCache`
  • Loading branch information
0xOsiris authored Aug 30, 2024
2 parents 21ad41b + 184fb44 commit d711856
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion benches/state_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn add_state_changes_benchmark(c: &mut Criterion) {
// Benchmark adding state changes to the cache with setup
c.bench_function("add state changes to cache with setup", |b| {
b.iter_batched(
|| StateChangeCache::new(),
|| StateChangeCache::<150>::new(),
|mut cache| {
for state_change in state_changes.clone() {
cache
Expand Down
6 changes: 3 additions & 3 deletions src/state_space/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use arraydeque::{ArrayDeque, CapacityError};

#[derive(Debug)]

pub struct StateChangeCache {
pub struct StateChangeCache<const CAP: usize> {
oldest_block: u64,
cache: ArrayDeque<StateChange, 150>,
cache: ArrayDeque<StateChange, CAP>,
}

impl StateChangeCache {
impl<const CAP: usize> StateChangeCache<CAP> {
pub fn new() -> Self {
StateChangeCache {
oldest_block: 0,
Expand Down
34 changes: 24 additions & 10 deletions src/state_space/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ impl From<Vec<AMM>> for StateSpace {
}

#[derive(Debug)]
pub struct StateSpaceManager<T, N, P> {
pub struct StateSpaceManager<T, N, P, const CAP: usize> {
state: Arc<RwLock<StateSpace>>,
state_change_cache: Arc<RwLock<StateChangeCache>>,
state_change_cache: Arc<RwLock<StateChangeCache<CAP>>>,
provider: Arc<P>,
phantom: PhantomData<(T, N)>,
}

// TODO: Much of this can be simplified
impl<T, N, P> StateSpaceManager<T, N, P>
impl<T, N, P> StateSpaceManager<T, N, P, 30>
where
T: Transport + Clone,
N: Network,
Expand Down Expand Up @@ -213,6 +212,21 @@ where
}
}

impl<T, N, P, const CAP: usize> StateSpaceManager<T, N, P, CAP>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N> + 'static,
{
pub fn new_with_capacity(amms: Vec<AMM>, provider: Arc<P>) -> Self {
Self {
state: Arc::new(RwLock::new(amms.into())),
state_change_cache: Arc::new(RwLock::new(StateChangeCache::new())),
provider,
phantom: PhantomData,
}
}
}
#[derive(Debug, Clone)]
pub struct StateChange {
pub state_change: Vec<AMM>,
Expand All @@ -228,9 +242,9 @@ impl StateChange {
}
}

pub async fn handle_state_changes_from_logs(
pub async fn handle_state_changes_from_logs<const CAP: usize>(
state: Arc<RwLock<StateSpace>>,
state_change_cache: Arc<RwLock<StateChangeCache>>,
state_change_cache: Arc<RwLock<StateChangeCache<CAP>>>,
logs: Vec<Log>,
) -> Result<Vec<Address>, StateSpaceError> {
// If there are no logs to process, return early
Expand Down Expand Up @@ -280,10 +294,10 @@ pub async fn handle_state_changes_from_logs(

/// Commits state changes contained in `prev_state` to the state change cache
/// and clears the `prev_state` vec
async fn commit_state_changes(
async fn commit_state_changes<const CAP: usize>(
prev_state: &mut Vec<AMM>,
block_number: u64,
state_change_cache: Arc<RwLock<StateChangeCache>>,
state_change_cache: Arc<RwLock<StateChangeCache<CAP>>>,
) {
if !prev_state.is_empty() {
let state_change = StateChange::new(prev_state.clone(), block_number);
Expand All @@ -297,9 +311,9 @@ async fn commit_state_changes(
}

/// Unwinds the state changes up to the specified block number
async fn unwind_state_changes(
async fn unwind_state_changes<const CAP: usize>(
state: Arc<RwLock<StateSpace>>,
state_change_cache: Arc<RwLock<StateChangeCache>>,
state_change_cache: Arc<RwLock<StateChangeCache<CAP>>>,
chain_head_block_number: u64,
) -> u64 {
let updated_amms = state_change_cache
Expand Down

0 comments on commit d711856

Please sign in to comment.