优化回测数据集内存并修复rolling依赖识别

This commit is contained in:
boris
2026-06-21 03:31:45 +08:00
parent 7f809fd875
commit 2f62d82420
2 changed files with 106 additions and 10 deletions
+15 -10
View File
@@ -1,7 +1,7 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fs;
use std::path::Path;
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
use chrono::{NaiveDate, NaiveDateTime};
use serde::{Deserialize, Serialize};
@@ -997,7 +997,7 @@ pub struct DataSet {
benchmark_by_date: BTreeMap<NaiveDate, BenchmarkSnapshot>,
market_series_by_symbol: HashMap<String, SymbolPriceSeries>,
benchmark_series_cache: BenchmarkPriceSeries,
eligible_universe_by_date: BTreeMap<NaiveDate, Vec<EligibleUniverseSnapshot>>,
eligible_universe_by_date: Arc<OnceLock<BTreeMap<NaiveDate, Vec<EligibleUniverseSnapshot>>>>,
benchmark_code: String,
futures_params_by_symbol: HashMap<String, Vec<FuturesTradingParameter>>,
}
@@ -1303,12 +1303,6 @@ impl DataSet {
let market_series_by_symbol = build_market_series(&market_by_date);
let benchmark_series_cache =
BenchmarkPriceSeries::new(&benchmark_by_date.values().cloned().collect::<Vec<_>>());
let eligible_universe_by_date = build_eligible_universe(
&factor_by_date,
&candidate_index,
&market_index,
&instruments,
);
let futures_params_by_symbol = build_futures_params_index(futures_params);
Ok(Self {
@@ -1328,7 +1322,7 @@ impl DataSet {
benchmark_by_date,
market_series_by_symbol,
benchmark_series_cache,
eligible_universe_by_date,
eligible_universe_by_date: Arc::new(OnceLock::new()),
benchmark_code,
futures_params_by_symbol,
})
@@ -2625,6 +2619,14 @@ impl DataSet {
pub fn eligible_universe_on(&self, date: NaiveDate) -> &[EligibleUniverseSnapshot] {
self.eligible_universe_by_date
.get_or_init(|| {
build_eligible_universe(
&self.factor_by_date,
&self.candidate_index,
&self.market_index,
&self.instruments,
)
})
.get(&date)
.map(Vec::as_slice)
.unwrap_or(&[])
@@ -3595,7 +3597,10 @@ fn build_market_series(
let mut grouped = HashMap::<String, Vec<&DailyMarketSnapshot>>::new();
for rows in market_by_date.values() {
for row in rows {
grouped.entry(row.symbol.clone()).or_default().push(row.as_ref());
grouped
.entry(row.symbol.clone())
.or_default()
.push(row.as_ref());
}
}