diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cc b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cc new file mode 100644 index 000000000..8ccfc78d0 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cc @@ -0,0 +1,18 @@ +#include "Matcher.h" + +namespace bts::spartan::mdp3 { + +template +inline void MergedMatcher::init(TIter first, TIter last) { + while (first != last) { + add_def(*first); + first++; + } +} + +template +inline void DirectMatcher::reset(SecurityID sec, Iter first, Iter last) { + _matcher_map().try_emplace(sec).first->second.reset(first, last); +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cpp index a5b143253..fd621f635 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cpp @@ -10,16 +10,30 @@ using namespace google::protobuf::util; namespace bts::spartan::mdp3 { -MergedMatcher::MergedMatcher() - : _matcher_map(), _trace(), _match_count(0), _match_count_this_per(0), - _per_start_time(TimeUtil::GetCurrentTime()) {} +struct MergedMatcher::PImpl { + PImpl() + : matcher_map(), trace(), match_count(0), match_count_this_per(0), + per_start_time(TimeUtil::GetCurrentTime()) {} + + std::unordered_set used_order_keys; + std::map asset_map; + std::map matcher_map; + Trace trace; + size_t match_count; + size_t match_count_this_per; + google::protobuf::Timestamp per_start_time; +}; + +MergedMatcher::MergedMatcher() : _me(new MergedMatcher::PImpl()) {} + +MergedMatcher::~MergedMatcher() {} void MergedMatcher::add_def(const SecurityDefinition &def) { std::string ass(def.asset()); - _asset_map.emplace(def.security_id(), ass); - auto m = _matcher_map.find(ass); - if (m == _matcher_map.end()) - m = _matcher_map.emplace(ass, FifoMatcherContext({true})).first; + _me->asset_map.emplace(def.security_id(), ass); + auto m = _me->matcher_map.find(ass); + if (m == _me->matcher_map.end()) + m = _me->matcher_map.emplace(ass, FifoMatcherContext({true})).first; m->second.add_security( def.security_id(), std::string(def.symbol()), std::vector{def.legs().begin(), def.legs().end()}); @@ -27,13 +41,15 @@ void MergedMatcher::add_def(const SecurityDefinition &def) { } std::string MergedMatcher::_get_asset(SecurityID sec) const { - auto it = _asset_map.find(sec); - return it == _asset_map.end() ? std::string() : it->second; + auto it = _me->asset_map.find(sec); + return it == _me->asset_map.end() ? std::string() : it->second; } -template using MBP = std::array; +template +using MBP = std::array; -template inline bool mbp_equals(const MBP &a, const MBP &b) { +template +inline bool mbp_equals(const MBP &a, const MBP &b) { for (uint32_t i = 0; i < a.size(); i++) { if (a[i].price != b[i].price || a[i].quantity != b[i].quantity) return false; @@ -46,8 +62,7 @@ template inline bool mbp_equals(const MBP &a, const MBP &b) { struct MBPData { MBPData() {} MBPData(FifoMergedSecurityMatcher &mat) - : bids(mat.book(Side::Buy)), - asks(mat.book(Side::Sell)), + : bids(mat.book(Side::Buy)), asks(mat.book(Side::Sell)), ibids(mat.implied_book(Side::Buy)), iasks(mat.implied_book(Side::Sell)) {} BookSide bids, asks; @@ -89,24 +104,34 @@ struct MBPSnapshot { FifoMatcherContext & MergedMatcher::_asset_matcher(SecurityID sec, bool add_if_missing) { - auto ass_it = _asset_map.find(sec); - if (add_if_missing && ass_it == _asset_map.end()) - ass_it = _asset_map.emplace(sec, std::string()).first; - bts_assert(ass_it != _asset_map.end()); - auto mat_it = _matcher_map.find(ass_it->second); - if (add_if_missing && mat_it == _matcher_map.end()) - mat_it = _matcher_map.emplace(ass_it->second, FifoMatcherContext({true})) - .first; - bts_assert(mat_it != _matcher_map.end()); + auto ass_it = _me->asset_map.find(sec); + if (add_if_missing && ass_it == _me->asset_map.end()) + ass_it = _me->asset_map.emplace(sec, std::string()).first; + bts_assert(ass_it != _me->asset_map.end()); + auto mat_it = _me->matcher_map.find(ass_it->second); + if (add_if_missing && mat_it == _me->matcher_map.end()) + mat_it = + _me->matcher_map.emplace(ass_it->second, FifoMatcherContext({true})) + .first; + bts_assert(mat_it != _me->matcher_map.end()); return mat_it->second; } +void MergedMatcher::reset( + SecurityID sec, + std::vector orders, + std::vector *effects) { + for (const auto &o : orders) + _me->used_order_keys.emplace(o.key); + _asset_matcher(sec).reset(sec, orders.begin(), orders.end(), effects); +} + void MergedMatcher::match( ChangeSrc, SmallOrderChangeVec const &changes, std::vector *res) { - _match_count_this_per++; - _match_count++; + _me->match_count_this_per++; + _me->match_count++; // The caller is not obligated to pass an empty vector. Consider an event // that involves multiple securities; perhaps the caller wants to put the // effects for each security in the same vector. The elements that were @@ -126,23 +151,23 @@ void MergedMatcher::match( it = mbps.emplace(ass, std::map()).first; auto mbp_tup_it = it->second.find(c.order.security); if (mbp_tup_it == it->second.end()) { - auto *mat = _matcher_map[ass].get_matcher(c.order.security); + auto *mat = _me->matcher_map[ass].get_matcher(c.order.security); bts_assert(mat != nullptr); it->second.emplace(c.order.security, MBPSnapshot(mat)); } } for (const auto &c : changes) { - _trace.log_incoming_change(c); + _me->trace.log_incoming_change(c); MatcherUtils::validate_incoming_order_change(c); switch (c.change) { case OrderChange::ChangeType::added: - bts_assert(_used_order_keys.emplace(c.order.key).second); + bts_assert(_me->used_order_keys.emplace(c.order.key).second); break; case OrderChange::ChangeType::updated: case OrderChange::ChangeType::updated_price: case OrderChange::ChangeType::removed: - bts_assert(_used_order_keys.count(c.order.key)); + bts_assert(_me->used_order_keys.count(c.order.key)); break; } // this holds individual fills, meaning one aggr fill per price, @@ -155,7 +180,7 @@ void MergedMatcher::match( // log/validate order updates if (res != nullptr) { for (auto it = res->begin() + effects_origin; it != res->end(); it++) - _trace.log_outgoing_update(std::get(*it)); + _me->trace.log_outgoing_update(std::get(*it)); for (auto it = res->begin() + effects_origin; it != res->end(); it++) MatcherUtils::validate_outgoing_update(std::get(*it)); // add mbp updates @@ -172,13 +197,13 @@ void MergedMatcher::match( } auto now = TimeUtil::GetCurrentTime(); auto usecs = TimeUtil::TimestampToMicroseconds(now) - - TimeUtil::TimestampToMicroseconds(_per_start_time); + TimeUtil::TimestampToMicroseconds(_me->per_start_time); if (usecs >= 1000000) { LOG(OrderMan, Diag, "usecs:{} events processed:{} tot events processed:{}", usecs, - _match_count_this_per, _match_count); - _match_count_this_per = 0; - _per_start_time = now; + _me->match_count_this_per, _me->match_count); + _me->match_count_this_per = 0; + _me->per_start_time = now; } } @@ -191,70 +216,88 @@ ImpliedBookSide MergedMatcher::implied_book(SecurityID id, Side s) { } void MergedMatcher::clear() { - for (auto &mp : _matcher_map) + for (auto &mp : _me->matcher_map) mp.second.clear(); } +struct DirectMatcher::PImpl { + PImpl() + : matcher_map(), trace(), match_count(0), match_count_this_per(0), + per_start_time(TimeUtil::GetCurrentTime()) {} + + std::map matcher_map; + Trace trace; + size_t match_count; + size_t match_count_this_per; + google::protobuf::Timestamp per_start_time; +}; + ///////////////// // DirectMatcher //////////////// -DirectMatcher::DirectMatcher() - : _matcher_map(), _trace(), _match_count(0), _match_count_this_per(0), - _per_start_time(TimeUtil::GetCurrentTime()) {} +DirectMatcher::DirectMatcher() : _me(new DirectMatcher::PImpl()) {} -DirectMatcher::DirectMatcher(const std::vector &inst_defs) { +DirectMatcher::DirectMatcher(const std::vector &inst_defs) + : DirectMatcher() { for (const auto &sec_id : inst_defs) - _matcher_map.insert(std::pair( + _me->matcher_map.insert(std::pair( sec_id, FifoSecurityMatcher(sec_id))); } +DirectMatcher::~DirectMatcher() {} + void DirectMatcher::match( ChangeSrc, SmallOrderChangeVec const &changes, std::vector *res) { - _match_count_this_per++; - _match_count++; + _me->match_count_this_per++; + _me->match_count++; std::vector> dirty; - dirty.reserve(_matcher_map.size()); + dirty.reserve(_me->matcher_map.size()); for (const auto &c : changes) { - auto it = _matcher_map.find(c.order.security); - bts_assert(it != _matcher_map.end()); + auto it = _me->matcher_map.find(c.order.security); + bts_assert(it != _me->matcher_map.end()); auto &sec_matcher = it->second; - _trace.log_incoming_change(c); + _me->trace.log_incoming_change(c); MatcherUtils::validate_incoming_order_change(c); sec_matcher.on_order_change(c, res); dirty.emplace_back(c.order.security, &it->second); } for (const auto &u : *res) - _trace.log_outgoing_update(std::get(u)); + _me->trace.log_outgoing_update(std::get(u)); for (const auto &u : *res) MatcherUtils::validate_outgoing_update(std::get(u)); // all order activity is not reflecting in the // books for (auto cm : dirty) res->emplace_back(SimBookUpdate{ - cm.first, cm.second->book(Side::Buy), - cm.second->book(Side::Sell), ImpliedBookSide(), ImpliedBookSide()}); + cm.first, cm.second->book(Side::Buy), cm.second->book(Side::Sell), + ImpliedBookSide(), ImpliedBookSide()}); auto now = TimeUtil::GetCurrentTime(); auto usecs = TimeUtil::TimestampToMicroseconds(now) - - TimeUtil::TimestampToMicroseconds(_per_start_time); + TimeUtil::TimestampToMicroseconds(_me->per_start_time); if (usecs >= 1000000) { LOG(OrderMan, Diag, "usecs:{} events processed:{} tot events processed:{}", usecs, - _match_count_this_per, _match_count); - _match_count_this_per = 0; - _per_start_time = now; + _me->match_count_this_per, _me->match_count); + _me->match_count_this_per = 0; + _me->per_start_time = now; } } BookSide DirectMatcher::book(SecurityID id, Side s) { - auto it = _matcher_map.find(id); - bts_assert(it != _matcher_map.end()); + auto it = _me->matcher_map.find(id); + bts_assert(it != _me->matcher_map.end()); return it->second.book(s); } void DirectMatcher::clear() { - for (auto &p : _matcher_map) + for (auto &p : _me->matcher_map) p.second.clear(); } + +std::map &DirectMatcher::_matcher_map() { + return _me->matcher_map; +} + } // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.h index bb44fea44..db4ad9014 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.h @@ -7,6 +7,7 @@ #include "market_data/common/mdp3_common.h" #include #include +#include #include #include @@ -15,20 +16,9 @@ namespace bts::spartan::mdp3 { class SecurityDefinition; class MergedMatcher { - private: - std::unordered_set _used_order_keys; - std::map _asset_map; - std::map _matcher_map; - Trace _trace; - size_t _match_count; - size_t _match_count_this_per; - google::protobuf::Timestamp _per_start_time; - std::string _get_asset(SecurityID sec) const; - FifoMatcherContext & - _asset_matcher(SecurityID, bool add_if_missing = true); - public: MergedMatcher(); + ~MergedMatcher(); using MatchEffect = std::variant; @@ -41,37 +31,31 @@ class MergedMatcher { BookSide book(SecurityID, Side); ImpliedBookSide implied_book(SecurityID, Side); - template void init(TIter first, TIter last) { - while (first != last) { - add_def(*first); - first++; - } - } + template + void init(TIter first, TIter last); void reset( SecurityID sec, std::vector orders, - std::vector *effects = nullptr) { - for (const auto &o : orders) - _used_order_keys.emplace(o.key); - _asset_matcher(sec).reset(sec, orders.begin(), orders.end(), effects); - } + std::vector *effects = nullptr); + void clear(); + + private: + struct PImpl; + std::unique_ptr _me; + + std::string _get_asset(SecurityID sec) const; + FifoMatcherContext &_asset_matcher(SecurityID, bool add_if_missing = true); }; // no implied matching (implied integration perf isn't great, // need to investigate before DirectMatcher can be retired). class DirectMatcher { - private: - std::map _matcher_map; - Trace _trace; - size_t _match_count; - size_t _match_count_this_per; - google::protobuf::Timestamp _per_start_time; - public: DirectMatcher(); DirectMatcher(std::vector const &); + ~DirectMatcher(); using MatchEffect = std::variant; @@ -83,10 +67,17 @@ class DirectMatcher { BookSide book(SecurityID, Side); ImpliedBookSide implied_book(SecurityID, Side); - template void reset(SecurityID sec, Iter first, Iter last) { - _matcher_map.try_emplace(sec).first->second.reset(first, last); - } + template + void reset(SecurityID sec, Iter first, Iter last); void clear(); + + private: + struct PImpl; + std::unique_ptr _me; + + std::map& _matcher_map(); }; } // namespace bts::spartan::mdp3 + +#include "Matcher.cc"