Skip to content

Commit 6b0e5de

Browse files
committed
Don't rehash on retry in concurrent set
Since the hash should never change, we only need to calculate it once.
1 parent 6634969 commit 6b0e5de

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

concurrent_set.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,21 @@ rb_concurrent_set_find(VALUE *set_obj_ptr, VALUE key)
181181
RUBY_ASSERT(key >= CONCURRENT_SET_SPECIAL_VALUE_COUNT);
182182

183183
VALUE set_obj;
184+
VALUE hash = 0;
184185

185186
retry:
186187
set_obj = RUBY_ATOMIC_VALUE_LOAD(*set_obj_ptr);
187188
RUBY_ASSERT(set_obj);
188189
struct concurrent_set *set = RTYPEDDATA_GET_DATA(set_obj);
189190

191+
if (hash == 0) {
192+
// We don't need to recompute the hash on every retry because it should
193+
// never change.
194+
hash = set->funcs->hash(key);
195+
}
196+
RUBY_ASSERT(hash == set->funcs->hash(key));
197+
190198
struct concurrent_set_probe probe;
191-
VALUE hash = set->funcs->hash(key);
192199
int idx = concurrent_set_probe_start(&probe, set, hash);
193200

194201
while (true) {
@@ -237,14 +244,21 @@ rb_concurrent_set_find_or_insert(VALUE *set_obj_ptr, VALUE key, void *data)
237244

238245
bool inserting = false;
239246
VALUE set_obj;
247+
VALUE hash = 0;
240248

241249
retry:
242250
set_obj = RUBY_ATOMIC_VALUE_LOAD(*set_obj_ptr);
243251
RUBY_ASSERT(set_obj);
244252
struct concurrent_set *set = RTYPEDDATA_GET_DATA(set_obj);
245253

254+
if (hash == 0) {
255+
// We don't need to recompute the hash on every retry because it should
256+
// never change.
257+
hash = set->funcs->hash(key);
258+
}
259+
RUBY_ASSERT(hash == set->funcs->hash(key));
260+
246261
struct concurrent_set_probe probe;
247-
VALUE hash = set->funcs->hash(key);
248262
int idx = concurrent_set_probe_start(&probe, set, hash);
249263

250264
while (true) {

0 commit comments

Comments
 (0)