Skip to content

"Vibe-Trading: Your Personal Trading Agent"

perf(backend): sort+dedup date merge in _align instead of np.unique

cgycorey7af1de54617f
diff --git a/agent/backtest/engines/base.py b/agent/backtest/engines/base.py
index 2c36ecdd1..6a8ec03e9 100644
--- a/agent/backtest/engines/base.py
+++ b/agent/backtest/engines/base.py
@@ -166,9 +166,17 @@ def _align(
Returns:
(dates, close_df, positions_df, returns_df)
"""
- # Build unified sorted date index from all symbols' trading calendars
+ # Build unified sorted date index from all symbols' trading calendars.
+ # sort+dedup instead of np.unique: unique's hash-based path costs ~15ms
+ # for a 250k-element merge of 50 calendars; sort+dedup is ~5ms and the
+ # sorted result is bit-identical (same values, same order).
indexes = [data_map[c].index for c in codes]
- merged = np.unique(np.concatenate([index.asi8 for index in indexes]))
+ merged = np.concatenate([index.asi8 for index in indexes])
+ merged.sort()
+ keep = np.empty(merged.shape[0], dtype=bool)
+ keep[0] = True
+ np.not_equal(merged[1:], merged[:-1], out=keep[1:])
+ merged = merged[keep]
common_tz = indexes[0].tz
if all(index.tz == common_tz for index in indexes) and common_tz is not None:
dates = pd.DatetimeIndex(pd.to_datetime(merged, utc=True).tz_convert(common_tz))