Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tasks/track_memory_allocations/allocs_semantic.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
File | File size || Sys allocs | Sys reallocs || Arena allocs | Arena reallocs | Arena bytes
-------------------------------------------------------------------------------------------------------------------------------------------
checker.ts | 2.92 MB || 2166 | 1015 || 0 | 0

cal.com.tsx | 1.06 MB || 14986 | 208 || 0 | 0

RadixUIAdoptionSection.jsx | 2.52 kB || 23 | 0 || 0 | 0

pdf.mjs | 567.30 kB || 1756 | 284 || 0 | 0

antd.js | 6.69 MB || 4139 | 502 || 0 | 0

binder.ts | 193.08 kB || 221 | 64 || 0 | 0

48 changes: 36 additions & 12 deletions tasks/track_memory_allocations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub fn run() -> Result<(), io::Error> {
let table_header = format_table_header(fixture_width, width);

let mut parser_out = table_header.clone();
let mut semantic_out = table_header.clone();
let mut minifier_out = table_header;

let mut allocator = Allocator::default();
Expand Down Expand Up @@ -163,12 +164,13 @@ pub fn run() -> Result<(), io::Error> {
allocator.reset();
reset_global_allocs();

let mut parsed = Parser::new(&allocator, &file.source_text, file.source_type)
.with_options(parse_options)
.parse();
assert!(parsed.errors.is_empty());

let parser_stats = record_stats(&allocator);
let (mut parsed, parser_stats) = record_stats_in(&allocator, || {
let parsed = Parser::new(&allocator, &file.source_text, file.source_type)
.with_options(parse_options)
.parse();
assert!(parsed.errors.is_empty());
parsed
});

parser_out.push_str(&format_table_row(
file.file_name.as_str(),
Expand All @@ -178,18 +180,27 @@ pub fn run() -> Result<(), io::Error> {
width,
));

let (scoping, semantic_stats) = record_stats_in(&allocator, || {
SemanticBuilder::new().build(&parsed.program).semantic.into_scoping()
});

semantic_out.push_str(&format_table_row(
file.file_name.as_str(),
file.source_text.len(),
&semantic_stats,
fixture_width,
width,
));

// Transform TypeScript to ESNext before minifying (minifier only works on esnext)
let scoping = SemanticBuilder::new().build(&parsed.program).semantic.into_scoping();
let transform_options = TransformOptions::from_target("esnext").unwrap();
let _ =
Transformer::new(&allocator, std::path::Path::new(&file.file_name), &transform_options)
.build_with_scoping(scoping, &mut parsed.program);

let before_minify_stats = record_stats(&allocator);

Minifier::new(minifier_options).minify(&allocator, &mut parsed.program);

let minifier_stats = record_stats_diff(&allocator, &before_minify_stats);
let ((), minifier_stats) = record_stats_in(&allocator, || {
Minifier::new(minifier_options).minify(&allocator, &mut parsed.program);
});

minifier_out.push_str(&format_table_row(
file.file_name.as_str(),
Expand All @@ -201,6 +212,7 @@ pub fn run() -> Result<(), io::Error> {
}

write_snapshot("tasks/track_memory_allocations/allocs_parser.snap", &parser_out)?;
write_snapshot("tasks/track_memory_allocations/allocs_semantic.snap", &semantic_out)?;
write_snapshot("tasks/track_memory_allocations/allocs_minifier.snap", &minifier_out)?;

Ok(())
Expand Down Expand Up @@ -231,6 +243,18 @@ fn record_stats_diff(allocator: &Allocator, prev: &AllocatorStats) -> AllocatorS
}
}

/// Records the allocations stats before and after the given closure is executed.
fn record_stats_in<F, R>(allocator: &Allocator, f: F) -> (R, AllocatorStats)
where
F: FnOnce() -> R,
{
let before_stats = record_stats(allocator);
let result = f();
let diff_stats = record_stats_diff(allocator, &before_stats);

(result, diff_stats)
}

/// Formats a single row of the allocator stats table
fn format_table_row(
file_name: &str,
Expand Down
Loading