Skip to content

Commit 0ce07cd

Browse files
committed
perf: use rustc-hash
1 parent 544c3b7 commit 0ce07cd

52 files changed

Lines changed: 220 additions & 205 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ convert_case = "0.6.0"
9191
path-clean = "1.0.1"
9292
sailfish = "0.8.3"
9393
merge-source-map = "1.2.0"
94+
rustc-hash = "1.1.0"
9495

9596
[features]
9697
profile = ["dep:eframe", "dep:puffin", "dep:puffin_egui"]

crates/core/src/collections.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pub use rustchash::{FxHashMap as HashMap, FxHashSet as HashSet};
2+
3+
mod rustchash {
4+
use std::collections::{HashMap, HashSet};
5+
use std::hash::BuildHasherDefault;
6+
7+
use rustc_hash::FxHasher;
8+
9+
pub type FxRandomState = BuildHasherDefault<FxHasher>;
10+
11+
pub type FxHashMap<K, V> = HashMap<K, V, FxRandomState>;
12+
13+
pub type FxHashSet<V> = HashSet<V, FxRandomState>;
14+
}
15+
16+
#[macro_export(local_inner_macros)]
17+
macro_rules! hashmap {
18+
(@single $($x:tt)*) => (());
19+
(@count $($rest:expr),*) => (<[()]>::len(&[$(hashmap!(@single $rest)),*]));
20+
21+
($($key:expr => $value:expr,)+) => { hashmap!($($key => $value),+) };
22+
($($key:expr => $value:expr),*) => {
23+
{
24+
let _cap = hashmap!(@count $($key),*);
25+
let mut _map = $crate::collections::HashMap::default();
26+
$(
27+
let _ = _map.insert($key, $value);
28+
)*
29+
_map
30+
}
31+
};
32+
}

crates/core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub use {
2929
tracing_subscriber, tungstenite, twox_hash,
3030
};
3131

