7
7
* The implementation is one round of the xorshift64* algorithm.
8
8
* Code Source: Wikipedia
9
9
*/
10
- uint64_t hashFn (uint64_t x ) {
10
+ inline uint64_t hashFn (uint64_t x ) {
11
11
x ^= x >> 12 ; // a
12
12
x ^= x << 25 ; // b
13
13
x ^= x >> 27 ; // c
14
14
return x * UINT64_C (2685821657736338717 );
15
15
}
16
16
17
- uint64_t computeBucketIndex (statepoint_table_t * table , uint64_t key ) {
17
+ inline uint64_t computeBucketIndex (statepoint_table_t * table , uint64_t key ) {
18
18
// Using modulo may introduce a little bias in the table.
19
19
// If you care, use the unbiased version that's floating around the internet.
20
- return hashFn (key ) % table -> size ;
20
+
21
+ // NOTE: we use bitwise AND instead of modulo because the size
22
+ // is a power-of-two. This is very important for performance.
23
+ return hashFn (key ) & (table -> size - 1 );
21
24
}
22
25
23
- size_t size_of_frame (uint16_t numSlots ) {
26
+ inline size_t size_of_frame (uint16_t numSlots ) {
24
27
return sizeof (frame_info_t ) + numSlots * sizeof (pointer_slot_t );
25
28
}
26
29
27
- size_t frame_size (frame_info_t * frame ) {
30
+ inline size_t frame_size (frame_info_t * frame ) {
28
31
return size_of_frame (frame -> numSlots );
29
32
}
30
33
31
34
// returns the next frame relative the current frame
32
- frame_info_t * next_frame (frame_info_t * cur ) {
35
+ inline frame_info_t * next_frame (frame_info_t * cur ) {
33
36
uint8_t * next = ((uint8_t * )cur ) + frame_size (cur );
34
37
return (frame_info_t * )next ;
35
38
}
@@ -41,6 +44,11 @@ statepoint_table_t* new_table(float loadFactor, uint64_t expectedElms) {
41
44
42
45
uint64_t numBuckets = (expectedElms / loadFactor ) + 1 ;
43
46
47
+ // round up to nearest power of two. this implementation requires
48
+ // it for the performance of lookup_return_address
49
+ uint64_t factor = ceil (log2 ((double ) numBuckets ));
50
+ numBuckets = 1ULL << factor ;
51
+
44
52
table_bucket_t * buckets = calloc (numBuckets , sizeof (table_bucket_t ));
45
53
assert (buckets && "bad alloc" );
46
54
0 commit comments