Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: parallel generate, speed up 10% #1001

Merged
merged 6 commits into from
Apr 3, 2024
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/mako/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ glob-match = "0.2.1"
heck = "0.4.1"
mako_core = { path = "../core" }
miette = { version = "5.10.0", features = ["fancy"] }
nanoid = "0.4.0"
oxc_resolver = "1.5.4"
percent-encoding = { version = "2.3.1" }
serde = { workspace = true }
Expand Down
26 changes: 13 additions & 13 deletions crates/mako/src/chunk_pot.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod ast_impl;
mod str_impl;
pub mod util;

use std::collections::HashMap;
use std::sync::Arc;
use std::vec;
Expand All @@ -12,6 +11,7 @@ use mako_core::swc_css_ast::Stylesheet;
use mako_core::ternary;

use crate::chunk::{Chunk, ChunkType};
pub use crate::chunk_pot::util::CHUNK_FILE_NAME_HASH_LENGTH;
use crate::chunk_pot::util::{hash_hashmap, hash_vec};
use crate::compiler::Context;
use crate::config::Mode;
Expand All @@ -25,25 +25,25 @@ pub struct ChunkPot<'a> {
pub js_name: String,
pub module_map: HashMap<String, (&'a Module, u64)>,
pub js_hash: u64,
stylesheet: Option<CssModules<'a>>,
pub stylesheet: Option<CssModules<'a>>,
}

impl<'cp> ChunkPot<'cp> {
pub fn from<'a: 'cp>(
chunk: &'a Chunk,
mg: &'a ModuleGraph,
context: &'cp Arc<Context>,
) -> Result<Self> {
let (js_modules, stylesheet) = ChunkPot::split_modules(chunk.get_modules(), mg, context)?;
) -> Self {
let (js_modules, stylesheet) = ChunkPot::split_modules(chunk.get_modules(), mg, context);

Ok(ChunkPot {
ChunkPot {
js_name: chunk.filename(),
chunk_type: chunk.chunk_type.clone(),
chunk_id: chunk.id.id.clone(),
module_map: js_modules.module_map,
js_hash: js_modules.raw_hash,
stylesheet,
})
}
}

pub fn to_normal_chunk_files(
Expand Down Expand Up @@ -130,7 +130,7 @@ impl<'cp> ChunkPot<'cp> {
module_ids: &'a IndexSet<ModuleId>,
module_graph: &'a ModuleGraph,
context: &'a Arc<Context>,
) -> Result<(JsModules<'a>, Option<CssModules<'a>>)> {
) -> (JsModules<'a>, Option<CssModules<'a>>) {
mako_core::mako_profile_function!(module_ids.len().to_string());
let mut module_map: HashMap<String, (&Module, u64)> = Default::default();
let mut merged_css_modules: Vec<(String, &Stylesheet)> = vec![];
Expand Down Expand Up @@ -177,7 +177,7 @@ impl<'cp> ChunkPot<'cp> {

let css_raw_hash = hash_vec(&css_raw_hashes);

Ok((
(
JsModules {
module_map,
raw_hash,
Expand All @@ -186,15 +186,15 @@ impl<'cp> ChunkPot<'cp> {
stylesheets,
raw_hash: css_raw_hash,
}),
))
)
} else {
Ok((
(
JsModules {
module_map,
raw_hash,
},
None,
))
)
}
}
}
Expand All @@ -204,12 +204,12 @@ struct JsModules<'a> {
raw_hash: u64,
}

struct CssModules<'a> {
pub struct CssModules<'a> {
stylesheets: Vec<&'a Stylesheet>,
raw_hash: u64,
}

fn get_css_chunk_filename(js_chunk_filename: &str) -> String {
pub fn get_css_chunk_filename(js_chunk_filename: &str) -> String {
format!(
"{}.css",
js_chunk_filename.strip_suffix(".js").unwrap_or("")
Expand Down
7 changes: 5 additions & 2 deletions crates/mako/src/chunk_pot/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,11 @@ fn to_module_fn_expr(module: &Module) -> Result<FnExpr> {
}
}

pub const CHUNK_FILE_NAME_HASH_LENGTH: usize = 8;

pub fn file_content_hash<T: AsRef<[u8]>>(content: T) -> String {
let digest = md5::compute(content);
let hash = format!("{:x}", digest);
hash[0..8].to_string()
let mut hash = format!("{:x}", digest);
hash.truncate(CHUNK_FILE_NAME_HASH_LENGTH);
hash
}
Loading
Loading