Skip to content

Commit b657bb6

Browse files
committed
perf(allocator): reduce time Mutex lock is held in FixedSizeAllocatorPool::get (#17079)
Follow-on after #17023. Small perf optimization. Minimize the time that the `Mutex` is locked, by dropping the `MutexGuard` before checking whether `pop()` returned `Some` or not , rather than after.
1 parent 40a30fe commit b657bb6

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

crates/oxc_allocator/src/pool/fixed_size.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ impl FixedSizeAllocatorPool {
7373
/// * Panics if the underlying mutex is poisoned.
7474
pub fn get(&self) -> Allocator {
7575
// Try to get an allocator from the pool
76-
{
77-
let maybe_allocator = self.allocators.lock().unwrap().pop();
78-
if let Some(allocator) = maybe_allocator {
79-
return allocator.into_inner();
80-
}
76+
let maybe_allocator = {
77+
let mut allocators = self.allocators.lock().unwrap();
78+
allocators.pop()
79+
};
80+
if let Some(allocator) = maybe_allocator {
81+
return allocator.into_inner();
8182
}
8283

8384
// Pool is empty. Try to create a new allocator.
@@ -87,8 +88,11 @@ impl FixedSizeAllocatorPool {
8788

8889
// Pool cannot produce another allocator. Wait for an existing allocator to be returned to the pool.
8990
loop {
90-
let mut maybe_allocator = self.available.wait(self.allocators.lock().unwrap()).unwrap();
91-
if let Some(allocator) = maybe_allocator.pop() {
91+
let maybe_allocator = {
92+
let mut allocators = self.available.wait(self.allocators.lock().unwrap()).unwrap();
93+
allocators.pop()
94+
};
95+
if let Some(allocator) = maybe_allocator {
9296
return allocator.into_inner();
9397
}
9498
}

0 commit comments

Comments
 (0)