Skip to content

Commit

Permalink
chore(es/minifier): Print slow files from minify-all example (#9899)
Browse files Browse the repository at this point in the history
**Description:**

I got

```
1970 files
Total size after minification: 46586141
Max duration (per file): 1.861938125s
Total run: Took 2.089914291s
```

This means that one single file is time-consuming, and we should optimize for the large files to reduce the total time.
  • Loading branch information
kdy1 authored Jan 19, 2025
1 parent ca2fd1e commit 2d87b89
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions crates/swc_ecma_minifier/examples/minify-all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

extern crate swc_malloc;

use std::{env, path::PathBuf, time::Instant};
use std::{
env,
path::PathBuf,
time::{Duration, Instant},
};

use anyhow::Result;
use rayon::prelude::*;
Expand Down Expand Up @@ -55,29 +59,38 @@ fn expand_dirs(dirs: Vec<String>) -> Vec<PathBuf> {
.collect()
}

#[derive(Default)]
struct Worker {
total_size: usize,
/// Single file max duration
max_duration: Duration,
}

impl Parallel for Worker {
fn create(&self) -> Self {
Worker { total_size: 0 }
Worker {
total_size: 0,
..*self
}
}

fn merge(&mut self, other: Self) {
self.total_size += other.total_size;
self.max_duration = self.max_duration.max(other.max_duration);
}
}

#[inline(never)] // For profiling
fn minify_all(files: &[PathBuf]) {
GLOBALS.set(&Default::default(), || {
let mut worker = Worker { total_size: 0 };
let mut worker = Worker::default();

worker.maybe_par(2, files, |worker, path| {
testing::run_test(false, |cm, handler| {
let fm = cm.load_file(path).expect("failed to load file");

let start = Instant::now();

let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();

Expand Down Expand Up @@ -120,14 +133,22 @@ fn minify_all(files: &[PathBuf]) {

let code = print(cm.clone(), &[output], true);

let duration = start.elapsed();

worker.total_size += code.len();
worker.max_duration = worker.max_duration.max(duration);

if duration > Duration::from_secs(1) {
eprintln!("{}: {:?}", path.display(), duration);
}

Ok(())
})
.unwrap()
});

eprintln!("Total size: {}", worker.total_size);
eprintln!("Max duration: {:?}", worker.max_duration);
});
}

Expand Down

0 comments on commit 2d87b89

Please sign in to comment.