Revert "perf: reuse daily snapshot views in stock selection"

This reverts commit 1df0081479.
This commit is contained in:
boris
2026-09-05 00:24:05 +08:00
parent 1df0081479
commit 1f10a6bb3d
2 changed files with 36 additions and 138 deletions
+17 -52
View File
@@ -522,7 +522,6 @@ pub struct YieldCurvePoint {
#[derive(Debug, Clone)]
pub struct EligibleUniverseSnapshot {
pub symbol: String,
pub symbol_id: u32,
pub market_cap_bn: f64,
pub free_float_cap_bn: f64,
}
@@ -1358,7 +1357,8 @@ impl<'a, T> DailySymbolRows<'a, T> {
/// the already indexed slices once and keeps all lookups read-only.
pub(crate) struct DailySnapshotView<'a> {
market: DailySymbolRows<'a, DailyMarketSnapshot>,
factors: DailySymbolRows<'a, DailyFactorSnapshot>,
factor_rows: &'a [DailyFactorSnapshot],
factor_symbol_ids: &'a [u32],
candidates: DailySymbolRows<'a, CandidateEligibility>,
}
@@ -1371,16 +1371,12 @@ impl<'a> DailySnapshotView<'a> {
self.candidates.get(symbol_id)
}
pub(crate) fn factor(&self, symbol_id: u32) -> Option<&'a DailyFactorSnapshot> {
self.factors.get(symbol_id)
}
pub(crate) fn factor_rows(&self) -> &'a [DailyFactorSnapshot] {
self.factors.rows
self.factor_rows
}
pub(crate) fn factor_symbol_ids(&self) -> &'a [u32] {
self.factors.symbol_ids
self.factor_symbol_ids
}
}
@@ -1954,12 +1950,16 @@ impl DataSet {
&self.market_symbol_ids_by_date,
&self.market_row_positions_by_date,
),
factors: rows_on(
date,
&self.factor_by_date,
&self.factor_symbol_ids_by_date,
&self.factor_row_positions_by_date,
),
factor_rows: self
.factor_by_date
.get(&date)
.map(Vec::as_slice)
.unwrap_or(&[]),
factor_symbol_ids: self
.factor_symbol_ids_by_date
.get(&date)
.map(Vec::as_slice)
.unwrap_or(&[]),
candidates: rows_on(
date,
&self.candidate_by_date,
@@ -3650,25 +3650,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.market_by_date,
&self.symbol_id_by_code,
)
})
.get_or_init(|| build_eligible_universe(&self.factor_by_date, &self.market_by_date))
.get(&date)
.map(Vec::as_slice)
.unwrap_or(&[])
}
pub fn fundamental_universe_on(&self, date: NaiveDate) -> Vec<EligibleUniverseSnapshot> {
build_fundamental_universe_for_date(
date,
&self.factor_by_date,
&self.market_by_date,
&self.symbol_id_by_code,
)
build_fundamental_universe_for_date(date, &self.factor_by_date, &self.market_by_date)
}
pub fn eligible_universe_on_with_risk_config(
@@ -3682,7 +3671,6 @@ impl DataSet {
&self.candidate_by_date,
&self.market_by_date,
&self.instruments,
&self.symbol_id_by_code,
risk_config,
)
}
@@ -4462,17 +4450,11 @@ fn build_order_book_depth_index(
fn build_eligible_universe(
factor_by_date: &BTreeMap<NaiveDate, Vec<DailyFactorSnapshot>>,
market_by_date: &BTreeMap<NaiveDate, Vec<DailyMarketSnapshot>>,
symbol_id_by_code: &AHashMap<String, u32>,
) -> BTreeMap<NaiveDate, Vec<EligibleUniverseSnapshot>> {
let mut per_date = BTreeMap::<NaiveDate, Vec<EligibleUniverseSnapshot>>::new();
for date in factor_by_date.keys() {
let rows = build_fundamental_universe_for_date(
*date,
factor_by_date,
market_by_date,
symbol_id_by_code,
);
let rows = build_fundamental_universe_for_date(*date, factor_by_date, market_by_date);
per_date.insert(*date, rows);
}
@@ -4483,16 +4465,12 @@ fn build_fundamental_universe_for_date(
date: NaiveDate,
factor_by_date: &BTreeMap<NaiveDate, Vec<DailyFactorSnapshot>>,
market_by_date: &BTreeMap<NaiveDate, Vec<DailyMarketSnapshot>>,
symbol_id_by_code: &AHashMap<String, u32>,
) -> Vec<EligibleUniverseSnapshot> {
let mut rows = Vec::new();
let Some(factors) = factor_by_date.get(&date) else {
return rows;
};
for factor in factors {
let Some(symbol_id) = symbol_id_by_code.get(&factor.symbol).copied() else {
continue;
};
if market_by_date
.get(&date)
.and_then(|rows| find_by_symbol(rows, &factor.symbol, |row| row.symbol.as_str()))
@@ -4506,7 +4484,6 @@ fn build_fundamental_universe_for_date(
}
rows.push(EligibleUniverseSnapshot {
symbol: factor.symbol.clone(),
symbol_id,
market_cap_bn,
free_float_cap_bn: decision_free_float_cap_bn(factor),
});
@@ -4526,7 +4503,6 @@ fn build_eligible_universe_for_date(
candidate_by_date: &BTreeMap<NaiveDate, Vec<CandidateEligibility>>,
market_by_date: &BTreeMap<NaiveDate, Vec<DailyMarketSnapshot>>,
instruments: &HashMap<String, Instrument>,
symbol_id_by_code: &AHashMap<String, u32>,
risk_config: &FidcRiskControlConfig,
) -> Vec<EligibleUniverseSnapshot> {
factor_by_date
@@ -4538,7 +4514,6 @@ fn build_eligible_universe_for_date(
candidate_by_date,
market_by_date,
instruments,
symbol_id_by_code,
risk_config,
)
})
@@ -4551,14 +4526,10 @@ fn build_eligible_universe_for_date_from_factors(
candidate_by_date: &BTreeMap<NaiveDate, Vec<CandidateEligibility>>,
market_by_date: &BTreeMap<NaiveDate, Vec<DailyMarketSnapshot>>,
instruments: &HashMap<String, Instrument>,
symbol_id_by_code: &AHashMap<String, u32>,
risk_config: &FidcRiskControlConfig,
) -> Vec<EligibleUniverseSnapshot> {
let mut rows = Vec::new();
for factor in factors {
let Some(symbol_id) = symbol_id_by_code.get(&factor.symbol).copied() else {
continue;
};
if factor.market_cap_bn <= 0.0 || !factor.market_cap_bn.is_finite() {
continue;
}
@@ -4596,7 +4567,6 @@ fn build_eligible_universe_for_date_from_factors(
let free_float_cap_bn = decision_free_float_cap_bn(factor);
rows.push(EligibleUniverseSnapshot {
symbol: factor.symbol.clone(),
symbol_id,
market_cap_bn,
free_float_cap_bn,
});
@@ -5035,10 +5005,6 @@ mod tests {
.map(|row| row.symbol.as_str()),
Some(symbol)
);
assert_eq!(
day.factor(symbol_id).map(|row| row.symbol.as_str()),
Some(symbol)
);
assert_eq!(
data.candidate_by_symbol_id(date, symbol_id)
.map(|row| row.symbol.as_str()),
@@ -5058,7 +5024,6 @@ mod tests {
Some("000300.SH")
);
assert!(data.factor_by_symbol_id(date, signal_id).is_none());
assert!(day.factor(signal_id).is_none());
assert!(data.candidate_by_symbol_id(date, signal_id).is_none());
assert!(day.candidate(signal_id).is_none());
assert_eq!(