Skip to content

Commit 1d55140

Browse files
committed
use the right hashpower for the item_locks table
For some reason I can't understand, I was accessing item_locks via the main hash table's power level, then modulus'ed to the number of item locks. hashpower can change as the hash table grows, except it only ever changes while no item locks are being held (via the item_global_lock synchronization bits). The item_locks hashpower is static for the duration. So this isn't a safety issue, but instead just using the hash table wrong and doing an extra modulus. As an aside, this does improve benchmarks by a tiny bit.
1 parent d8b1047 commit 1d55140

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

thread.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static pthread_mutex_t cqi_freelist_lock;
5656
static pthread_mutex_t *item_locks;
5757
/* size of the item lock hash table */
5858
static uint32_t item_lock_count;
59+
static unsigned int item_lock_hashpower;
5960
#define hashsize(n) ((unsigned long int)1<<(n))
6061
#define hashmask(n) (hashsize(n)-1)
6162
/* this lock is temporarily engaged during a hash table expansion */
@@ -123,7 +124,7 @@ void item_unlock_global(void) {
123124
void item_lock(uint32_t hv) {
124125
uint8_t *lock_type = pthread_getspecific(item_lock_type_key);
125126
if (likely(*lock_type == ITEM_LOCK_GRANULAR)) {
126-
mutex_lock(&item_locks[(hv & hashmask(hashpower)) % item_lock_count]);
127+
mutex_lock(&item_locks[hv & hashmask(item_lock_hashpower)]);
127128
} else {
128129
mutex_lock(&item_global_lock);
129130
}
@@ -137,7 +138,7 @@ void item_lock(uint32_t hv) {
137138
* switch so it should stay safe.
138139
*/
139140
void *item_trylock(uint32_t hv) {
140-
pthread_mutex_t *lock = &item_locks[(hv & hashmask(hashpower)) % item_lock_count];
141+
pthread_mutex_t *lock = &item_locks[hv & hashmask(item_lock_hashpower)];
141142
if (pthread_mutex_trylock(lock) == 0) {
142143
return lock;
143144
}
@@ -151,7 +152,7 @@ void item_trylock_unlock(void *lock) {
151152
void item_unlock(uint32_t hv) {
152153
uint8_t *lock_type = pthread_getspecific(item_lock_type_key);
153154
if (likely(*lock_type == ITEM_LOCK_GRANULAR)) {
154-
mutex_unlock(&item_locks[(hv & hashmask(hashpower)) % item_lock_count]);
155+
mutex_unlock(&item_locks[hv & hashmask(item_lock_hashpower)]);
155156
} else {
156157
mutex_unlock(&item_global_lock);
157158
}
@@ -803,6 +804,7 @@ void thread_init(int nthreads, struct event_base *main_base) {
803804
}
804805

805806
item_lock_count = hashsize(power);
807+
item_lock_hashpower = power;
806808

807809
item_locks = calloc(item_lock_count, sizeof(pthread_mutex_t));
808810
if (! item_locks) {

0 commit comments

Comments
 (0)