Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit fbb2ef0

Browse files
committed
chore(hash*): minor performance tuning
1 parent 9283162 commit fbb2ef0

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Resizing of a [`HashMap`](#hashmap) is entirely non-blocking and lock-free; resi
3636

3737
### Examples
3838

39-
If the key is unique, an entry can be inserted. The inserted entry can be updated, read, and removed synchronously or asynchronously.
39+
Inserted entries can be updated, read, and removed synchronously or asynchronously.
4040

4141
```rust
4242
use scc::HashMap;

src/hash_table.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,29 @@ where
13231323
return;
13241324
}
13251325

1326+
if self
1327+
.minimum_capacity_var()
1328+
.fetch_update(AcqRel, Acquire, |lock_state| {
1329+
if lock_state >= RESIZING {
1330+
None
1331+
} else {
1332+
Some(lock_state + RESIZING)
1333+
}
1334+
})
1335+
.is_err()
1336+
{
1337+
// The bucket array is being replaced with a new one.
1338+
return;
1339+
}
1340+
let _lock_guard = ExitGuard::new((), |()| {
1341+
self.minimum_capacity_var().fetch_sub(RESIZING, Release);
1342+
});
1343+
1344+
if self.bucket_array_var().load(Acquire, guard) != current_array_ptr {
1345+
// Resized in the meantime.
1346+
return;
1347+
}
1348+
13261349
let minimum_capacity = self.minimum_capacity();
13271350
let capacity = current_array.num_slots();
13281351
let estimated_num_entries = Self::sample(current_array, sampling_index);
@@ -1362,28 +1385,6 @@ where
13621385
return;
13631386
}
13641387

1365-
if self
1366-
.minimum_capacity_var()
1367-
.fetch_update(AcqRel, Acquire, |lock_state| {
1368-
if lock_state >= RESIZING {
1369-
None
1370-
} else {
1371-
Some(lock_state + RESIZING)
1372-
}
1373-
})
1374-
.is_err()
1375-
{
1376-
// The bucket array is being replaced with a new one.
1377-
return;
1378-
}
1379-
let _lock_guard = ExitGuard::new((), |()| {
1380-
self.minimum_capacity_var().fetch_sub(RESIZING, Release);
1381-
});
1382-
1383-
if self.bucket_array_var().load(Acquire, guard) != current_array_ptr {
1384-
return;
1385-
}
1386-
13871388
if try_drop_table {
13881389
// Try to drop the hash table with all the buckets locked.
13891390
let mut writer_guard = ExitGuard::new((0, false), |(len, success): (usize, bool)| {

0 commit comments

Comments
 (0)