Skip to content

Commit 6b6334c

Browse files
ddobbelaereDisservin
authored andcommitted
Replace FNV-1a hash by faster and better MurmurHash64A.
Replace FNV-1a hash algorithm by public domain MurmurHash64A, by Austin Appleby, which is faster and has better hash properties. This is also the default hash algorithm of GNU C++ Standard Library. Startup time on a given system (with x86-64-avx2 binaries) reduces from 378ms to 332ms (46ms faster). Hashing of the largest data input (46.1MB) goes from 44.2ms to 6.2ms, so about 7x faster. No functional change closes official-stockfish#6579 Bench: 2570943
1 parent 005f0f9 commit 6b6334c

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

src/misc.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,45 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) {
428428
void sync_cout_start() { std::cout << IO_LOCK; }
429429
void sync_cout_end() { std::cout << IO_UNLOCK; }
430430

431+
// Hash function based on public domain MurmurHash64A, by Austin Appleby.
432+
uint64_t hash_bytes(const char* data, size_t size) {
433+
const uint64_t m = 0xc6a4a7935bd1e995ull;
434+
const int r = 47;
435+
436+
uint64_t h = size * m;
437+
438+
const char* end = data + (size & ~(size_t) 7);
439+
440+
for (const char* p = data; p != end; p += 8)
441+
{
442+
uint64_t k;
443+
std::memcpy(&k, p, sizeof(k));
444+
445+
k *= m;
446+
k ^= k >> r;
447+
k *= m;
448+
449+
h ^= k;
450+
h *= m;
451+
}
452+
453+
if (size & 7)
454+
{
455+
uint64_t k = 0;
456+
for (int i = (size & 7) - 1; i >= 0; i--)
457+
k = (k << 8) | (uint64_t) end[i];
458+
459+
h ^= k;
460+
h *= m;
461+
}
462+
463+
h ^= h >> r;
464+
h *= m;
465+
h ^= h >> r;
466+
467+
return h;
468+
}
469+
431470
// Trampoline helper to avoid moving Logger to misc.h
432471
void start_logger(const std::string& fname) { Logger::start(fname); }
433472

src/misc.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,7 @@ inline uint64_t mul_hi64(uint64_t a, uint64_t b) {
306306
#endif
307307
}
308308

309-
inline std::uint64_t hash_bytes(const char* data, std::size_t size) {
310-
// FNV-1a 64-bit
311-
const char* p = data;
312-
std::uint64_t h = 14695981039346656037ull;
313-
for (std::size_t i = 0; i < size; ++i)
314-
h = (h ^ p[i]) * 1099511628211ull;
315-
return h;
316-
}
309+
uint64_t hash_bytes(const char*, size_t);
317310

318311
template<typename T>
319312
inline std::size_t get_raw_data_hash(const T& value) {

0 commit comments

Comments
 (0)