refactor: Use UnorderedSetMap for ref counts
This commit is contained in:
1 parent
fbeb65b53b
commit
cc85c658d9
4 files changed
+121
-29
No files matched your search
@@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace bts::collections {
|
||||
|
||||
template <
|
||||
class Key,
|
||||
class Value,
|
||||
class KeyHash = std::hash<Key>,
|
||||
class ValueHash = std::hash<Value>>
|
||||
class UnorderedSetMap {
|
||||
public:
|
||||
using Values = std::unordered_set<Value, ValueHash>;
|
||||
using Data = std::unordered_map<Key, Values, KeyHash>;
|
||||
template <typename T, typename... Ts>
|
||||
using AllSame = std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>>;
|
||||
|
||||
std::size_t size() const;
|
||||
std::size_t size(Key key) const;
|
||||
|
||||
template <typename... ValuesToAdd, typename = AllSame<Value, ValuesToAdd...>>
|
||||
std::tuple<typename Data::iterator, bool, std::size_t> add(
|
||||
Key key, ValuesToAdd... values);
|
||||
|
||||
template <
|
||||
typename... ValuesToRemove,
|
||||
typename = AllSame<Value, ValuesToRemove...>>
|
||||
std::tuple<typename Data::iterator, bool, std::size_t> remove(
|
||||
Key key, ValuesToRemove... values);
|
||||
|
||||
private:
|
||||
Data data_;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
template <class Key, class Value, class KeyHash, class ValueHash>
|
||||
std::size_t UnorderedSetMap<Key, Value, KeyHash, ValueHash>::size() const {
|
||||
return data_.size();
|
||||
}
|
||||
|
||||
template <class Key, class Value, class KeyHash, class ValueHash>
|
||||
std::size_t UnorderedSetMap<Key, Value, KeyHash, ValueHash>::size(
|
||||
Key key) const {
|
||||
auto it = data_.find(key);
|
||||
return it == data_.end() ? 0 : it->second.size();
|
||||
}
|
||||
|
||||
template <class Key, class Value, class KeyHash, class ValueHash>
|
||||
template <typename... ValuesToAdd, typename AllSame>
|
||||
std::tuple<
|
||||
typename UnorderedSetMap<Key, Value, KeyHash, ValueHash>::Data::iterator,
|
||||
bool,
|
||||
std::size_t>
|
||||
UnorderedSetMap<Key, Value, KeyHash, ValueHash>::add(
|
||||
Key key, ValuesToAdd... values) {
|
||||
auto [it, is_new] = data_.try_emplace(key);
|
||||
auto &val_set = it->second;
|
||||
auto sz_before = val_set.size();
|
||||
[](...) {}((val_set.insert(values))...);
|
||||
std::size_t n_added = val_set.size() - sz_before;
|
||||
return std::make_tuple(it, is_new, n_added);
|
||||
}
|
||||
|
||||
template <class Key, class Value, class KeyHash, class ValueHash>
|
||||
template <typename... ValuesToRemove, typename AllSame>
|
||||
std::tuple<
|
||||
typename UnorderedSetMap<Key, Value, KeyHash, ValueHash>::Data::iterator,
|
||||
bool,
|
||||
std::size_t>
|
||||
UnorderedSetMap<Key, Value, KeyHash, ValueHash>::remove(
|
||||
Key key, ValuesToRemove... values) {
|
||||
auto it = data_.find(key);
|
||||
if (it == data_.end()) {
|
||||
return std::make_tuple(it, false, std::size_t(0));
|
||||
}
|
||||
auto &val_set = it->second;
|
||||
auto sz_before = val_set.size();
|
||||
[](...) {}((val_set.erase(values))...);
|
||||
std::size_t n_removed = val_set.size() - sz_before;
|
||||
bool remove_key = val_set.empty();
|
||||
if (remove_key) {
|
||||
data_.erase(it);
|
||||
it = data_.end();
|
||||
}
|
||||
return std::make_tuple(it, remove_key, n_removed);
|
||||
}
|
||||
|
||||
} // namespace bts::collections
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "MarketDataRegistration.h"
|
||||
#include "lib/api/md/MarketDataSubscriptionParams.pb.h"
|
||||
#include "lib/api/md/binance/BinanceMarketDataSubscriptionParams.pb.h"
|
||||
#include "lib/collections/UnorderedSetMap.h"
|
||||
#include "lib/core/ProtoPrinter.h"
|
||||
#include "lib/domain/SecurityID.h"
|
||||
#include "lib/domain/SecurityResolver.h"
|
||||
@@ -61,7 +62,8 @@ struct MarketDataGateway::PImpl {
|
||||
binance::BinanceDepthStreamSiteKey,
|
||||
binance::BinanceDepthStreamSite>
|
||||
binance_sites;
|
||||
std::unordered_map<domain::SecurityID, std::uint32_t> sec_sub_ref_counts;
|
||||
collections::UnorderedSetMap<domain::SecurityID, MarketDataListenerID>
|
||||
sec_listeners;
|
||||
std::unordered_map<domain::SecurityID, SecData> sec_data_map;
|
||||
std::unordered_map<MarketDataListenerID, MarketDataListenerData>
|
||||
registered_listeners;
|
||||
@@ -83,14 +85,14 @@ struct MarketDataGateway::PImpl {
|
||||
binance::BinanceDepthStreamSite &get_binance_site(
|
||||
const binance::BinanceDepthStreamSiteKey &k) {
|
||||
return binance_sites
|
||||
.try_emplace(k, k, &sec_sub_ref_counts, ®istered_listeners)
|
||||
.try_emplace(k, k, &sec_listeners, ®istered_listeners)
|
||||
.first->second;
|
||||
}
|
||||
|
||||
std::pair<binance::BinanceDepthStreamSite *, bool> try_emplace_binance_site(
|
||||
const binance::BinanceDepthStreamSiteKey &k) {
|
||||
auto p = binance_sites.try_emplace(
|
||||
k, k, &sec_sub_ref_counts, ®istered_listeners);
|
||||
k, k, &sec_listeners, ®istered_listeners);
|
||||
return {&p.first->second, p.second};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "BinanceDepthStreamSite.h"
|
||||
#include "lib/collections/UnorderedSetMap.h"
|
||||
#include "lib/api/md/binance/BinanceMarketDataSubscriptionParams.pb.h"
|
||||
#include "lib/domain/SecurityID.h"
|
||||
#include "lib/domain/SecurityResolver.h"
|
||||
@@ -20,20 +21,20 @@ namespace bts::md::binance {
|
||||
|
||||
BinanceDepthStreamSite::BinanceDepthStreamSite(
|
||||
BinanceDepthStreamSiteKey site_key,
|
||||
std::unordered_map<domain::SecurityID, std::uint32_t> *ref_counts_ptr,
|
||||
collections::UnorderedSetMap<domain::SecurityID, MarketDataListenerID> *sec_listeners_ptr,
|
||||
std::unordered_map<MarketDataListenerID, MarketDataListenerData>
|
||||
*listeners_ptr)
|
||||
: site_key_(std::move(site_key)), ref_counts_ptr_(ref_counts_ptr),
|
||||
: site_key_(std::move(site_key)), sec_listeners_ptr_(sec_listeners_ptr),
|
||||
listeners_ptr_(listeners_ptr) {}
|
||||
|
||||
std::unordered_map<domain::SecurityID, std::uint32_t> &
|
||||
BinanceDepthStreamSite::ref_counts_() {
|
||||
return *ref_counts_ptr_;
|
||||
collections::UnorderedSetMap<domain::SecurityID, MarketDataListenerID> &
|
||||
BinanceDepthStreamSite::sec_listeners_() {
|
||||
return *sec_listeners_ptr_;
|
||||
}
|
||||
|
||||
const std::unordered_map<domain::SecurityID, std::uint32_t> &
|
||||
BinanceDepthStreamSite::ref_counts_() const {
|
||||
return *ref_counts_ptr_;
|
||||
const collections::UnorderedSetMap<domain::SecurityID, MarketDataListenerID> &
|
||||
BinanceDepthStreamSite::sec_listeners_() const {
|
||||
return *sec_listeners_ptr_;
|
||||
}
|
||||
|
||||
std::unordered_map<MarketDataListenerID, MarketDataListenerData> &
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "BinanceDepthStreamSiteKey.h"
|
||||
#include "lib/api/md/MarketDataSubscriptionParams.pb.h"
|
||||
#include "lib/api/md/binance/BinanceMarketDataSubscriptionParams.pb.h"
|
||||
#include "lib/collections/UnorderedSetMap.h"
|
||||
#include "lib/domain/SecurityID.h"
|
||||
#include "lib/domain/SecurityResolver.h"
|
||||
#include "lib/domain/Venue.h"
|
||||
@@ -24,16 +25,16 @@ namespace bts::md::binance {
|
||||
* @brief Facilitates the flow of Binance market data updates (from a particular
|
||||
* site) to subscribed listeners (of type IMarketDataListener)
|
||||
*
|
||||
* @details Maintains a reference count of subscriptions per symbol, so for a
|
||||
* given symbol, one internal subscription serves all listeners subscribed
|
||||
* to that symbol
|
||||
* @details For a given symbol, one internal subscription serves all listeners
|
||||
* subscribed to that symbol
|
||||
*/
|
||||
class BinanceDepthStreamSite {
|
||||
public:
|
||||
BinanceDepthStreamSite() = default;
|
||||
BinanceDepthStreamSite(
|
||||
BinanceDepthStreamSiteKey site_key,
|
||||
std::unordered_map<domain::SecurityID, std::uint32_t> *ref_counts_ptr,
|
||||
collections::UnorderedSetMap<domain::SecurityID, MarketDataListenerID>
|
||||
*sec_listeners_ptr,
|
||||
std::unordered_map<MarketDataListenerID, MarketDataListenerData>
|
||||
*listeners_ptr);
|
||||
|
||||
@@ -67,8 +68,8 @@ class BinanceDepthStreamSite {
|
||||
|
||||
private:
|
||||
BinanceDepthStreamSiteKey site_key_;
|
||||
std::unordered_map<domain::SecurityID, std::uint32_t> *ref_counts_ptr_ =
|
||||
nullptr;
|
||||
collections::UnorderedSetMap<domain::SecurityID, MarketDataListenerID>
|
||||
*sec_listeners_ptr_ = nullptr;
|
||||
std::unordered_map<MarketDataListenerID, MarketDataListenerData>
|
||||
*listeners_ptr_ = nullptr;
|
||||
std::optional<binance::BinanceDiffDepthSubscription> binance_ = std::nullopt;
|
||||
@@ -76,9 +77,10 @@ class BinanceDepthStreamSite {
|
||||
// (disposed and recreated) without unbinding the existing callback.
|
||||
BinanceDiffDepthSubscription::OnDepthChanged on_depth_changed_proxy_;
|
||||
|
||||
std::unordered_map<domain::SecurityID, std::uint32_t> &ref_counts_();
|
||||
const std::unordered_map<domain::SecurityID, std::uint32_t> &
|
||||
ref_counts_() const;
|
||||
collections::UnorderedSetMap<domain::SecurityID, MarketDataListenerID> &
|
||||
sec_listeners_();
|
||||
const collections::UnorderedSetMap<domain::SecurityID, MarketDataListenerID>
|
||||
&sec_listeners_() const;
|
||||
|
||||
std::unordered_map<MarketDataListenerID, MarketDataListenerData> &
|
||||
listeners_();
|
||||
@@ -109,7 +111,8 @@ void BinanceDepthStreamSite::subscribe(
|
||||
"Cannot subscribe. Listener already subscribed to sec. sid: "
|
||||
"{}; sym: {}",
|
||||
sym, sid);
|
||||
if (ref_counts_().try_emplace(sid, 0).first->second++ == 0) {
|
||||
auto [it, is_new_sym, _] = sec_listeners_().add(sid, listener_id);
|
||||
if (is_new_sym) {
|
||||
ensure_connect_();
|
||||
// first subscription for this symbol
|
||||
binance_->add_symbol(sym);
|
||||
@@ -134,14 +137,8 @@ void BinanceDepthStreamSite::unsubscribe(
|
||||
"Cannot unsubscribe. Listener not subscribed to sec. sid: {}; "
|
||||
"sym: {}",
|
||||
sid, sym);
|
||||
auto rc_it = ref_counts_().find(sid);
|
||||
bts_assert_msg(
|
||||
rc_it != ref_counts_().end(),
|
||||
"Inter-listener security subscription ref count out of sync "
|
||||
"with listener's subscribed securities.");
|
||||
if (--rc_it->second == 0) {
|
||||
// last subscription for this symbol
|
||||
ref_counts_().erase(rc_it);
|
||||
auto [it, removed_sym, _] = sec_listeners_().remove(sid, listener_id);
|
||||
if (removed_sym) {
|
||||
binance_->remove_symbol(sym);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user