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.cpp | 5 +++-- src/core/crypto/key_manager.h | 41 +++++++++++------------------------------ 2 files changed, 14 insertions(+), 32 deletions(-) (limited to 'src/core/crypto') diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index db8b22c85..95158e630 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -125,7 +125,8 @@ bool KeyManager::KeyFileExists(bool title) { FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "prod.keys"); } -const std::unordered_map> KeyManager::s128_file_id = { +void KeyManager::DeriveSDSeedLazy() { +const boost::container::flat_map> KeyManager::s128_file_id = { {"master_key_00", {S128KeyType::Master, 0, 0}}, {"master_key_01", {S128KeyType::Master, 1, 0}}, {"master_key_02", {S128KeyType::Master, 2, 0}}, @@ -169,7 +170,7 @@ const std::unordered_map> KeyManager::s128_fi {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast(KeyAreaKeyType::System)}}, }; -const std::unordered_map> KeyManager::s256_file_id = { +const boost::container::flat_map> KeyManager::s256_file_id = { {"header_key", {S256KeyType::Header, 0, 0}}, {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}}, {"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}}, 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