From 3156695bbd48bab2af6089129f5a26679c96684a Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Thu, 13 Nov 2025 13:05:35 -0600 Subject: [PATCH] use `unordered_map` instead of `map` for lookasides some of these are used in fairly hot code, and so using `unordered_map` should improve performance --- library/include/DataDefs.h | 46 +++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/library/include/DataDefs.h b/library/include/DataDefs.h index 7fac3c0c5e..a067b8e24a 100644 --- a/library/include/DataDefs.h +++ b/library/include/DataDefs.h @@ -31,7 +31,9 @@ distribution. #include #include #include +#include #include +#include #include "BitArray.h" #include "Export.h" @@ -136,8 +138,8 @@ namespace DFHack class DFHACK_EXPORT compound_identity : public constructed_identity { static std::list* list; - static std::map* parent_map; - static std::map>* children_map; + static std::unordered_map* parent_map; + static std::unordered_map>* children_map; static std::vector* top_scope; const char *dfhack_name; @@ -204,7 +206,7 @@ namespace DFHack class DFHACK_EXPORT enum_identity : public compound_identity { public: struct ComplexData { - std::map value_index_map; + std::unordered_map value_index_map; std::vector index_value_map; ComplexData(std::initializer_list values); size_t size() const { @@ -289,8 +291,8 @@ namespace DFHack }; class DFHACK_EXPORT struct_identity : public compound_identity { - static std::map* parent_map; - static std::map>* children_map; + static std::unordered_map* parent_map; + static std::unordered_map>* children_map; const struct_field_info *fields; @@ -355,6 +357,30 @@ namespace DFHack virtual void build_metatable(lua_State *state) const; }; + namespace + { + template + struct overload : Bases ... + { + using is_transparent = void; + using Bases::operator() ...; + }; + + struct char_pointer_hash + { + auto operator()(const char* ptr) const noexcept + { + return std::hash{}(ptr); + } + }; + + using transparent_string_hash = overload< + std::hash, + std::hash, + char_pointer_hash + >; + } + #ifdef _MSC_VER using virtual_ptr = void*; #else @@ -367,13 +393,13 @@ namespace DFHack class DFHACK_EXPORT virtual_identity : public struct_identity { public: using interpose_t = VMethodInterposeLinkBase*; - using interpose_list_t = std::map; + using interpose_list_t = std::unordered_map; private: - static std::map> *name_lookup; - static std::map* known; - static std::map* vtable_ptr_map; - static std::map* interpose_list_map; + static std::unordered_map> *name_lookup; + static std::unordered_map* known; + static std::unordered_map* vtable_ptr_map; + static std::unordered_map* interpose_list_map; const char *original_name;