Skip to content

Commit aed935a

Browse files
committed
refactor(allocator/pool): move resetting into AllocatorPool::add
1 parent cbef849 commit aed935a

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

crates/oxc_allocator/src/pool/fixed_size.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ impl AllocatorPool {
6666

6767
/// Add a [`FixedSizeAllocator`] to the pool.
6868
///
69-
/// The `Allocator` should be empty, ready to be re-used.
69+
/// The `Allocator` is reset by this method, so it's ready to be re-used.
7070
///
7171
/// # Panics
7272
///
7373
/// Panics if the underlying mutex is poisoned.
74-
fn add(&self, allocator: FixedSizeAllocator) {
74+
fn add(&self, mut allocator: FixedSizeAllocator) {
75+
allocator.reset();
7576
let mut allocators = self.allocators.lock().unwrap();
7677
allocators.push(allocator);
7778
}
@@ -97,8 +98,7 @@ impl Drop for AllocatorGuard<'_> {
9798
/// Return [`Allocator`] back to the pool.
9899
fn drop(&mut self) {
99100
// SAFETY: After taking ownership of the `FixedSizeAllocator`, we do not touch the `ManuallyDrop` again
100-
let mut allocator = unsafe { ManuallyDrop::take(&mut self.allocator) };
101-
allocator.reset();
101+
let allocator = unsafe { ManuallyDrop::take(&mut self.allocator) };
102102
self.pool.add(allocator);
103103
}
104104
}

crates/oxc_allocator/src/pool/standard.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ impl AllocatorPool {
3535

3636
/// Add an [`Allocator`] to the pool.
3737
///
38-
/// The `Allocator` should be empty, ready to be re-used.
38+
/// The `Allocator` is reset by this method, so it's ready to be re-used.
3939
///
4040
/// # Panics
4141
///
4242
/// Panics if the underlying mutex is poisoned.
43-
fn add(&self, allocator: Allocator) {
43+
fn add(&self, mut allocator: Allocator) {
44+
allocator.reset();
4445
let mut allocators = self.allocators.lock().unwrap();
4546
allocators.push(allocator);
4647
}
@@ -66,8 +67,7 @@ impl Drop for AllocatorGuard<'_> {
6667
/// Return [`Allocator`] back to the pool.
6768
fn drop(&mut self) {
6869
// SAFETY: After taking ownership of the `Allocator`, we do not touch the `ManuallyDrop` again
69-
let mut allocator = unsafe { ManuallyDrop::take(&mut self.allocator) };
70-
allocator.reset();
70+
let allocator = unsafe { ManuallyDrop::take(&mut self.allocator) };
7171
self.pool.add(allocator);
7272
}
7373
}

0 commit comments

Comments
 (0)