用快速哈希优化回测内部索引

This commit is contained in:
boris
2026-08-24 12:09:09 +08:00
parent 1d7ac19886
commit c52478708f
5 changed files with 40 additions and 26 deletions
+14 -8
View File
@@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::{Arc, OnceLock};
use ahash::AHashMap;
use chrono::{NaiveDate, NaiveDateTime};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
@@ -1136,12 +1137,12 @@ pub struct DataSet {
execution_quotes_by_date: HashMap<NaiveDate, HashMap<String, Vec<IntradayExecutionQuote>>>,
order_book_depth_index: HashMap<(NaiveDate, String), Vec<IntradayOrderBookDepthLevel>>,
benchmark_by_date: BTreeMap<NaiveDate, BenchmarkSnapshot>,
market_series_by_symbol: Arc<HashMap<String, Arc<SymbolPriceSeries>>>,
adjusted_close_series_by_symbol: Arc<HashMap<String, Arc<AdjustedCloseSeries>>>,
market_series_by_symbol: Arc<AHashMap<String, Arc<SymbolPriceSeries>>>,
adjusted_close_series_by_symbol: Arc<AHashMap<String, Arc<AdjustedCloseSeries>>>,
market_series_by_symbol_id: Arc<Vec<Option<Arc<SymbolPriceSeries>>>>,
adjusted_close_series_by_symbol_id: Arc<Vec<Option<Arc<AdjustedCloseSeries>>>>,
benchmark_series_cache: BenchmarkPriceSeries,
symbol_id_by_code: Arc<HashMap<String, u32>>,
symbol_id_by_code: Arc<AHashMap<String, u32>>,
eligible_universe_by_date: Arc<OnceLock<BTreeMap<NaiveDate, Vec<EligibleUniverseSnapshot>>>>,
benchmark_code: String,
futures_params_by_symbol: HashMap<String, Vec<FuturesTradingParameter>>,
@@ -1305,27 +1306,32 @@ 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::<String, Vec<&DailyMarketSnapshot>>::new();
let mut market_rows_by_symbol = AHashMap::<String, Vec<&DailyMarketSnapshot>>::new();
for row in &market {
market_rows_by_symbol
.entry(row.symbol.clone())
.or_default()
.push(row.as_ref());
}
let market_rows_by_symbol = market_rows_by_symbol.into_iter().collect::<Vec<_>>();
let market_series_by_symbol = market_rows_by_symbol
.into_par_iter()
.map(|(symbol, rows)| {
let series = Arc::new(SymbolPriceSeries::new(symbol.clone(), rows));
(symbol, series)
})
.collect::<HashMap<_, _>>();
.collect::<Vec<_>>()
.into_iter()
.collect::<AHashMap<_, _>>();
let adjusted_close_series_by_symbol = market_series_by_symbol
.par_iter()
.filter_map(|(symbol, market)| {
AdjustedCloseSeries::new(market, &factor_by_date)
.map(|series| (symbol.clone(), Arc::new(series)))
})
.collect::<HashMap<_, _>>();
.collect::<Vec<_>>()
.into_iter()
.collect::<AHashMap<_, _>>();
let factor_texts = factor_texts
.into_iter()
.filter_map(|mut item| {
@@ -3298,7 +3304,7 @@ fn build_symbol_id_index(
market_by_date: &BTreeMap<NaiveDate, Vec<Arc<DailyMarketSnapshot>>>,
factor_by_date: &BTreeMap<NaiveDate, Vec<Arc<DailyFactorSnapshot>>>,
candidate_by_date: &BTreeMap<NaiveDate, Vec<Arc<CandidateEligibility>>>,
) -> HashMap<String, u32> {
) -> AHashMap<String, u32> {
let mut symbols = instruments.keys().cloned().collect::<HashSet<_>>();
for rows in market_by_date.values() {
for row in rows {
@@ -3337,7 +3343,7 @@ fn build_symbol_id_index(
fn build_group_symbol_ids<T, F>(
groups: &BTreeMap<NaiveDate, Vec<Arc<T>>>,
symbol_id_by_code: &HashMap<String, u32>,
symbol_id_by_code: &AHashMap<String, u32>,
symbol_of: F,
) -> BTreeMap<NaiveDate, Vec<u32>>
where