Skip to content

Commit 9f84d46

Browse files
committed
refactor(allocator): replace AtomicUsize with Cell<usize> in AllocationStats
1 parent 76d7ef7 commit 9f84d46

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

crates/oxc_allocator/src/tracking.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,45 @@
1616
//! As soon as we replace `bumpalo` with our own arena allocator, we'll remove the hack from `get_stats_ref`,
1717
//! and make this sound.
1818
19-
use std::{
20-
ptr,
21-
sync::atomic::{AtomicUsize, Ordering::SeqCst},
22-
};
19+
use std::{cell::Cell, ptr};
2320

2421
use bumpalo::Bump;
2522

2623
use crate::{Allocator, allocator::STATS_FIELD_OFFSET};
2724

2825
/// Counters of allocations and reallocations made in an [`Allocator`].
29-
//
30-
// Note: These fields could be `Cell<usize>` instead of `AtomicUsize`, because `Allocator` should not
31-
// be `Sync`. But currently it is (which is unsound!) because of other terrible hacks.
3226
#[derive(Default)]
3327
pub struct AllocationStats {
3428
/// Number of allocations
35-
num_alloc: AtomicUsize,
29+
num_alloc: Cell<usize>,
3630
/// Number of reallocations
37-
num_realloc: AtomicUsize,
31+
num_realloc: Cell<usize>,
3832
}
3933

4034
impl AllocationStats {
4135
/// Record that an allocation was made.
4236
pub(crate) fn record_allocation(&self) {
43-
self.num_alloc.fetch_add(1, SeqCst);
37+
self.num_alloc.set(self.num_alloc.get() + 1);
4438
}
4539

4640
/// Record that a reallocation was made.
4741
pub(crate) fn record_reallocation(&self) {
48-
self.num_realloc.fetch_add(1, SeqCst);
42+
self.num_realloc.set(self.num_realloc.get() + 1);
4943
}
5044

5145
/// Reset allocation counters.
5246
pub(crate) fn reset(&self) {
53-
self.num_alloc.store(0, SeqCst);
54-
self.num_realloc.store(0, SeqCst);
47+
self.num_alloc.set(0);
48+
self.num_realloc.set(0);
5549
}
5650
}
5751

5852
impl Allocator {
5953
/// Get number of allocations and reallocations made in this [`Allocator`].
6054
#[doc(hidden)]
6155
pub fn get_allocation_stats(&self) -> (usize, usize) {
62-
let num_alloc = self.stats.num_alloc.load(SeqCst);
63-
let num_realloc = self.stats.num_realloc.load(SeqCst);
56+
let num_alloc = self.stats.num_alloc.get();
57+
let num_realloc = self.stats.num_realloc.get();
6458
(num_alloc, num_realloc)
6559
}
6660
}

0 commit comments

Comments
 (0)