From cde665c56514c1b701c0fe94fc943c7692be7f32 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Thu, 16 Aug 2018 17:10:01 -0400 Subject: key_manager: Switch to boost flat_map for keys Should make key gets marginally faster. --- src/core/crypto/key_manager.h | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index 0c62d4421..33b1ad383 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -7,10 +7,11 @@ #include #include #include -#include #include +#include #include #include "common/common_types.h" +#include "core/loader/loader.h" namespace Core::Crypto { @@ -59,34 +60,14 @@ struct KeyIndex { } }; -// The following two (== and hash) are so KeyIndex can be a key in unordered_map - -template -bool operator==(const KeyIndex& lhs, const KeyIndex& rhs) { - return std::tie(lhs.type, lhs.field1, lhs.field2) == std::tie(rhs.type, rhs.field1, rhs.field2); -} - +// boost flat_map requires operator< for O(log(n)) lookups. template -bool operator!=(const KeyIndex& lhs, const KeyIndex& rhs) { - return !operator==(lhs, rhs); +bool operator<(const KeyIndex& lhs, const KeyIndex& rhs) { + return (static_cast(lhs.type) < static_cast(rhs.type)) || + (lhs.type == rhs.type && lhs.field1 < rhs.field1) || + (lhs.type == rhs.type && lhs.field1 == rhs.field1 && lhs.field2 < rhs.field2); } -} // namespace Core::Crypto - -namespace std { -template -struct hash> { - size_t operator()(const Core::Crypto::KeyIndex& k) const { - using std::hash; - - return ((hash()(static_cast(k.type)) ^ (hash()(k.field1) << 1)) >> 1) ^ - (hash()(k.field2) << 1); - } -}; -} // namespace std - -namespace Core::Crypto { - class KeyManager { public: KeyManager(); @@ -103,15 +84,15 @@ public: static bool KeyFileExists(bool title); private: - std::unordered_map, Key128> s128_keys; - std::unordered_map, Key256> s256_keys; + boost::container::flat_map, Key128> s128_keys; + boost::container::flat_map, Key256> s256_keys; bool dev_mode; void LoadFromFile(const std::string& filename, bool is_title_keys); void AttemptLoadKeyFile(const std::string& dir1, const std::string& dir2, const std::string& filename, bool title); - static const std::unordered_map> s128_file_id; - static const std::unordered_map> s256_file_id; + static const boost::container::flat_map> s128_file_id; + static const boost::container::flat_map> s256_file_id; }; } // namespace Core::Crypto -- cgit v1.2.3