Skip to content

Commit

Permalink
Use a set instead of a vec for storing recently accessed assets, move…
Browse files Browse the repository at this point in the history
… size enforcement to the setter
  • Loading branch information
zbuc committed May 3, 2024
1 parent 8ba3704 commit d66ea67
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crates/core/component/dex/src/component/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ impl Component for Dex {
.fixed_candidates
.iter()
.cloned()
// Limit the inclusion of recently accessed assets to 10 to avoid
// The set of recently accessed assets is already limited to avoid
// potentially blowing up routing time.
.chain(state.recently_accessed_assets().iter().take(10).cloned())
.chain(state.recently_accessed_assets().iter().cloned())
.collect::<Vec<_>>(),
);

Expand Down
12 changes: 10 additions & 2 deletions crates/core/component/dex/src/component/position_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
use crate::{event, state_key};

const DYNAMIC_ASSET_LIMIT: usize = 10;
const RECENTLY_ACCESSED_ASSET_LIMIT: usize = 10;

mod base_liquidity_index;
mod counter;
Expand Down Expand Up @@ -144,7 +145,7 @@ pub trait PositionRead: StateRead {
}

/// Fetch the list of assets interacted with during this block.
fn recently_accessed_assets(&self) -> im::Vector<asset::Id> {
fn recently_accessed_assets(&self) -> im::OrdSet<asset::Id> {
self.object_get(state_key::recently_accessed_assets())
.unwrap_or_default()
}
Expand Down Expand Up @@ -270,7 +271,14 @@ pub trait PositionManager: StateWrite + PositionRead {
#[tracing::instrument(level = "debug", skip_all)]
fn add_recently_accessed_asset(&mut self, asset_id: asset::Id) {
let mut assets = self.recently_accessed_assets();
assets.push_back(asset_id);

// Limit the number of recently accessed assets to prevent blowing
// up routing time.
if assets.len() >= RECENTLY_ACCESSED_ASSET_LIMIT {
return;
}

assets.insert(asset_id);
self.object_put(state_key::recently_accessed_assets(), assets);
}

Expand Down

0 comments on commit d66ea67

Please sign in to comment.