Skip to content

Commit

Permalink
feat(es/minifier): Print total size from minify-all example (#9897)
Browse files Browse the repository at this point in the history
**Description:**

I need this information to know if an optimization is worth the cost
  • Loading branch information
kdy1 authored Jan 19, 2025
1 parent 489f5fd commit 134000f
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions crates/swc_ecma_minifier/examples/minify-all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extern crate swc_malloc;

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

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

struct Worker;
struct Worker {
total_size: usize,
}

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

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

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

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

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

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

fs::write("output.js", code.as_bytes()).expect("failed to write output");
worker.total_size += code.len();

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

eprintln!("Total size: {}", worker.total_size);
});
}

Expand Down

0 comments on commit 134000f

Please sign in to comment.