Skip to content

Commit 3c39d46

Browse files
perf(allocator): hold Mutex lock for shortest possible time in FixedSizeAllocatorPool (#17099)
Small optimization. Hold `Mutex` lock for as shorter time as possible by dropping the `MutexGuard` before the `Condvar::notify_one` call.
1 parent 853f659 commit 3c39d46

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

crates/oxc_allocator/src/pool/fixed_size.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ impl FixedSizeAllocatorPool {
7272
/// # Panics
7373
/// * Panics if the underlying mutex is poisoned.
7474
pub fn get(&self) -> Allocator {
75-
// Try to get an allocator from the pool
75+
// Try to get an allocator from the pool.
76+
// This is in a block, so that `Mutex` lock is held for the shortest possible time.
7677
let maybe_allocator = {
7778
let mut allocators = self.allocators.lock().unwrap();
7879
allocators.pop()
@@ -88,6 +89,7 @@ impl FixedSizeAllocatorPool {
8889

8990
// Pool cannot produce another allocator. Wait for an existing allocator to be returned to the pool.
9091
loop {
92+
// This is in a block, so that `Mutex` lock is held for the shortest possible time
9193
let maybe_allocator = {
9294
let mut allocators = self.available.wait(self.allocators.lock().unwrap()).unwrap();
9395
allocators.pop()
@@ -140,8 +142,12 @@ impl FixedSizeAllocatorPool {
140142
FixedSizeAllocator { allocator: ManuallyDrop::new(allocator) };
141143
fixed_size_allocator.reset();
142144

143-
let mut allocators = self.allocators.lock().unwrap();
144-
allocators.push(fixed_size_allocator);
145+
// This is in a block, so that `Mutex` lock is held for the shortest possible time
146+
{
147+
let mut allocators = self.allocators.lock().unwrap();
148+
allocators.push(fixed_size_allocator);
149+
}
150+
145151
self.available.notify_one();
146152
}
147153
}

0 commit comments

Comments
 (0)