Skip to content

Commit 49dd15e

Browse files
committed
tracking works
1 parent 2f08582 commit 49dd15e

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "3"
3-
members = ["crates/*", "packages/napi", "tasks/benchmark"]
3+
members = ["crates/*", "packages/napi", "tasks/benchmark", "oxc_allocator"]
44
default-members = ["crates/*"]
55

66
[workspace.package]
@@ -11,11 +11,12 @@ homepage = "https://github.com/kermanx/jsshaker"
1111
keywords = ["JavaScript", "TypeScript", "oxc"]
1212
license = "MIT"
1313
repository = "https://github.com/kermanx/jsshaker"
14-
rust-version = "1.89.0" # Should sync with oxc
14+
rust-version = "1.89.0" # Should sync with oxc
1515
description = "Code size optimizer for JavaScript"
1616

1717
[workspace.dependencies]
1818
jsshaker = { path = "crates/jsshaker" }
19+
oxc_allocator = { path = "oxc_allocator" }
1920

2021
bitflags = "2.6.0"
2122
clap = "4.5.20"
@@ -25,7 +26,6 @@ flamescope = "0.1.3"
2526
flate2 = "1.1.0"
2627
line-index = "0.1.2"
2728
oxc = "0.107.0"
28-
oxc_allocator = { "version" = "0.107.0", features = ["track_allocations"] }
2929
oxc_ast_visit = "0.107.0"
3030
oxc_ecmascript = "0.107.0"
3131
oxc_index = "4.1.0"

crates/jsshaker/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ flame = { workspace = true, optional = true }
3131
flamescope = { workspace = true, optional = true }
3232
flate2 = { workspace = true }
3333
oxc = { workspace = true, features = ["codegen", "semantic", "minifier"] }
34+
oxc_allocator = { workspace = true, features = ["track_allocations"] }
3435
oxc_ast_visit = { workspace = true }
3536
oxc_ecmascript = { workspace = true }
3637
oxc_index = { workspace = true }

crates/jsshaker/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub fn tree_shake<F: Vfs + 'static>(options: JsShakerOptions<F>, entry: String)
7070
conditional_data,
7171
..
7272
} = unsafe { &mut *(&mut analyzer as *mut _) };
73+
74+
allocator.print_allocation_stats_types();
75+
7376
let mangler = Rc::new(RefCell::new(mangler));
7477
let mut codegen_return = FxHashMap::default();
7578
for module_info in mem::take(&mut modules.modules) {

oxc_allocator/src/alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl Alloc for Bump {
9999
// `track_allocations` feature, so should never be compiled in production code.
100100
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
101101
unsafe {
102-
crate::tracking::get_stats_ref(self).record_allocation();
102+
crate::tracking::get_stats_ref(self).record_allocation::<Layout>(0);
103103
}
104104

105105
self.alloc_layout(layout)

oxc_allocator/src/allocator.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl Allocator {
301301
const { assert!(!std::mem::needs_drop::<T>(), "Cannot allocate Drop type in arena") };
302302

303303
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
304-
self.stats.record_allocation();
304+
self.stats.record_allocation::<T>(std::mem::size_of::<T>());
305305

306306
self.bump.alloc(val)
307307
}
@@ -325,7 +325,7 @@ impl Allocator {
325325
#[inline(always)]
326326
pub fn alloc_str<'alloc>(&'alloc self, src: &str) -> &'alloc str {
327327
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
328-
self.stats.record_allocation();
328+
self.stats.record_allocation::<&str>(std::mem::size_of_val(src));
329329

330330
self.bump.alloc_str(src)
331331
}
@@ -348,7 +348,7 @@ impl Allocator {
348348
#[inline(always)]
349349
pub fn alloc_slice_copy<T: Copy>(&self, src: &[T]) -> &mut [T] {
350350
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
351-
self.stats.record_allocation();
351+
self.stats.record_allocation::<&[T]>(std::mem::size_of_val(src));
352352

353353
self.bump.alloc_slice_copy(src)
354354
}
@@ -363,7 +363,7 @@ impl Allocator {
363363
/// Panics if reserving space matching `layout` fails.
364364
pub fn alloc_layout(&self, layout: Layout) -> NonNull<u8> {
365365
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
366-
self.stats.record_allocation();
366+
self.stats.record_allocation::<Layout>(0);
367367

368368
self.bump.alloc_layout(layout)
369369
}
@@ -416,7 +416,7 @@ impl Allocator {
416416
);
417417

418418
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
419-
self.stats.record_allocation();
419+
self.stats.record_allocation::<[&str; N]>(total_len);
420420

421421
// Create actual `&str` in a separate function, to ensure that `alloc_concat_strs_array`
422422
// is inlined, so that compiler has knowledge to remove the overflow checks above.

oxc_allocator/src/tracking.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,22 @@ pub struct AllocationStats {
2929
num_alloc: Cell<usize>,
3030
/// Number of reallocations
3131
num_realloc: Cell<usize>,
32+
33+
types: std::cell::RefCell<std::collections::HashMap<&'static str, (usize, usize)>>
3234
}
3335

3436
impl AllocationStats {
3537
/// Record that an allocation was made.
36-
pub(crate) fn record_allocation(&self) {
38+
pub(crate) fn record_allocation<T>(&self, size: usize) {
3739
// Counter maxes out at `usize::MAX`, but if there's that many allocations,
3840
// the exact number is not important
3941
self.num_alloc.set(self.num_alloc.get().saturating_add(1));
42+
43+
let type_name = std::any::type_name::<T>();
44+
let mut map = self.types.borrow_mut();
45+
let entry = map.entry(type_name).or_insert((0, 0));
46+
entry.0 += 1; // 数量 +1
47+
entry.1 += size; // 总字节数 + size
4048
}
4149

4250
/// Record that a reallocation was made.
@@ -61,6 +69,15 @@ impl Allocator {
6169
let num_realloc = self.stats.num_realloc.get();
6270
(num_alloc, num_realloc)
6371
}
72+
73+
pub fn print_allocation_stats_types(&self) {
74+
let map = self.stats.types.borrow();
75+
let mut entries: Vec<_> = map.iter().collect();
76+
entries.sort_by(|a, b| b.1.1.cmp(&a.1.1));
77+
for (name, (count, size)) in entries {
78+
println!("{size}\t{count}\t{name}");
79+
}
80+
}
6481
}
6582

6683
/// Get reference to [`AllocationStats`] for a [`Bump`].

0 commit comments

Comments
 (0)