Optimise checking for stream_id membership in a strategy
Closed this issue · 0 comments
There is an opportunity for a small optimisation of BaseStrategy.stream_ids
. Apart from when acccessing BaseStrategy.info
(rare), this property is used to test whether a strategy is subscribed to an update (happens on each market update times the number of loaded strategies).
If BaseStrategy.historic_stream_ids
is converted to a set, the lookup becomes instantaneous as no iteration takes place. Sets use hashing for membership testing and since hash of an int (stream id) is the int itself, this is basically a zero-cost check. I've measured a 10-20x speedup over the current implementation when the list contains 100 stream ids, and there might be a lot more when backtesting. This optimisation does not work for live markets because streams ids can change on the fly, and creating a set is slower than creating a list.
Compared to overall time it takes to process a market book when backtesting, this optimisation results in a tiny speedup. However, considering a single backtesting session involves millions of stream id membership checks, it could shave off some seconds or even minutes off a single session, which is always welcome. More importantly, it changes the algorithmic complexity of the stream id lookup from O(n) to O(1).