Skip to content

Commit

Permalink
Switch hash tables to emhash8::HashMap.
Browse files Browse the repository at this point in the history
This is much faster than std::unordered_map, and also slightly faster
than phmap::flat_hash_map that was included in PR ninja-build#2468.
It is MIT-licensed, and we just include the .h file wholesale.

I haven't done a detailed test of all the various unordered_maps
out there, but this is the overall highest-ranking contender on

  https://martin.ankerl.com/2022/08/27/hashmap-bench-01/

except for ankerl::unordered_dense::map, which requires C++17.

For a no-op build of Chromium (Linux, Zen 2),
this reduces time spent from 5.14 to 4.62 seconds.
  • Loading branch information
Steinar H. Gunderson committed Nov 4, 2024
1 parent 269d0d4 commit e845a50
Show file tree
Hide file tree
Showing 2 changed files with 1,832 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string.h>
#include "string_piece.h"
#include "util.h"
#include "third_party/emhash/hash_table8.hpp"

// MurmurHash2, by Austin Appleby
static inline
Expand Down Expand Up @@ -53,8 +54,6 @@ unsigned int MurmurHash2(const void* key, size_t len) {
return h;
}

#include <unordered_map>

namespace std {
template<>
struct hash<StringPiece> {
Expand All @@ -73,7 +72,7 @@ struct hash<StringPiece> {
/// mapping StringPiece => Foo*.
template<typename V>
struct ExternalStringHashMap {
typedef std::unordered_map<StringPiece, V> Type;
typedef emhash8::HashMap<StringPiece, V> Type;
};

#endif // NINJA_MAP_H_
Loading

0 comments on commit e845a50

Please sign in to comment.