Commit 542f89ff authored by Tamar Christina's avatar Tamar Christina Committed by Ben Gamari

Replace hashing function for string keys implementation with xxhash

When doing profiling on startup time of ghci on Windows, both cold and
startup loading static LLVM libs, the profiler is showing a glaring red
spot on the division operation of the the hashStr function.

In fact profiling shows 14% of the time is spent hashing the keys.

So I am replacing the hash function with xxHash which is a very fast
non-crypto hash. It's faster than MurMurHash which node etc use.

It also passes SMHasher. I can provide if required the collected raw
data.  But from analysis done on the keys, xxHash does not introduce
more collisions than before, the amount splits seem about the same and
the distributions among the buckets are slightly more uniform than

However the runtime dropped enough to remove the function completely
from the profiler's report.

There's also a noticeable improvement in responsiveness.

xxHash is BSD licensed and can be found

Test Plan: ./validate

Reviewers: austin, bgamari, erikd, simonmar

Reviewed By: bgamari

Subscribers: rwbarton, thomie

GHC Trac Issues: #13165

Differential Revision:
parent a4c2ac2c
......@@ -13,6 +13,7 @@
#include "Hash.h"
#include "RtsUtils.h"
#include "xxhash.h"
#include <string.h>
......@@ -78,18 +79,14 @@ hashWord(const HashTable *table, StgWord key)
hashStr(const HashTable *table, char *key)
int h, bucket;
char *s;
s = key;
for (h=0; *s; s++) {
h *= 128;
h += *s;
h = h % 1048583; /* some random large prime */
#if x86_64_HOST_ARCH
StgWord h = XXH64 (key, strlen(key), 1048583);
StgWord h = XXH32 (key, strlen(key), 1048583);
/* Mod the size of the hash table (a power of 2) */
bucket = h & table->mask1;
int bucket = h & table->mask1;
if (bucket < table->split) {
/* Mod the size of the expanded hash table (also a power of 2) */
......@@ -375,6 +375,8 @@ rts/RtsUtils_CC_OPTS += -DTargetVendor=\"$(TargetVendor_CPP)\"
rts/RtsUtils_CC_OPTS += -DGhcUnregisterised=\"$(GhcUnregisterised)\"
rts/RtsUtils_CC_OPTS += -DGhcEnableTablesNextToCode=\"$(GhcEnableTablesNextToCode)\"
rts/xxhash_CC_OPTS += -O3 -ffast-math -ftree-vectorize
# Compile various performance-critical pieces *without* -fPIC -dynamic
# even when building a shared library. If we don't do this, then the
This diff is collapsed.
This diff is collapsed.
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment