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

refactor: Use ahash from swc_common in more places #7815

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 1 addition & 13 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ plugin_transform_host_native = [
]

[dependencies]
ahash = "0.8.3"
anyhow = "1"
base64 = "0.13.0"
dashmap = "5.4.0"
Expand Down
19 changes: 9 additions & 10 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use swc_atoms::JsWord;
use swc_cached::regex::CachedRegex;
use swc_common::{
chain,
collections::{AHashMap, AHashSet},
collections::{AHashMap, AHashSet, ARandomState},
comments::{Comments, SingleThreadedComments},
errors::Handler,
plugin::metadata::TransformPluginMetadataContext,
Expand Down Expand Up @@ -1497,7 +1497,7 @@ impl Default for ErrorFormat {
}

/// `paths` section of `tsconfig.json`.
pub type Paths = IndexMap<String, Vec<String>, ahash::RandomState>;
pub type Paths = IndexMap<String, Vec<String>, ARandomState>;
pub(crate) type CompiledPaths = Vec<(String, Vec<String>)>;

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -1746,7 +1746,7 @@ pub struct ErrorConfig {
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct GlobalPassOption {
#[serde(default)]
pub vars: IndexMap<JsWord, JsWord, ahash::RandomState>,
pub vars: IndexMap<JsWord, JsWord, ARandomState>,
#[serde(default)]
pub envs: GlobalInliningPassEnvs,

Expand Down Expand Up @@ -1826,7 +1826,7 @@ impl GlobalPassOption {
} else {
match &self.envs {
GlobalInliningPassEnvs::List(env_list) => {
static CACHE: Lazy<DashMap<Vec<String>, ValuesMap, ahash::RandomState>> =
static CACHE: Lazy<DashMap<Vec<String>, ValuesMap, ARandomState>> =
Lazy::new(Default::default);

let cache_key = env_list.iter().cloned().collect::<Vec<_>>();
Expand All @@ -1847,9 +1847,8 @@ impl GlobalPassOption {
}

GlobalInliningPassEnvs::Map(map) => {
static CACHE: Lazy<
DashMap<Vec<(JsWord, JsWord)>, ValuesMap, ahash::RandomState>,
> = Lazy::new(Default::default);
static CACHE: Lazy<DashMap<Vec<(JsWord, JsWord)>, ValuesMap, ARandomState>> =
Lazy::new(Default::default);

let cache_key = self
.vars
Expand All @@ -1873,7 +1872,7 @@ impl GlobalPassOption {
};

let global_exprs = {
static CACHE: Lazy<DashMap<Vec<(JsWord, JsWord)>, GlobalExprMap, ahash::RandomState>> =
static CACHE: Lazy<DashMap<Vec<(JsWord, JsWord)>, GlobalExprMap, ARandomState>> =
Lazy::new(Default::default);

let cache_key = self
Expand Down Expand Up @@ -1904,7 +1903,7 @@ impl GlobalPassOption {
};

let global_map = {
static CACHE: Lazy<DashMap<Vec<(JsWord, JsWord)>, ValuesMap, ahash::RandomState>> =
static CACHE: Lazy<DashMap<Vec<(JsWord, JsWord)>, ValuesMap, ARandomState>> =
Lazy::new(Default::default);

let cache_key = self
Expand Down Expand Up @@ -1943,7 +1942,7 @@ fn default_env_name() -> String {
}

fn build_resolver(base_url: PathBuf, paths: CompiledPaths) -> Box<SwcImportResolver> {
static CACHE: Lazy<DashMap<(PathBuf, CompiledPaths), SwcImportResolver, ahash::RandomState>> =
static CACHE: Lazy<DashMap<(PathBuf, CompiledPaths), SwcImportResolver, ARandomState>> =
Lazy::new(Default::default);

if let Some(cached) = CACHE.get(&(base_url.clone(), paths.clone())) {
Expand Down
1 change: 0 additions & 1 deletion crates/swc_bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ concurrent = ["swc_common/concurrent", "dashmap", "rayon", "indexmap/rayon"]
default = []

[dependencies]
ahash = "0.8"
anyhow = "1"
crc = "2.1.0"
dashmap = { version = "5.4.0", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/bundler/chunk/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use indexmap::IndexSet;
use petgraph::EdgeDirection;
use swc_atoms::js_word;
use swc_common::{
collections::{AHashMap, AHashSet},
collections::{AHashMap, AHashSet, ARandomState},
sync::Lock,
FileName, SyntaxContext, DUMMY_SP,
};
Expand Down Expand Up @@ -138,7 +138,7 @@ where
graph: &ModuleGraph,
start: ModuleId,
dejavu: &mut AHashSet<ModuleId>,
) -> IndexSet<ModuleId, ahash::RandomState> {
) -> IndexSet<ModuleId, ARandomState> {
let mut set = IndexSet::default();

for dep in graph.neighbors_directed(start, Outgoing) {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/bundler/export.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use indexmap::IndexMap;
use swc_atoms::{js_word, JsWord};
use swc_common::{FileName, SyntaxContext};
use swc_common::{collections::ARandomState, FileName, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
Expand Down Expand Up @@ -42,7 +42,7 @@ where
#[derive(Debug, Default)]
pub(super) struct RawExports {
/// Key is None if it's exported from the module itself.
pub items: IndexMap<Option<Str>, Vec<Specifier>, ahash::RandomState>,
pub items: IndexMap<Option<Str>, Vec<Specifier>, ARandomState>,
}

#[derive(Debug, Default)]
Expand Down
3 changes: 1 addition & 2 deletions crates/swc_bundler/src/bundler/finalize.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::path::{Path, PathBuf};

use ahash::AHashMap;
use anyhow::Error;
use relative_path::RelativePath;
use swc_atoms::js_word;
use swc_common::{util::move_map::MoveMap, FileName, Mark, DUMMY_SP};
use swc_common::{collections::AHashMap, util::move_map::MoveMap, FileName, Mark, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::{
fixer::fixer,
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_bundler/src/bundler/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;

use anyhow::Error;
use indexmap::IndexMap;
use swc_common::{sync::Lrc, FileName, SourceMap, Span, GLOBALS};
use swc_common::{collections::ARandomState, sync::Lrc, FileName, SourceMap, Span, GLOBALS};
use swc_ecma_ast::*;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput};
use swc_ecma_utils::drop_span;
Expand All @@ -19,7 +19,7 @@ pub(crate) struct Tester<'a> {

pub struct Loader {
cm: Lrc<SourceMap>,
files: IndexMap<String, String, ahash::RandomState>,
files: IndexMap<String, String, ARandomState>,
}

impl Load for Loader {
Expand Down Expand Up @@ -108,7 +108,7 @@ pub(crate) fn suite() -> TestBuilder {

#[derive(Default)]
pub(crate) struct TestBuilder {
files: IndexMap<String, String, ahash::RandomState>,
files: IndexMap<String, String, ARandomState>,
}

impl TestBuilder {
Expand Down
8 changes: 6 additions & 2 deletions crates/swc_bundler/src/modules/sort/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use std::{collections::VecDeque, iter::from_fn, mem::take, time::Instant};

use indexmap::IndexSet;
use petgraph::EdgeDirection::Outgoing;
use swc_common::{collections::AHashSet, sync::Lrc, SourceMap, SyntaxContext};
use swc_common::{
collections::{AHashSet, ARandomState},
sync::Lrc,
SourceMap, SyntaxContext,
};
use swc_ecma_ast::*;
use swc_ecma_utils::prepend_stmts;

Expand Down Expand Up @@ -133,7 +137,7 @@ fn cycles_for(
cycles: &[Vec<ModuleId>],
id: ModuleId,
checked: &mut Vec<ModuleId>,
) -> IndexSet<ModuleId, ahash::RandomState> {
) -> IndexSet<ModuleId, ARandomState> {
checked.push(id);
let mut v = cycles
.iter()
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_bundler/src/modules/sort/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use indexmap::IndexSet;
use petgraph::EdgeDirection::{Incoming as Dependants, Outgoing as Dependencies};
use swc_atoms::js_word;
use swc_common::{
collections::{AHashMap, AHashSet},
collections::{AHashMap, AHashSet, ARandomState},
sync::Lrc,
util::take::Take,
SourceMap, Spanned, SyntaxContext,
Expand Down Expand Up @@ -501,11 +501,11 @@ impl Visit for InitializerFinder {
/// But we care about modifications.
#[derive(Default)]
struct RequirementCalculator {
required_ids: IndexSet<(Id, Required), ahash::RandomState>,
required_ids: IndexSet<(Id, Required), ARandomState>,
/// While bundling, there can be two bindings with same name and syntax
/// context, in case of wrapped es modules. We exclude them from dependency
/// graph.
excluded: IndexSet<Id, ahash::RandomState>,
excluded: IndexSet<Id, ARandomState>,

in_weak: bool,
in_var_decl: bool,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
V: Clone,
{
#[cfg(feature = "concurrent")]
inner: dashmap::DashMap<K, V, ahash::RandomState>,
inner: dashmap::DashMap<K, V, swc_common::collections::ARandomState>,
#[cfg(not(feature = "concurrent"))]
inner: std::cell::RefCell<swc_common::collections::AHashMap<K, V>>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ concurrent = ["parking_lot"]
debug = []
default = []
diagnostic-serde = []
perf = ["parking_lot"]
perf = []
plugin-base = ["__plugin", "anyhow", "rkyv-impl", "diagnostic-serde"]
plugin-mode = ["__plugin_mode", "plugin-base"]
plugin-rt = ["__plugin_rt", "plugin-base"]
Expand Down
12 changes: 8 additions & 4 deletions crates/swc_common/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ mod fxhash {

use rustc_hash::FxHasher;

pub type AHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
pub type ARandomState = BuildHasherDefault<FxHasher>;

pub type AHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
pub type AHashMap<K, V> = HashMap<K, V, ARandomState>;

pub type AHashSet<V> = HashSet<V, ARandomState>;
}

#[cfg(not(feature = "perf"))]
mod ahash {
use std::collections::{HashMap, HashSet};

pub type AHashMap<K, V> = HashMap<K, V, ahash::RandomState>;
pub type ARandomState = ahash::RandomState;

pub type AHashMap<K, V> = HashMap<K, V, ARandomState>;

pub type AHashSet<V> = HashSet<V, ahash::RandomState>;
pub type AHashSet<V> = HashSet<V, ARandomState>;
}
1 change: 0 additions & 1 deletion crates/swc_ecma_lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ version = "0.85.19"
bench = false

[dependencies]
ahash = "0.8"
auto_impl = "1.1.0"
dashmap = "5.4.0"
parking_lot = "0.12.1"
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_lints/src/rules/dot_notation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dashmap::DashMap;
use regex::Regex;
use serde::{Deserialize, Serialize};
use swc_common::{errors::HANDLER, sync::Lazy, Span};
use swc_common::{collections::ARandomState, errors::HANDLER, sync::Lazy, Span};
use swc_ecma_ast::*;
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};

Expand Down Expand Up @@ -76,7 +76,7 @@ impl DotNotation {
}

if let Some(pattern) = &self.pattern {
static REGEX_CACHE: Lazy<DashMap<String, Regex, ahash::RandomState>> =
static REGEX_CACHE: Lazy<DashMap<String, Regex, ARandomState>> =
Lazy::new(Default::default);

if !REGEX_CACHE.contains_key(pattern) {
Expand Down