-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use 64 bit hash and fix out of bounds access (#2102)
There don't seem to be any statistically significant changes in performance. I filtered Ruby processes and ran `loop { puts :a }` in a loop (see test plan). Test Plan ======= https://pprof.me/fc0fff3/ ``` sudo bpftool prog | grep profile_cpu ``` ``` 2938: perf_event name profile_cpu tag 4554576c1d62dc61 gpl run_time_ns 136221451 run_cnt 24408 -> 5581.02 ns / run 2268 run_cnt 60823512 cycles -> 26818.13 cycles / run 42618949 instructions -> 18791.42 instructions / run # 0.70 insns per cycle ``` ``` 3004: perf_event name profile_cpu tag 75f4ac73b948f3c0q gpl run_time_ns 144678180 run_cnt 23700 -> 6104.56 ns / run 2279 run_cnt 60910573 cycles -> 26726.89 cycles / run 42422810 instructions -> 18614.66 instructions / run # 0.70 insns per cycle ```
- Loading branch information
Showing
8 changed files
with
24 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,26 @@ | ||
#include "common.h" | ||
|
||
// Avoid pulling in any other headers. | ||
typedef unsigned int uint32_t; | ||
|
||
// murmurhash2 from | ||
// https://github.com/aappleby/smhasher/blob/92cf3702fcfaadc84eb7bef59825a23e0cd84f56/src/MurmurHash2.cpp | ||
uint32_t MurmurHash2(const void *key, int len, uint32_t seed) { | ||
/* 'm' and 'r' are mixing constants generated offline. | ||
They're not really 'magic', they just happen to work well. */ | ||
|
||
const uint32_t m = 0x5bd1e995; | ||
const int r = 24; | ||
|
||
/* Initialize the hash to a 'random' value */ | ||
// https://github.com/aappleby/smhasher/blob/92cf3702fcfaadc84eb7bef59825a23e0cd84f56/src/MurmurHash2.cpp/* */ | ||
|
||
uint32_t h = seed ^ len; | ||
unsigned long long hash_stack(stack_trace_t *stack, int seed) { | ||
const unsigned long long m = 0xc6a4a7935bd1e995LLU; | ||
const int r = 47; | ||
unsigned long long hash = seed ^ (stack->len * m); | ||
|
||
/* Mix 4 bytes at a time into the hash */ | ||
|
||
const unsigned char *data = (const unsigned char *)key; | ||
// MAX_STACK_DEPTH * 2 = 256 (because we hash 32 bits at a time). | ||
for (int i = 0; i < 256; i++) { | ||
if (len < 4) { | ||
break; | ||
} | ||
uint32_t k = *(uint32_t *)data; | ||
for(int i=0; i<MAX_STACK_DEPTH; i++){ | ||
unsigned long long k = stack->addresses[i]; | ||
|
||
k *= m; | ||
k ^= k >> r; | ||
k *= m; | ||
|
||
h *= m; | ||
h ^= k; | ||
|
||
data += 4; | ||
len -= 4; | ||
hash ^= k; | ||
hash *= m; | ||
} | ||
|
||
/* Handle the last few bytes of the input array */ | ||
|
||
switch (len) { | ||
case 3: | ||
h ^= data[2] << 16; | ||
case 2: | ||
h ^= data[1] << 8; | ||
case 1: | ||
h ^= data[0]; | ||
h *= m; | ||
}; | ||
|
||
/* Do a few final mixes of the hash to ensure the last few | ||
// bytes are well-incorporated. */ | ||
|
||
h ^= h >> 13; | ||
h *= m; | ||
h ^= h >> 15; | ||
|
||
return h; | ||
return hash; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters