From 25439b094ae93fe75df17250275591dd2ff272f8 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 21 Dec 2024 16:22:48 +0000 Subject: [PATCH] improvement: Perform sec lookup in const time --- src/lib/md/SecurityCache.cpp | 245 ++++++++++++++++++++++--------- src/lib/md/SecurityCache.h | 6 + src/lib/md/SecurityOracle.cpp | 10 ++ src/lib/md/SecurityOracle.h | 3 +- src/lib/md/security_type_index.h | 61 ++++++++ src/lib/md/venue_index.h | 87 +++++++++++ 6 files changed, 338 insertions(+), 74 deletions(-) create mode 100644 src/lib/md/security_type_index.h create mode 100644 src/lib/md/venue_index.h diff --git a/src/lib/md/SecurityCache.cpp b/src/lib/md/SecurityCache.cpp index c3904e256..8b80fa51d 100644 --- a/src/lib/md/SecurityCache.cpp +++ b/src/lib/md/SecurityCache.cpp @@ -3,6 +3,8 @@ #include "lib/domain_model/Security.h" #include "lib/domain_model/Security.pb.h" #include "lib/logging/logging.h" +#include "lib/md/security_type_index.h" +#include "lib/md/venue_index.h" #include "lib/utils/Dispatcher.h" #include "lib/utils/LockBox.h" @@ -14,9 +16,152 @@ namespace bts { namespace md { +using std::string; +using std::unordered_map; + +struct SecurityCacheIndex { + btree::map cache; + btree::map ex_cache; + VenueIndex>> + venue_type_ids; + + const bts::api::Security *get_security( + api::Venue venue, const std::string &desc) { + auto id = get_id_(venue, desc); + auto sec_it = cache.find(SecurityKey{id}); + if (sec_it == cache.end()) { + return nullptr; + } + return sec_it->second.get(); + } + + const bts::api::Security *get_security( + api::Venue venue, api::SecurityType type, const std::string &desc) { + auto id = get_id_(venue, type, desc); + auto sec_it = cache.find(SecurityKey{id}); + if (sec_it == cache.end()) { + return nullptr; + } + return sec_it->second.get(); + } + + const Security *try_get_security(SecurityKey key) const { + auto iter = cache.find(key); + if (iter != cache.end()) { + return iter->second.get(); + } + return (const api::Security *)nullptr; + } + + const Security *try_get_security(ExchangeSecurityKey key) const { + auto iter = ex_cache.find(key); + if (iter != ex_cache.end()) { + return iter->second.get(); + } + return (const api::Security *)nullptr; + } + + std::vector get_securities( + api::Venue venue, api::SecurityType type, const std::string &name) { + std::vector result; + const bool all_types = type == api::SECURITYTYPE_UNSET; + const bool all_venues = venue == api::VENUE_UNSET; + const bool all_names = name.empty(); + for (auto const &p : cache) { + const auto &s = *p.second; + if ((all_types || + type == securities::security_type(s.Contract_case())) && + (all_venues || venue == s.exchange()) && + (all_names || name == securities::descriptor(s))) { + result.push_back(p.second.get()); + } + } + return result; + } + + std::vector get_securities( + std::function include) { + std::vector result; + for (auto const &p : cache) { + auto &s = *p.second; + if (include(s)) { + result.push_back(&s); + } + } + return result; + } + + bool add_live_security(SecurityPtr security) { + bts_assert(security); + if (security->has_glbx()) { + ExchangeSecurityKey key(security->id()); + ex_cache.insert(std::make_pair(key, security)); + } + SecurityKey key(security->id()); + // Insert but don't overwrite existing + auto result = + cache.insert(std::make_pair(SecurityKey(security->id()), security)); + return result.second; + } + + const Security *add_db_security(SecurityPtr security) { + bts_assert(security); + if (security->has_glbx()) { + ExchangeSecurityKey key(security->id()); + ex_cache.insert(std::make_pair(key, security)); + } + SecurityKey key(security->id()); + auto it = cache.insert(std::make_pair(key, security)); + const Security *active_sec = it.first->second.get(); + // If the security was just added, active_sec is equal to security. + // If it was already present, it must be a recently inserted 'live' + // security and we should use it (instead of the db security). + venue_type_ids.get_or_add(security->exchange()) + .get_or_add(bts::securities::security_type(*active_sec)) + .try_emplace( + bts::securities::descriptor(*active_sec), active_sec->id()); + return active_sec; + } + + private: + std::uint32_t get_id_(api::Venue venue, const std::string &desc) { + auto id = get_id_(venue, api::SECURITYTYPE_SPOT, desc); + if (id != 0) { + return id; + } + id = get_id_(venue, api::SECURITYTYPE_FUTURE, desc); + if (id != 0) { + return id; + } + id = get_id_(venue, api::SECURITYTYPE_OPTION, desc); + if (id != 0) { + return id; + } + id = get_id_(venue, api::SECURITYTYPE_SPREAD, desc); + if (id != 0) { + return id; + } + id = get_id_(venue, api::SECURITYTYPE_SYNTHETIC, desc); + if (id != 0) { + return id; + } + return 0; + } + + std::uint32_t get_id_( + api::Venue venue, api::SecurityType type, const std::string &desc) { + auto &type_store = venue_type_ids.get_or_add(venue); + auto &ids = type_store.get_or_add(type); + auto it = ids.find(desc); + if (it != ids.end()) { + return it->second; + } + return 0; + } +}; + struct SecurityCacheImpl { - utils::LockBox> cache; - utils::LockBox> ex_cache; + utils::LockBox cache; tbb::concurrent_queue update_queue; void enqueue_update_db(const SecurityPtr &); void flush(); @@ -53,23 +198,13 @@ SecurityCache::SecurityCache() : _me(std::make_shared()) {} SecurityCache::~SecurityCache() {} const Security *SecurityCache::try_get_security(SecurityKey key) const { - return _me->cache.lock([key](btree::map &t) { - auto iter = t.find(key); - if (iter != t.end()) { - return iter->second.get(); - } - return (const api::Security *)nullptr; - }); + return _me->cache.lock( + [key](SecurityCacheIndex &t) { return t.try_get_security(key); }); } + const Security *SecurityCache::try_get_security(ExchangeSecurityKey key) const { - return _me->ex_cache.lock( - [key](btree::map &t) { - auto iter = t.find(key); - if (iter != t.end()) { - return iter->second.get(); - } - return (const api::Security *)nullptr; - }); + return _me->cache.lock( + [key](SecurityCacheIndex &t) { return t.try_get_security(key); }); } const Security *SecurityCache::get_security(SecurityKey key) { @@ -88,47 +223,31 @@ const Security *SecurityCache::get_security(SecurityKey key) { } const Security *SecurityCache::get_security( + api::Venue venue, const std::string &name) { + return _me->cache.lock([venue, name](SecurityCacheIndex &cache) { + return cache.get_security(venue, name); + }); +} + +const Security *SecurityCache::get_security( api::Venue venue, api::SecurityType type, const std::string &name) { - const Security *result = nullptr; - _me->cache.lock([&result, venue, type, name](auto &cache) { - for (auto const &p : cache) { - if ((type == api::SECURITYTYPE_UNSET || - type == securities::security_type(p.second->Contract_case())) && - (venue == api::VENUE_UNSET && venue == p.second->exchange()) && - (name.empty() || name == securities::descriptor(*p.second))) { - result = p.second.get(); - break; - } - } + return _me->cache.lock([venue, type, name](SecurityCacheIndex &cache) { + return cache.get_security(venue, type, name); }); - return result; } std::vector SecurityCache::get_securities( api::Venue venue, api::SecurityType type, const std::string &name) { - std::vector result; - _me->cache.lock([&result, venue, type, name](auto &cache) { - const bool all_types = type == api::SECURITYTYPE_UNSET; - const bool all_venues = venue == api::VENUE_UNSET; - const bool all_names = name.empty(); - for (auto const &p : cache) { - const auto &s = *p.second; - if ((all_types || - type == securities::security_type(s.Contract_case())) && - (all_venues || venue == s.exchange()) && - (all_names || name == securities::descriptor(s))) { - result.push_back(p.second.get()); - } - } + return _me->cache.lock([venue, type, &name](SecurityCacheIndex &cache) { + return cache.get_securities(venue, type, name); }); - return result; } std::vector SecurityCache::get_securities( std::function include) { std::vector result; - _me->cache.lock([&result, include](auto &cache) { - for (auto const &p : cache) { + _me->cache.lock([&result, include](SecurityCacheIndex &cache) { + for (auto const &p : cache.cache) { auto &s = *p.second; if (include(s)) { result.push_back(&s); @@ -142,8 +261,8 @@ std::vector SecurityCache::get_securities() { std::vector result; result.reserve(100000); - _me->cache.lock([&result](auto &cache) { - for (auto const &p : cache) { + _me->cache.lock([&result](SecurityCacheIndex &cache) { + for (const auto &p : cache.cache) { result.push_back(p.second.get()); } }); @@ -154,19 +273,8 @@ const Security *SecurityCache::add_db_security(const SecurityPtr &security) { bts_assert(security); LOG(md, Info, "SecurityCache::add_db_security security: {}", bts::securities::descriptor(*security)); - if (security->has_glbx()) { - _me->ex_cache.lock([&security](auto &cache) { - ExchangeSecurityKey key(security->id()); - cache.insert(std::make_pair(key, security)); - }); - } - return _me->cache.lock([&security](auto &cache) { - SecurityKey key(security->id()); - auto result = cache.insert(std::make_pair(key, security)); - // If result was inserted result.first->second is equal to security - // If the value was already present it must be a recently inserted 'live' - // security and we should use it - return result.first->second.get(); + return _me->cache.lock([&security](SecurityCacheIndex &cache) { + return cache.add_db_security(security); }); } @@ -174,19 +282,10 @@ bool SecurityCache::add_live_security(const SecurityPtr &security) { bts_assert(security); LOG(md, Diag, "SecurityCache::add_live_security security: {}", bts::securities::descriptor(*security)); - if (security->has_glbx()) { - _me->ex_cache.lock([&security](auto &cache) { - ExchangeSecurityKey key(security->id()); - cache.insert(std::make_pair(key, security)); - }); - } - return _me->cache.lock([&](auto &cache) { - SecurityKey key(security->id()); - // Insert but don't overwrite existing - auto result = - cache.insert(std::make_pair(SecurityKey(security->id()), security)); + return _me->cache.lock([&](SecurityCacheIndex &cache) { + const bool is_new_sec = cache.add_live_security(security); _me->enqueue_update_db(security); - return result.second; + return is_new_sec; }); } diff --git a/src/lib/md/SecurityCache.h b/src/lib/md/SecurityCache.h index c64fba6de..42de1393c 100644 --- a/src/lib/md/SecurityCache.h +++ b/src/lib/md/SecurityCache.h @@ -23,6 +23,12 @@ class SecurityCache { // Load security from cache with fallback to database const Security *get_security(SecurityKey key); + /** + * Finds the first security with matching @p venue and @p name. Params that are + * empty/unset are ignored. + */ + const Security *get_security(api::Venue venue, const std::string &name); + /** * Finds the first security with matching @p venue @p type and @p name. Params that * are empty/unset are ignored. diff --git a/src/lib/md/SecurityOracle.cpp b/src/lib/md/SecurityOracle.cpp index 69d4d2247..9b12d955b 100644 --- a/src/lib/md/SecurityOracle.cpp +++ b/src/lib/md/SecurityOracle.cpp @@ -17,6 +17,16 @@ const api::Security *SecurityOracle::get_security( return _cache.try_get_security(key); } +const api::Security *SecurityOracle::get_security( + api::Venue venue, const std::string &name) const { + return _cache.get_security(venue, name); +} + +const api::Security *SecurityOracle::get_security( + api::Venue venue, api::SecurityType type, const std::string &name) const { + return _cache.get_security(venue, type, name); +} + std::vector SecurityOracle::get_securities() const { return _cache.get_securities(); } diff --git a/src/lib/md/SecurityOracle.h b/src/lib/md/SecurityOracle.h index 9309df1b7..c32539d1a 100644 --- a/src/lib/md/SecurityOracle.h +++ b/src/lib/md/SecurityOracle.h @@ -20,7 +20,8 @@ class SecurityOracle { ~SecurityOracle(); const api::Security *get_security(SecurityKey key) const; const api::Security *get_security(ExchangeSecurityKey key) const; - + const api::Security *get_security(api::Venue venue, const std::string &name) const; + const api::Security *get_security(api::Venue venue, api::SecurityType type, const std::string &name) const; /** * Finds all securities where @p include returns true. */ diff --git a/src/lib/md/security_type_index.h b/src/lib/md/security_type_index.h new file mode 100644 index 000000000..9347f8928 --- /dev/null +++ b/src/lib/md/security_type_index.h @@ -0,0 +1,61 @@ +#pragma once + +#include "lib/domain_model/Security.pb.h" +#include "lib/utils/bts_assert.h" + +#include + +namespace bts::md { + +template +class SecurityTypeIndex { + std::optional spot; + std::optional future; + std::optional option; + std::optional spread; + std::optional synthetic; + + public: + std::optional &get(api::SecurityType type) { return get_(*this, type); } + + const std::optional &get(api::SecurityType type) const { + return get_(*this, type); + } + + template + T &get_or_add(api::SecurityType type, Args... args) { + std::optional &val = get_(*this, type); + if (!val) { + val.emplace(args...); + } + return *val; + } + + private: + template + static auto &get_(U &self, api::SecurityType type) { + switch (type) { + case api::SECURITYTYPE_SPOT: + return self.spot; + case api::SECURITYTYPE_OPTION: + return self.option; + case api::SECURITYTYPE_FUTURE: + return self.future; + case api::SECURITYTYPE_SPREAD: + return self.spread; + case api::SECURITYTYPE_SYNTHETIC: + return self.synthetic; + case api::SECURITYTYPE_UNSET: + case api::SecurityType_INT_MIN_SENTINEL_DO_NOT_USE_: + case api::SecurityType_INT_MAX_SENTINEL_DO_NOT_USE_: + default: + bts_assert_msgf( + false, "SecurityType '{}' not present in VenueIndex", + api::SecurityType_Name(type)); + static std::optional default_rv; + return default_rv; + } + } +}; + +} // namespace bts::md diff --git a/src/lib/md/venue_index.h b/src/lib/md/venue_index.h new file mode 100644 index 000000000..09f12c32f --- /dev/null +++ b/src/lib/md/venue_index.h @@ -0,0 +1,87 @@ +#pragma once + +#include "lib/domain_model/Security.pb.h" +#include "lib/utils/bts_assert.h" + +#include + +namespace bts::md { + +template +class VenueIndex { + std::optional binance; + std::optional binance_coin_futures; + std::optional binance_us; + std::optional binance_usds_futures; + std::optional okx; + std::optional deribit; + std::optional cboe; + std::optional cboecfe; + std::optional glbx; + std::optional ice; + std::optional taifex; + std::optional coinbase; + + public: + std::optional &get(api::Venue venue) { return get_(*this, venue); } + + const std::optional &get(api::Venue venue) const { + return get_(*this, venue); + } + + template + T &get_or_add(api::Venue venue, Args... args) { + std::optional &val = get_(*this, venue); + if (!val) { + val.emplace(args...); + } + return *val; + } + + private: + template + static auto &get_(U &self, api::Venue venue) { + switch (venue) { + case api::VENUE_CBOE: + return self.cboe; + case api::VENUE_CBOECFE: + return self.cboecfe; + case api::VENUE_GLBX: + return self.glbx; + case api::VENUE_ICE: + return self.ice; + case api::VENUE_DERIBIT: + return self.deribit; + case api::VENUE_BINANCE: + return self.binance; + case api::VENUE_BINANCE_COIN_FUTURES: + return self.binance_coin_futures; + case api::VENUE_BINANCE_USDS_FUTURES: + return self.binance_usds_futures; + case api::VENUE_BINANCE_US: + return self.binance_us; + case api::VENUE_COINBASE: + return self.coinbase; + case api::VENUE_OKX: + return self.okx; + case api::VENUE_TAIFEX: + return self.taifex; + break; + case api::VENUE_FTX: + case api::VENUE_SIM: + case api::VENUE_SIMICE: + case api::VENUE_BTS: + case api::VENUE_UNSET: + case api::Venue_INT_MIN_SENTINEL_DO_NOT_USE_: + case api::Venue_INT_MAX_SENTINEL_DO_NOT_USE_: + default: + bts_assert_msgf( + false, "Venue '{}' not present in VenueIndex", + api::Venue_Name(venue)); + static std::optional default_rv; + return default_rv; + } + } +}; + +} // namespace bts::md