perf: intern stock-state symbols
This commit is contained in:
@@ -1327,6 +1327,7 @@ pub struct DataSet {
|
||||
market_series_end_positions_by_symbol_id: Arc<Option<SymbolSeriesEndPositionIndex>>,
|
||||
benchmark_series_cache: Arc<BenchmarkPriceSeries>,
|
||||
symbol_id_by_code: Arc<AHashMap<String, u32>>,
|
||||
symbol_by_id: Arc<Vec<Arc<str>>>,
|
||||
eligible_universe_by_date: Arc<OnceLock<BTreeMap<NaiveDate, Vec<EligibleUniverseSnapshot>>>>,
|
||||
benchmark_code: String,
|
||||
futures_params_by_symbol: Arc<HashMap<String, Vec<FuturesTradingParameter>>>,
|
||||
@@ -1771,6 +1772,10 @@ impl DataSet {
|
||||
&factor_by_date,
|
||||
&candidate_by_date,
|
||||
);
|
||||
let mut symbol_by_id = vec![Arc::<str>::from(""); symbol_id_by_code.len()];
|
||||
for (symbol, symbol_id) in &symbol_id_by_code {
|
||||
symbol_by_id[*symbol_id as usize] = Arc::<str>::from(symbol.as_str());
|
||||
}
|
||||
let mut instruments_by_symbol_id = vec![None; symbol_id_by_code.len()];
|
||||
for (symbol, instrument) in &instruments {
|
||||
if let Some(symbol_id) = symbol_id_by_code.get(symbol).copied() {
|
||||
@@ -1859,6 +1864,7 @@ impl DataSet {
|
||||
),
|
||||
benchmark_series_cache: Arc::new(benchmark_series_cache),
|
||||
symbol_id_by_code: Arc::new(symbol_id_by_code),
|
||||
symbol_by_id: Arc::new(symbol_by_id),
|
||||
eligible_universe_by_date: Arc::new(OnceLock::new()),
|
||||
benchmark_code,
|
||||
futures_params_by_symbol: Arc::new(futures_params_by_symbol),
|
||||
@@ -1912,6 +1918,11 @@ impl DataSet {
|
||||
self.symbol_id_by_code.get(symbol).copied()
|
||||
}
|
||||
|
||||
pub(crate) fn shared_symbol_by_id(&self, symbol_id: u32) -> Option<Arc<str>> {
|
||||
let symbol = self.symbol_by_id.get(symbol_id as usize)?;
|
||||
(!symbol.is_empty()).then(|| Arc::clone(symbol))
|
||||
}
|
||||
|
||||
pub(crate) fn symbol_count(&self) -> usize {
|
||||
self.symbol_id_by_code.len()
|
||||
}
|
||||
@@ -5047,6 +5058,10 @@ mod tests {
|
||||
.map(|row| row.symbol.as_str()),
|
||||
Some(symbol)
|
||||
);
|
||||
let first_shared = data.shared_symbol_by_id(symbol_id).expect("shared symbol");
|
||||
let second_shared = data.shared_symbol_by_id(symbol_id).expect("shared symbol");
|
||||
assert_eq!(first_shared.as_ref(), symbol);
|
||||
assert!(Arc::ptr_eq(&first_shared, &second_shared));
|
||||
assert_eq!(
|
||||
data.market_by_symbol_id(date, symbol_id)
|
||||
.map(|row| row.symbol.as_str()),
|
||||
|
||||
Reference in New Issue
Block a user