Skip to content

Commit 3156695

Browse files
committed
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
1 parent 004b4dd commit 3156695

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

library/include/DataDefs.h

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ distribution.
3131
#include <string>
3232
#include <type_traits>
3333
#include <utility>
34+
#include <unordered_map>
3435
#include <vector>
36+
#include <functional>
3537

3638
#include "BitArray.h"
3739
#include "Export.h"
@@ -136,8 +138,8 @@ namespace DFHack
136138

137139
class DFHACK_EXPORT compound_identity : public constructed_identity {
138140
static std::list<const compound_identity*>* list;
139-
static std::map<const compound_identity*, const compound_identity*>* parent_map;
140-
static std::map<const compound_identity*, std::vector<const compound_identity*>>* children_map;
141+
static std::unordered_map<const compound_identity*, const compound_identity*>* parent_map;
142+
static std::unordered_map<const compound_identity*, std::vector<const compound_identity*>>* children_map;
141143
static std::vector<const compound_identity*>* top_scope;
142144

143145
const char *dfhack_name;
@@ -204,7 +206,7 @@ namespace DFHack
204206
class DFHACK_EXPORT enum_identity : public compound_identity {
205207
public:
206208
struct ComplexData {
207-
std::map<int64_t, size_t> value_index_map;
209+
std::unordered_map<int64_t, size_t> value_index_map;
208210
std::vector<int64_t> index_value_map;
209211
ComplexData(std::initializer_list<int64_t> values);
210212
size_t size() const {
@@ -289,8 +291,8 @@ namespace DFHack
289291
};
290292

291293
class DFHACK_EXPORT struct_identity : public compound_identity {
292-
static std::map<const struct_identity*, const struct_identity*>* parent_map;
293-
static std::map<const struct_identity*, std::vector<const struct_identity*>>* children_map;
294+
static std::unordered_map<const struct_identity*, const struct_identity*>* parent_map;
295+
static std::unordered_map<const struct_identity*, std::vector<const struct_identity*>>* children_map;
294296

295297
const struct_field_info *fields;
296298

@@ -355,6 +357,30 @@ namespace DFHack
355357
virtual void build_metatable(lua_State *state) const;
356358
};
357359

360+
namespace
361+
{
362+
template<typename ... Bases>
363+
struct overload : Bases ...
364+
{
365+
using is_transparent = void;
366+
using Bases::operator() ...;
367+
};
368+
369+
struct char_pointer_hash
370+
{
371+
auto operator()(const char* ptr) const noexcept
372+
{
373+
return std::hash<std::string_view>{}(ptr);
374+
}
375+
};
376+
377+
using transparent_string_hash = overload<
378+
std::hash<std::string>,
379+
std::hash<std::string_view>,
380+
char_pointer_hash
381+
>;
382+
}
383+
358384
#ifdef _MSC_VER
359385
using virtual_ptr = void*;
360386
#else
@@ -367,13 +393,13 @@ namespace DFHack
367393
class DFHACK_EXPORT virtual_identity : public struct_identity {
368394
public:
369395
using interpose_t = VMethodInterposeLinkBase*;
370-
using interpose_list_t = std::map<int, interpose_t>;
396+
using interpose_list_t = std::unordered_map<int, interpose_t>;
371397

372398
private:
373-
static std::map<const std::string, const virtual_identity*, std::less<>> *name_lookup;
374-
static std::map<void*, const virtual_identity*>* known;
375-
static std::map<const virtual_identity*, void*>* vtable_ptr_map;
376-
static std::map<const virtual_identity*, interpose_list_t>* interpose_list_map;
399+
static std::unordered_map<std::string, const virtual_identity*, transparent_string_hash, std::equal_to<>> *name_lookup;
400+
static std::unordered_map<void*, const virtual_identity*>* known;
401+
static std::unordered_map<const virtual_identity*, void*>* vtable_ptr_map;
402+
static std::unordered_map<const virtual_identity*, interpose_list_t>* interpose_list_map;
377403

378404
const char *original_name;
379405

0 commit comments

Comments
 (0)