32+
pub mod collections;
33+
3234
#[macro_export]
3335
macro_rules! mako_profile_scope {
3436
($id:expr) => {

crates/mako/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ glob-match = "0.2.1"
2121

2222
[dev-dependencies]
2323
insta = { version = "1.30.0", features = ["yaml"] }
24-
maplit = "1.0.2"
2524
testing = "0.35.10"
2625
swc_ecma_transforms_testing = "0.136.2"
2726

crates/mako/src/build.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::collections::{HashMap, HashSet};
21
use std::path::PathBuf;
32
use std::sync::mpsc::Sender;
43
use std::sync::Arc;
54
use std::time::Instant;
65

76
use cached::proc_macro::cached;
87
use mako_core::anyhow::{anyhow, Result};
8+
use mako_core::collections::{HashMap, HashSet};
99
use mako_core::colored::Colorize;
1010
use mako_core::lazy_static::lazy_static;
1111
use mako_core::rayon::ThreadPool;
@@ -82,7 +82,7 @@ impl Compiler {
8282
pub fn build_tasks(&self, tasks: Vec<Task>, with_cache: bool) -> Result<HashSet<ModuleId>> {
8383
debug!("build tasks: {:?}", tasks);
8484
if tasks.is_empty() {
85-
return Ok(HashSet::new());
85+
return Ok(HashSet::default());
8686
}
8787

8888
let (pool, rs, rr) = create_thread_pool::<Result<(Module, ModuleDeps, Task)>>();
@@ -99,7 +99,7 @@ impl Compiler {
9999
}
100100

101101
let mut errors = vec![];
102-
let mut module_ids = HashSet::new();
102+
let mut module_ids = HashSet::default();
103103
for r in rr {
104104
count -= 1;
105105
match r {
@@ -279,7 +279,7 @@ module.exports = new Promise((resolve, reject) => {{
279279
raw: code,
280280
raw_hash: 0,
281281
resolved_resource: Some(resource.clone()),
282-
missing_deps: HashMap::new(),
282+
missing_deps: HashMap::default(),
283283
ignored_deps: vec![],
284284
top_level_await: false,
285285
is_async: has_script,
@@ -342,7 +342,7 @@ module.exports = new Promise((resolve, reject) => {{
342342
// resolve
343343
let mut dep_resolve_err = None;
344344
let mut dependencies_resource = Vec::new();
345-
let mut missing_deps = HashMap::new();
345+
let mut missing_deps = HashMap::default();
346346
let mut ignored_deps = Vec::new();
347347

348348
for dep in deps {
@@ -370,7 +370,7 @@ module.exports = new Promise((resolve, reject) => {{
370370
let source = e.1;
371371
let span = e.3;
372372
// 使用 hasMap 记录循环依赖
373-
let mut target_map: HashMap<String, i32> = HashMap::new();
373+
let mut target_map: HashMap<String, i32> = HashMap::default();
374374
target_map.insert(target, 1);
375375

376376
let mut err = format!("Module not found: Can't resolve '{}'", source);

crates/mako/src/chunk_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::collections::{HashMap, HashSet};
21
use std::hash::Hasher;
32

3+
use mako_core::collections::{HashMap, HashSet};
44
use mako_core::petgraph::stable_graph::{DefaultIx, NodeIndex, StableDiGraph};
55
use mako_core::petgraph::visit::Dfs;
66
use mako_core::petgraph::Direction;
@@ -19,7 +19,7 @@ impl ChunkGraph {
1919
pub fn new() -> Self {
2020
Self {
2121
graph: StableDiGraph::new(),
22-
id_index_map: HashMap::new(),
22+
id_index_map: HashMap::default(),
2323
}
2424
}
2525

crates/mako/src/chunk_pot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ mod ast_impl;
22
mod str_impl;
33
pub mod util;
44

5-
use std::collections::HashMap;
65
use std::sync::Arc;
76
use std::vec;
87

98
use mako_core::anyhow::Result;
9+
use mako_core::collections::HashMap;
1010
use mako_core::indexmap::IndexSet;
1111
use mako_core::swc_css_ast::Stylesheet;
1212
use mako_core::ternary;

crates/mako/src/chunk_pot/ast_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::collections::HashMap;
21
use std::sync::Arc;
32

43
use cached::proc_macro::cached;
54
use mako_core::anyhow::Result;
65
use mako_core::cached::SizedCache;
6+
use mako_core::collections::HashMap;
77
use mako_core::swc_common::{Mark, DUMMY_SP, GLOBALS};
88
use mako_core::swc_css_ast::Stylesheet;
99
use mako_core::swc_css_codegen::writer::basic::{BasicCssWriter, BasicCssWriterConfig};

crates/mako/src/chunk_pot/str_impl.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::collections::HashMap;
21
use std::sync::Arc;
32

43
use cached::proc_macro::cached;
54
use mako_core::anyhow::{anyhow, Result};
65
use mako_core::cached::SizedCache;
6+
use mako_core::collections::HashMap;
77
use mako_core::rayon::prelude::*;
88
use mako_core::swc_ecma_codegen::text_writer::JsWriter;
99
use mako_core::swc_ecma_codegen::{Config as JsCodegenConfig, Emitter};
@@ -223,11 +223,7 @@ fn pot_to_chunk_module_object_string(pot: &ChunkPot, context: &Arc<Context>) ->
223223
let sorted_kv = {
224224
mako_core::mako_profile_scope!("collect_&_sort");
225225

226-
let mut sorted_kv = pot
227-
.module_map
228-
.iter()
229-
.map(|(k, v)| (k, v))
230-
.collect::<Vec<_>>();
226+
let mut sorted_kv = pot.module_map.iter().collect::<Vec<_>>();
231227

232228
if context.config.hash {
233229
sorted_kv.sort_by_key(|(k, _)| *k);

0 commit comments

Comments
 (0)