From 2a4a9d1290d6eab940c7b853e7750991417e9fd1 Mon Sep 17 00:00:00 2001 From: boris Date: Sat, 1 Aug 2026 21:34:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9E=84=E5=BB=BA=E6=97=A0=E9=94=81=E8=AF=81?= =?UTF-8?q?=E5=88=B8=E5=BA=8F=E5=88=97=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/fidc-core/src/data.rs | 85 ++++++++++++------------------------ 1 file changed, 28 insertions(+), 57 deletions(-) diff --git a/crates/fidc-core/src/data.rs b/crates/fidc-core/src/data.rs index 3a554e8..d7a33c2 100644 --- a/crates/fidc-core/src/data.rs +++ b/crates/fidc-core/src/data.rs @@ -1,5 +1,5 @@ use std::collections::{BTreeMap, HashMap, HashSet}; -use std::sync::{Arc, OnceLock, RwLock}; +use std::sync::{Arc, OnceLock}; use chrono::{NaiveDate, NaiveDateTime}; use serde::{Deserialize, Serialize}; @@ -1052,8 +1052,8 @@ pub struct DataSet { execution_quotes_by_date: HashMap>>, order_book_depth_index: HashMap<(NaiveDate, String), Vec>, benchmark_by_date: BTreeMap, - market_series_by_symbol: Arc>>>, - adjusted_close_series_by_symbol: Arc>>>, + market_series_by_symbol: Arc>>, + adjusted_close_series_by_symbol: Arc>>, benchmark_series_cache: BenchmarkPriceSeries, eligible_universe_by_date: Arc>>>, source_daily_volume_contract_symbols: HashSet, @@ -1242,6 +1242,27 @@ impl DataSet { let mut factor_by_date = group_arc_by_date(&factors, |item| item.date); sort_arc_groups_by_symbol(&mut factor_by_date, |item| item.symbol.as_str()); + let mut market_rows_by_symbol = HashMap::>::new(); + for row in &market { + market_rows_by_symbol + .entry(row.symbol.clone()) + .or_default() + .push(row.as_ref()); + } + let market_series_by_symbol = market_rows_by_symbol + .into_iter() + .map(|(symbol, rows)| { + let series = Arc::new(SymbolPriceSeries::new(symbol.clone(), rows)); + (symbol, series) + }) + .collect::>(); + let adjusted_close_series_by_symbol = market_series_by_symbol + .iter() + .filter_map(|(symbol, market)| { + AdjustedCloseSeries::new(market, &factor_by_date) + .map(|series| (symbol.clone(), Arc::new(series))) + }) + .collect::>(); let factor_texts = factor_texts .into_iter() .filter_map(|mut item| { @@ -1285,8 +1306,8 @@ impl DataSet { execution_quotes_by_date, order_book_depth_index, benchmark_by_date, - market_series_by_symbol: Arc::new(RwLock::new(HashMap::new())), - adjusted_close_series_by_symbol: Arc::new(RwLock::new(HashMap::new())), + market_series_by_symbol: Arc::new(market_series_by_symbol), + adjusted_close_series_by_symbol: Arc::new(adjusted_close_series_by_symbol), benchmark_series_cache, eligible_universe_by_date: Arc::new(OnceLock::new()), source_daily_volume_contract_symbols, @@ -1340,61 +1361,11 @@ impl DataSet { } fn market_series(&self, symbol: &str) -> Option> { - if let Some(series) = self - .market_series_by_symbol - .read() - .expect("market series cache lock poisoned") - .get(symbol) - .cloned() - { - return Some(series); - } - - let rows = self - .market_by_date - .values() - .filter_map(|day_rows| find_arc_by_symbol(day_rows, symbol, |row| row.symbol.as_str())) - .collect::>(); - if rows.is_empty() { - return None; - } - - let series = Arc::new(SymbolPriceSeries::new(symbol.to_string(), rows)); - let mut cache = self - .market_series_by_symbol - .write() - .expect("market series cache lock poisoned"); - Some( - cache - .entry(symbol.to_string()) - .or_insert_with(|| Arc::clone(&series)) - .clone(), - ) + self.market_series_by_symbol.get(symbol).cloned() } fn adjusted_close_series(&self, symbol: &str) -> Option> { - if let Some(series) = self - .adjusted_close_series_by_symbol - .read() - .expect("adjusted close series cache lock poisoned") - .get(symbol) - .cloned() - { - return Some(series); - } - - let market = self.market_series(symbol)?; - let series = Arc::new(AdjustedCloseSeries::new(&market, &self.factor_by_date)?); - let mut cache = self - .adjusted_close_series_by_symbol - .write() - .expect("adjusted close series cache lock poisoned"); - Some( - cache - .entry(symbol.to_string()) - .or_insert_with(|| Arc::clone(&series)) - .clone(), - ) + self.adjusted_close_series_by_symbol.get(symbol).cloned() } pub fn factor(&self, date: NaiveDate, symbol: &str) -> Option<&DailyFactorSnapshot> {