refactor: Hide private matcher members behind PImpl
This commit is contained in:
1 parent
cf19a63aac
commit
35d395fc35
3 files changed
+140
-88
No files matched your search
@@ -0,0 +1,18 @@
|
||||
#include "Matcher.h"
|
||||
|
||||
namespace bts::spartan::mdp3 {
|
||||
|
||||
template <class TIter>
|
||||
inline void MergedMatcher::init(TIter first, TIter last) {
|
||||
while (first != last) {
|
||||
add_def(*first);
|
||||
first++;
|
||||
}
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
inline void DirectMatcher::reset(SecurityID sec, Iter first, Iter last) {
|
||||
_matcher_map().try_emplace(sec).first->second.reset(first, last);
|
||||
}
|
||||
|
||||
} // namespace bts::spartan::mdp3
|
||||
@@ -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<OrderKey> used_order_keys;
|
||||
std::map<SecurityID, std::string> asset_map;
|
||||
std::map<std::string, FifoMatcherContext> 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<LegDef>{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 <int L> using MBP = std::array<BookLevel, L>;
|
||||
template <int L>
|
||||
using MBP = std::array<BookLevel, L>;
|
||||
|
||||
template <int L> inline bool mbp_equals(const MBP<L> &a, const MBP<L> &b) {
|
||||
template <int L>
|
||||
inline bool mbp_equals(const MBP<L> &a, const MBP<L> &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 <int L> inline bool mbp_equals(const MBP<L> &a, const MBP<L> &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<MDP3Order> orders,
|
||||
std::vector<MatchEffect> *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<MatchEffect> *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<SecurityID, MBPSnapshot>()).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<SimOrderUpdate>(*it));
|
||||
_me->trace.log_outgoing_update(std::get<SimOrderUpdate>(*it));
|
||||
for (auto it = res->begin() + effects_origin; it != res->end(); it++)
|
||||
MatcherUtils::validate_outgoing_update(std::get<SimOrderUpdate>(*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<SecurityID, FifoSecurityMatcher> 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<SecurityID> &inst_defs) {
|
||||
DirectMatcher::DirectMatcher(const std::vector<SecurityID> &inst_defs)
|
||||
: DirectMatcher() {
|
||||
for (const auto &sec_id : inst_defs)
|
||||
_matcher_map.insert(std::pair<SecurityID, FifoSecurityMatcher>(
|
||||
_me->matcher_map.insert(std::pair<SecurityID, FifoSecurityMatcher>(
|
||||
sec_id, FifoSecurityMatcher(sec_id)));
|
||||
}
|
||||
|
||||
DirectMatcher::~DirectMatcher() {}
|
||||
|
||||
void DirectMatcher::match(
|
||||
ChangeSrc,
|
||||
SmallOrderChangeVec const &changes,
|
||||
std::vector<MatchEffect> *res) {
|
||||
_match_count_this_per++;
|
||||
_match_count++;
|
||||
_me->match_count_this_per++;
|
||||
_me->match_count++;
|
||||
std::vector<std::pair<SecurityID, FifoSecurityMatcher *>> 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<SimOrderUpdate>(u));
|
||||
_me->trace.log_outgoing_update(std::get<SimOrderUpdate>(u));
|
||||
for (const auto &u : *res)
|
||||
MatcherUtils::validate_outgoing_update(std::get<SimOrderUpdate>(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<SecurityID, FifoSecurityMatcher> &DirectMatcher::_matcher_map() {
|
||||
return _me->matcher_map;
|
||||
}
|
||||
|
||||
} // namespace bts::spartan::mdp3
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "market_data/common/mdp3_common.h"
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
@@ -15,20 +16,9 @@ namespace bts::spartan::mdp3 {
|
||||
class SecurityDefinition;
|
||||
|
||||
class MergedMatcher {
|
||||
private:
|
||||
std::unordered_set<OrderKey> _used_order_keys;
|
||||
std::map<SecurityID, std::string> _asset_map;
|
||||
std::map<std::string, FifoMatcherContext> _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<SimBookUpdate, SimOrderUpdate>;
|
||||
|
||||
@@ -41,37 +31,31 @@ class MergedMatcher {
|
||||
BookSide book(SecurityID, Side);
|
||||
ImpliedBookSide implied_book(SecurityID, Side);
|
||||
|
||||
template <class TIter> void init(TIter first, TIter last) {
|
||||
while (first != last) {
|
||||
add_def(*first);
|
||||
first++;
|
||||
}
|
||||
}
|
||||
template <class TIter>
|
||||
void init(TIter first, TIter last);
|
||||
|
||||
void reset(
|
||||
SecurityID sec,
|
||||
std::vector<MDP3Order> orders,
|
||||
std::vector<MatchEffect> *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<MatchEffect> *effects = nullptr);
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> _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<SecurityID, FifoSecurityMatcher> _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<SecurityID> const &);
|
||||
~DirectMatcher();
|
||||
|
||||
using MatchEffect = std::variant<SimBookUpdate, SimOrderUpdate>;
|
||||
|
||||
@@ -83,10 +67,17 @@ class DirectMatcher {
|
||||
BookSide book(SecurityID, Side);
|
||||
ImpliedBookSide implied_book(SecurityID, Side);
|
||||
|
||||
template <class Iter> void reset(SecurityID sec, Iter first, Iter last) {
|
||||
_matcher_map.try_emplace(sec).first->second.reset(first, last);
|
||||
}
|
||||
template <class Iter>
|
||||
void reset(SecurityID sec, Iter first, Iter last);
|
||||
void clear();
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> _me;
|
||||
|
||||
std::map<SecurityID, FifoSecurityMatcher>& _matcher_map();
|
||||
};
|
||||
|
||||
} // namespace bts::spartan::mdp3
|
||||
|
||||
#include "Matcher.cc"
|
||||
Reference in new issue
Block a user