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 5, 2024
1 parent 40efd00 commit f1a2f42
Show file tree
Hide file tree
Showing 3 changed files with 1,839 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_
7 changes: 7 additions & 0 deletions src/third_party/emhash/README.ninja
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Description: emhash8::HashMap for C++14/17
Version: 1.6.5 (commit bdebddbdce1b473bbc189178fd523ef4a876ea01)
URL: https://github.com/ktprime/emhash
Copyright: Copyright (c) 2021-2024 Huang Yuanbing & bailuzhou AT 163.com
SPDX-License-Identifier: MIT
Local changes:
- None.
Loading

0 comments on commit f1a2f42

Please sign in to comment.