Skip to content

Reduce memory usage, faster copies and other small improvements #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 78 additions & 66 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ lazy_static = "1.4.0"
toml_edit = "0.22.9"
winnow = "0.6.5"
proptest = "1.4.0"
smol_str = {version="0.3.2", features=["serde"]}
tracing = { version = "0.1.40", features = ["log"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
tracing-opentelemetry = "0.24.0"
Expand Down
14 changes: 7 additions & 7 deletions src/agent/legacy_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ mod tests {

// Prepare UTC datetimes that fall before, within and after market hours
let format = "%Y-%m-%d %H:%M";
let bad_datetimes_before = vec![
let bad_datetimes_before = [
NaiveDateTime::parse_from_str("2023-11-20 04:30", format)?.and_utc(),
NaiveDateTime::parse_from_str("2023-11-21 05:30", format)?.and_utc(),
NaiveDateTime::parse_from_str("2023-11-22 06:30", format)?.and_utc(),
Expand All @@ -337,7 +337,7 @@ mod tests {
NaiveDateTime::parse_from_str("2023-11-26 10:30", format)?.and_utc(),
];

let ok_datetimes = vec![
let ok_datetimes = [
NaiveDateTime::parse_from_str("2023-11-20 05:30", format)?.and_utc(),
NaiveDateTime::parse_from_str("2023-11-21 06:30", format)?.and_utc(),
NaiveDateTime::parse_from_str("2023-11-22 07:30", format)?.and_utc(),
Expand All @@ -347,7 +347,7 @@ mod tests {
NaiveDateTime::parse_from_str("2023-11-26 11:30", format)?.and_utc(),
];

let bad_datetimes_after = vec![
let bad_datetimes_after = [
NaiveDateTime::parse_from_str("2023-11-20 06:30", format)?.and_utc(),
NaiveDateTime::parse_from_str("2023-11-21 07:30", format)?.and_utc(),
NaiveDateTime::parse_from_str("2023-11-22 08:30", format)?.and_utc(),
Expand Down Expand Up @@ -384,18 +384,18 @@ mod tests {
"Europe/Amsterdam,23:00-24:00,00:00-01:00,O,C,C,C,C".parse()?;

let format = "%Y-%m-%d %H:%M";
let ok_datetimes = vec![
let ok_datetimes = [
NaiveDate::from_ymd_opt(2023, 11, 20)
.unwrap()
.and_time(MAX_TIME_INSTANT.clone())
.and_time(*MAX_TIME_INSTANT)
.and_local_timezone(Tz::Europe__Amsterdam)
.unwrap(),
NaiveDateTime::parse_from_str("2023-11-21 00:00", format)?
.and_local_timezone(Tz::Europe__Amsterdam)
.unwrap(),
];

let bad_datetimes = vec![
let bad_datetimes = [
// Start of Monday Nov 20th, must not be confused for MAX_TIME_INSTANT on that day
NaiveDateTime::parse_from_str("2023-11-20 00:00", format)?
.and_local_timezone(Tz::Europe__Amsterdam)
Expand All @@ -404,7 +404,7 @@ mod tests {
// confused for Wednesday 00:00 which is open.
NaiveDate::from_ymd_opt(2023, 11, 21)
.unwrap()
.and_time(MAX_TIME_INSTANT.clone())
.and_time(*MAX_TIME_INSTANT)
.and_local_timezone(Tz::Europe__Amsterdam)
.unwrap(),
];
Expand Down
11 changes: 3 additions & 8 deletions src/agent/market_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const MAX_TIME_INSTANT: NaiveTime = NaiveTime::MIN
.overflowing_sub_signed(Duration::nanoseconds(1))
.0;

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct MarketSchedule {
pub timezone: Tz,
pub weekly_schedule: Vec<ScheduleDayKind>,
Expand Down Expand Up @@ -193,8 +193,9 @@ impl Display for HolidayDaySchedule {
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub enum ScheduleDayKind {
#[default]
Open,
Closed,
TimeRanges(Vec<RangeInclusive<NaiveTime>>),
Expand All @@ -210,12 +211,6 @@ impl ScheduleDayKind {
}
}

impl Default for ScheduleDayKind {
fn default() -> Self {
Self::Open
}
}

impl Display for ScheduleDayKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
11 changes: 6 additions & 5 deletions src/agent/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use {
registry::Registry,
},
serde::Deserialize,
smol_str::SmolStr,
solana_sdk::pubkey::Pubkey,
std::{
net::SocketAddr,
Expand Down Expand Up @@ -64,9 +65,7 @@ pub async fn spawn(addr: impl Into<SocketAddr> + 'static) {
let mut buf = String::new();
let response = encode(&mut buf, &&PROMETHEUS_REGISTRY.lock().await)
.map_err(|e| -> Box<dyn std::error::Error> { e.into() })
.and_then(|_| -> Result<_, Box<dyn std::error::Error>> {
Ok(Box::new(reply::with_status(buf, StatusCode::OK)))
})
.map(|_| Box::new(reply::with_status(buf, StatusCode::OK)))
.unwrap_or_else(|e| {
tracing::error!(err = ?e, "Metrics: Could not gather metrics from registry");
Box::new(reply::with_status(
Expand Down Expand Up @@ -115,8 +114,10 @@ impl ProductGlobalMetrics {
metrics
}

pub fn update(&self, product_key: &Pubkey, maybe_symbol: Option<String>) {
let symbol_string = maybe_symbol.unwrap_or(format!("unknown_{}", product_key));
pub fn update(&self, product_key: &Pubkey, maybe_symbol: Option<SmolStr>) {
let symbol_string = maybe_symbol
.map(|x| x.into())
.unwrap_or(format!("unknown_{}", product_key));

#[deny(unused_variables)]
let Self { update_count } = self;
Expand Down
42 changes: 23 additions & 19 deletions src/agent/pyth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,50 @@ use {
Deserialize,
Serialize,
},
std::collections::BTreeMap,
smol_str::SmolStr,
std::{
collections::BTreeMap,
sync::Arc,
},
};

pub mod rpc;

pub type Pubkey = String;
pub type Attrs = BTreeMap<String, String>;
pub type Pubkey = SmolStr;
pub type Attrs = BTreeMap<SmolStr, SmolStr>;

pub type Price = i64;
pub type Exponent = i64;
pub type Conf = u64;
pub type Slot = u64;

#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct ProductAccountMetadata {
pub account: Pubkey,
pub attr_dict: Attrs,
pub price: Vec<PriceAccountMetadata>,
pub price: Arc<[PriceAccountMetadata]>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct PriceAccountMetadata {
pub account: Pubkey,
pub price_type: String,
pub price_type: SmolStr,
pub price_exponent: Exponent,
}

#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct ProductAccount {
pub account: Pubkey,
pub attr_dict: Attrs,
pub price_accounts: Vec<PriceAccount>,
pub price_accounts: Arc<[PriceAccount]>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct PriceAccount {
pub account: Pubkey,
pub price_type: String,
pub price_type: SmolStr,
pub price_exponent: Exponent,
pub status: String,
pub status: SmolStr,
pub price: Price,
pub conf: Conf,
pub twap: Price,
Expand All @@ -52,36 +56,36 @@ pub struct PriceAccount {
pub prev_slot: Slot,
pub prev_price: Price,
pub prev_conf: Conf,
pub publisher_accounts: Vec<PublisherAccount>,
pub publisher_accounts: Arc<[PublisherAccount]>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct PublisherAccount {
pub account: Pubkey,
pub status: String,
pub status: SmolStr,
pub price: Price,
pub conf: Conf,
pub slot: Slot,
}

#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct NotifyPrice {
pub subscription: SubscriptionID,
pub result: PriceUpdate,
}

#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct NotifyPriceSched {
pub subscription: SubscriptionID,
}

pub type SubscriptionID = i64;

#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct PriceUpdate {
pub price: Price,
pub conf: Conf,
pub status: String,
pub status: SmolStr,
pub valid_slot: Slot,
pub pub_slot: Slot,
}
Loading
Loading