Skip to content

Commit

Permalink
switch to fxHashMap and fxHashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jul 21, 2024
1 parent e344732 commit a37af41
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 34 deletions.
16 changes: 16 additions & 0 deletions packages/yak-swc/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 packages/yak-swc/yak_swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ css_in_js_parser = { path = "../css_in_js_parser" }
relative_posix_path = { path = "../relative_posix_path" }
itertools = "0.13.0"
percent-encoding = "2.3.1"
fxhash = "0.2.1"

[dev-dependencies]
swc = "0.279.0"
Expand Down
8 changes: 4 additions & 4 deletions packages/yak-swc/yak_swc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use css_in_js_parser::{parse_css, to_css, CommentStateType};
use css_in_js_parser::{Declaration, ParserState};
use fxhash::FxHashMap;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::Path;
use std::vec;
use swc_core::common::comments::Comment;
Expand Down Expand Up @@ -84,7 +84,7 @@ where
/// e.g. const Rotation = keyframes`...` -> Rotation\
/// e.g. const Button = styled.button`...` -> Button\
/// Used to replace expressions with the actual class name or keyframes name
variable_name_mapping: HashMap<String, YakCss>,
variable_name_mapping: FxHashMap<String, YakCss>,
/// Naming convention to generate unique css identifiers
naming_convention: NamingConvention,
/// Expression replacement to replace a yak library call with the transformed one
Expand All @@ -110,7 +110,7 @@ where
variables: VariableVisitor::new(),
yak_library_imports: YakImportVisitor::new(),
naming_convention: NamingConvention::new(),
variable_name_mapping: HashMap::new(),
variable_name_mapping: FxHashMap::default(),
expression_replacement: None,
css_module_identifier: None,
dev_mode,
Expand Down Expand Up @@ -347,7 +347,7 @@ where
// Literal expressions which can't be replaced by constant values
// and must be kept for the final output (so they run at runtime)
let mut runtime_expressions: Vec<Expr> = vec![];
let mut runtime_css_variables: HashMap<String, Expr> = HashMap::new();
let mut runtime_css_variables: FxHashMap<String, Expr> = FxHashMap::default();
// When moving units into css variables it has to be removed from the next css code
// e.g. styled.button`left: ${({$x}) => $x}px;` -> `left: var(--left);`
let mut css_code_offset: usize = 0;
Expand Down
8 changes: 4 additions & 4 deletions packages/yak-swc/yak_swc/src/utils/assert_css_expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use fxhash::FxHashSet;
use swc_core::common::{Span, Spanned};
use swc_core::ecma::visit::VisitMutWith;
use swc_core::ecma::{ast::*, visit::VisitMut};
Expand All @@ -11,7 +11,7 @@ use swc_core::plugin::errors::HANDLER;
/// css`foo:bar;`
/// () => css`foo:bar;`
/// ({$active}) => { return $active && css`foo:bar;` }
pub fn assert_css_expr(expr: &mut Expr, message: String, valid_idents: HashSet<String>) {
pub fn assert_css_expr(expr: &mut Expr, message: String, valid_idents: FxHashSet<String>) {
let mut visitor = ExprVisitor::new(valid_idents);
let error_spans = match expr {
// if it's a function or a return statement, visit the children:
Expand Down Expand Up @@ -41,12 +41,12 @@ pub fn assert_css_expr(expr: &mut Expr, message: String, valid_idents: HashSet<S
/// Visitor implementation to walk the tree
struct ExprVisitor {
is_returning: bool,
valid_idents: HashSet<String>,
valid_idents: FxHashSet<String>,
pub error_spans: Vec<Span>,
}

impl ExprVisitor {
pub fn new(valid_idents: HashSet<String>) -> Self {
pub fn new(valid_idents: FxHashSet<String>) -> Self {
Self {
is_returning: false,
valid_idents,
Expand Down
8 changes: 4 additions & 4 deletions packages/yak-swc/yak_swc/src/utils/ast_helper.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::HashMap;

use fxhash::FxHashMap;
use itertools::Itertools;

use swc_core::{common::DUMMY_SP, ecma::ast::*, plugin::errors::HANDLER};

/// Convert a HashMap to an Object expression
pub fn expr_hash_map_to_object(values: HashMap<String, Expr>) -> Expr {
pub fn expr_hash_map_to_object(values: FxHashMap<String, Expr>) -> Expr {
let properties = values
.into_iter()
.sorted_by_key(|(key, _)| key.clone())
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn member_expr_to_strings(member_expr: &MemberExpr) -> Option<(Ident, Vec<St
Expr::Lit(Lit::Num(num)) => {
props.push(num.value.to_string());
}
_ => return None,
_ => return None,
},
MemberProp::PrivateName(_) => return None,
}
Expand Down
10 changes: 5 additions & 5 deletions packages/yak-swc/yak_swc/src/variable_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::collections::HashMap;
use fxhash::FxHashMap;
use swc_core::ecma::visit::VisitMutWith;
use swc_core::ecma::{ast::*, visit::VisitMut};

#[derive(Debug)]
/// Visitor implementation to gather all variable declarations
/// and their values from the AST
pub struct VariableVisitor {
variables: HashMap<String, Box<Expr>>,
imports: HashMap<String, String>,
variables: FxHashMap<String, Box<Expr>>,
imports: FxHashMap<String, String>,
}

impl VariableVisitor {
pub fn new() -> Self {
Self {
variables: HashMap::new(),
imports: HashMap::new(),
variables: FxHashMap::default(),
imports: FxHashMap::default(),
}
}

Expand Down
14 changes: 7 additions & 7 deletions packages/yak-swc/yak_swc/src/yak_imports.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use fxhash::{FxHashMap, FxHashSet};
use swc_core::{
common::DUMMY_SP,
ecma::{ast::*, visit::VisitMut},
Expand All @@ -9,21 +9,21 @@ use swc_core::{
/// Side effect: converts the import source from "next-yak" to "next-yak/internal"
pub struct YakImportVisitor {
/// Imports from "next-yak"
yak_library_imports: HashMap<String, String>,
yak_library_imports: FxHashMap<String, String>,
/// Utilities used from "next-yak/internal"
yak_utilities: HashMap<String, Ident>,
yak_utilities: FxHashMap<String, Ident>,
/// Identifiers used in the css function
pub yak_css_idents: HashSet<String>,
pub yak_css_idents: FxHashSet<String>,
}

const UTILITIES: &[&str] = &["unitPostFix", "mergeCssProp"];

impl YakImportVisitor {
pub fn new() -> Self {
Self {
yak_library_imports: HashMap::new(),
yak_utilities: HashMap::new(),
yak_css_idents: HashSet::new(),
yak_library_imports: FxHashMap::default(),
yak_utilities: FxHashMap::default(),
yak_css_idents: FxHashSet::default(),
}
}

Expand Down
20 changes: 10 additions & 10 deletions packages/yak-swc/yak_swc/src/yak_transforms.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use fxhash::FxHashMap;

use crate::utils::ast_helper::{create_member_prop_from_string, expr_hash_map_to_object};
use css_in_js_parser::{CssScope, Declaration, ParserState, ScopeType};
Expand Down Expand Up @@ -52,7 +52,7 @@ pub trait YakTransform {
css_module_identifier: Ident,
runtime_expressions: Vec<Expr>,
declarations: &[Declaration],
runtime_css_variables: HashMap<String, Expr>,
runtime_css_variables: FxHashMap<String, Expr>,
) -> YakTransformResult;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ impl YakTransform for TransformNestedCss {
css_module_identifier: Ident,
runtime_expressions: Vec<Expr>,
declarations: &[Declaration],
runtime_css_variables: HashMap<String, Expr>,
runtime_css_variables: FxHashMap<String, Expr>,
) -> YakTransformResult {
let mut arguments: Vec<ExprOrSpread> = vec![];
if !declarations.is_empty() {
Expand All @@ -117,7 +117,7 @@ impl YakTransform for TransformNestedCss {
arguments.extend(runtime_expressions.into_iter().map(ExprOrSpread::from));
if !runtime_css_variables.is_empty() {
arguments.push(
expr_hash_map_to_object(HashMap::from([(
expr_hash_map_to_object(FxHashMap::from_iter([(
"style".to_string(),
expr_hash_map_to_object(runtime_css_variables),
)]))
Expand Down Expand Up @@ -176,7 +176,7 @@ impl YakTransform for TransformCssMixin {
css_module_identifier: Ident,
runtime_expressions: Vec<Expr>,
declarations: &[Declaration],
runtime_css_variables: HashMap<String, Expr>,
runtime_css_variables: FxHashMap<String, Expr>,
) -> YakTransformResult {
let mut arguments: Vec<ExprOrSpread> = vec![];
if !declarations.is_empty() {
Expand All @@ -192,7 +192,7 @@ impl YakTransform for TransformCssMixin {
arguments.extend(runtime_expressions.into_iter().map(ExprOrSpread::from));
if !runtime_css_variables.is_empty() {
arguments.push(
expr_hash_map_to_object(HashMap::from([(
expr_hash_map_to_object(FxHashMap::from_iter([(
"style".to_string(),
expr_hash_map_to_object(runtime_css_variables),
)]))
Expand Down Expand Up @@ -251,7 +251,7 @@ impl YakTransform for TransformStyled {
css_module_identifier: Ident,
runtime_expressions: Vec<Expr>,
declarations: &[Declaration],
runtime_css_variables: HashMap<String, Expr>,
runtime_css_variables: FxHashMap<String, Expr>,
) -> YakTransformResult {
let mut arguments: Vec<ExprOrSpread> = vec![];
if !declarations.is_empty() {
Expand All @@ -267,7 +267,7 @@ impl YakTransform for TransformStyled {
arguments.extend(runtime_expressions.into_iter().map(ExprOrSpread::from));
if !runtime_css_variables.is_empty() {
arguments.push(
expr_hash_map_to_object(HashMap::from([(
expr_hash_map_to_object(FxHashMap::from_iter([(
"style".to_string(),
expr_hash_map_to_object(runtime_css_variables),
)]))
Expand Down Expand Up @@ -328,7 +328,7 @@ impl YakTransform for TransformKeyframes {
css_module_identifier: Ident,
_runtime_expressions: Vec<Expr>,
declarations: &[Declaration],
runtime_css_variables: HashMap<String, Expr>,
runtime_css_variables: FxHashMap<String, Expr>,
) -> YakTransformResult {
let mut arguments: Vec<ExprOrSpread> = vec![];
if !declarations.is_empty() {
Expand All @@ -343,7 +343,7 @@ impl YakTransform for TransformKeyframes {
}
if !runtime_css_variables.is_empty() {
arguments.push(
expr_hash_map_to_object(HashMap::from([(
expr_hash_map_to_object(FxHashMap::from_iter([(
"style".to_string(),
expr_hash_map_to_object(runtime_css_variables),
)]))
Expand Down

0 comments on commit a37af41

Please sign in to comment.