Skip to content

Commit a9d77e0

Browse files
authored
Reduce memory usage, faster copies and other small improvements (#152)
* Remove useless use of vec! * Use of Default * Unnecessary clone call * Big discrepancy in fields size on enum * Add import * Complex type * unnecessary vec! * fix usage of Arc * Simplify call * Update solana libs * Remove unnecessary clone * Replacing imutable strings by SmolStr * Fix formatting
1 parent c177d5b commit a9d77e0

File tree

12 files changed

+335
-300
lines changed

12 files changed

+335
-300
lines changed

Cargo.lock

Lines changed: 78 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ lazy_static = "1.4.0"
4646
toml_edit = "0.22.9"
4747
winnow = "0.6.5"
4848
proptest = "1.4.0"
49+
smol_str = {version="0.3.2", features=["serde"]}
4950
tracing = { version = "0.1.40", features = ["log"] }
5051
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
5152
tracing-opentelemetry = "0.24.0"

src/agent/legacy_schedule.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ mod tests {
327327

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

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

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

386386
let format = "%Y-%m-%d %H:%M";
387-
let ok_datetimes = vec![
387+
let ok_datetimes = [
388388
NaiveDate::from_ymd_opt(2023, 11, 20)
389389
.unwrap()
390-
.and_time(MAX_TIME_INSTANT.clone())
390+
.and_time(*MAX_TIME_INSTANT)
391391
.and_local_timezone(Tz::Europe__Amsterdam)
392392
.unwrap(),
393393
NaiveDateTime::parse_from_str("2023-11-21 00:00", format)?
394394
.and_local_timezone(Tz::Europe__Amsterdam)
395395
.unwrap(),
396396
];
397397

398-
let bad_datetimes = vec![
398+
let bad_datetimes = [
399399
// Start of Monday Nov 20th, must not be confused for MAX_TIME_INSTANT on that day
400400
NaiveDateTime::parse_from_str("2023-11-20 00:00", format)?
401401
.and_local_timezone(Tz::Europe__Amsterdam)
@@ -404,7 +404,7 @@ mod tests {
404404
// confused for Wednesday 00:00 which is open.
405405
NaiveDate::from_ymd_opt(2023, 11, 21)
406406
.unwrap()
407-
.and_time(MAX_TIME_INSTANT.clone())
407+
.and_time(*MAX_TIME_INSTANT)
408408
.and_local_timezone(Tz::Europe__Amsterdam)
409409
.unwrap(),
410410
];

src/agent/market_schedule.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const MAX_TIME_INSTANT: NaiveTime = NaiveTime::MIN
4747
.overflowing_sub_signed(Duration::nanoseconds(1))
4848
.0;
4949

50-
#[derive(Clone, Debug, Eq, PartialEq)]
50+
#[derive(Debug, Eq, PartialEq, Clone)]
5151
pub struct MarketSchedule {
5252
pub timezone: Tz,
5353
pub weekly_schedule: Vec<ScheduleDayKind>,
@@ -193,8 +193,9 @@ impl Display for HolidayDaySchedule {
193193
}
194194
}
195195

196-
#[derive(Clone, Debug, Eq, PartialEq)]
196+
#[derive(Default, Clone, Debug, Eq, PartialEq)]
197197
pub enum ScheduleDayKind {
198+
#[default]
198199
Open,
199200
Closed,
200201
TimeRanges(Vec<RangeInclusive<NaiveTime>>),
@@ -210,12 +211,6 @@ impl ScheduleDayKind {
210211
}
211212
}
212213

213-
impl Default for ScheduleDayKind {
214-
fn default() -> Self {
215-
Self::Open
216-
}
217-
}
218-
219214
impl Display for ScheduleDayKind {
220215
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
221216
match self {

src/agent/metrics.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use {
1515
registry::Registry,
1616
},
1717
serde::Deserialize,
18+
smol_str::SmolStr,
1819
solana_sdk::pubkey::Pubkey,
1920
std::{
2021
net::SocketAddr,
@@ -64,9 +65,7 @@ pub async fn spawn(addr: impl Into<SocketAddr> + 'static) {
6465
let mut buf = String::new();
6566
let response = encode(&mut buf, &&PROMETHEUS_REGISTRY.lock().await)
6667
.map_err(|e| -> Box<dyn std::error::Error> { e.into() })
67-
.and_then(|_| -> Result<_, Box<dyn std::error::Error>> {
68-
Ok(Box::new(reply::with_status(buf, StatusCode::OK)))
69-
})
68+
.map(|_| Box::new(reply::with_status(buf, StatusCode::OK)))
7069
.unwrap_or_else(|e| {
7170
tracing::error!(err = ?e, "Metrics: Could not gather metrics from registry");
7271
Box::new(reply::with_status(
@@ -115,8 +114,10 @@ impl ProductGlobalMetrics {
115114
metrics
116115
}
117116

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

121122
#[deny(unused_variables)]
122123
let Self { update_count } = self;

src/agent/pyth.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,50 @@ use {
33
Deserialize,
44
Serialize,
55
},
6-
std::collections::BTreeMap,
6+
smol_str::SmolStr,
7+
std::{
8+
collections::BTreeMap,
9+
sync::Arc,
10+
},
711
};
812

913
pub mod rpc;
1014

11-
pub type Pubkey = String;
12-
pub type Attrs = BTreeMap<String, String>;
15+
pub type Pubkey = SmolStr;
16+
pub type Attrs = BTreeMap<SmolStr, SmolStr>;
1317

1418
pub type Price = i64;
1519
pub type Exponent = i64;
1620
pub type Conf = u64;
1721
pub type Slot = u64;
1822

19-
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
23+
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
2024
pub struct ProductAccountMetadata {
2125
pub account: Pubkey,
2226
pub attr_dict: Attrs,
23-
pub price: Vec<PriceAccountMetadata>,
27+
pub price: Arc<[PriceAccountMetadata]>,
2428
}
2529

26-
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
30+
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
2731
pub struct PriceAccountMetadata {
2832
pub account: Pubkey,
29-
pub price_type: String,
33+
pub price_type: SmolStr,
3034
pub price_exponent: Exponent,
3135
}
3236

33-
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
37+
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
3438
pub struct ProductAccount {
3539
pub account: Pubkey,
3640
pub attr_dict: Attrs,
37-
pub price_accounts: Vec<PriceAccount>,
41+
pub price_accounts: Arc<[PriceAccount]>,
3842
}
3943

40-
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
44+
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
4145
pub struct PriceAccount {
4246
pub account: Pubkey,
43-
pub price_type: String,
47+
pub price_type: SmolStr,
4448
pub price_exponent: Exponent,
45-
pub status: String,
49+
pub status: SmolStr,
4650
pub price: Price,
4751
pub conf: Conf,
4852
pub twap: Price,
@@ -52,36 +56,36 @@ pub struct PriceAccount {
5256
pub prev_slot: Slot,
5357
pub prev_price: Price,
5458
pub prev_conf: Conf,
55-
pub publisher_accounts: Vec<PublisherAccount>,
59+
pub publisher_accounts: Arc<[PublisherAccount]>,
5660
}
5761

58-
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
62+
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
5963
pub struct PublisherAccount {
6064
pub account: Pubkey,
61-
pub status: String,
65+
pub status: SmolStr,
6266
pub price: Price,
6367
pub conf: Conf,
6468
pub slot: Slot,
6569
}
6670

67-
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
71+
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
6872
pub struct NotifyPrice {
6973
pub subscription: SubscriptionID,
7074
pub result: PriceUpdate,
7175
}
7276

73-
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
77+
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
7478
pub struct NotifyPriceSched {
7579
pub subscription: SubscriptionID,
7680
}
7781

7882
pub type SubscriptionID = i64;
7983

80-
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, PartialEq, Eq)]
84+
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq)]
8185
pub struct PriceUpdate {
8286
pub price: Price,
8387
pub conf: Conf,
84-
pub status: String,
88+
pub status: SmolStr,
8589
pub valid_slot: Slot,
8690
pub pub_slot: Slot,
8791
}

0 commit comments

Comments
 (0)