Skip to content

Commit

Permalink
perf(atoms): Replace string-cache with hstr (#8126)
Browse files Browse the repository at this point in the history
**Description:**

`hstr` is an alternative for `string-cache` which does not support static strings and does not use a global mutex.
 
**Related issue:**

 - Closes #4946.
 - Closes #7974.
  • Loading branch information
kdy1 committed Nov 7, 2023
1 parent d1c6d6e commit aa22746
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 135 deletions.
38 changes: 20 additions & 18 deletions Cargo.lock

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

8 changes: 1 addition & 7 deletions crates/swc_atoms/Cargo.toml
@@ -1,6 +1,5 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
build = "build.rs"
description = "Atoms for the swc project."
documentation = "https://rustdoc.swc.rs/swc_atoms/"
edition = "2021"
Expand All @@ -19,16 +18,11 @@ rkyv-impl = ["__rkyv", "rkyv", "bytecheck"]
[dependencies]
# bytecheck version should be in sync with rkyv version. Do not bump individually.
bytecheck = { version = "0.6.10", optional = true }
hstr = "0.2.3"
once_cell = "1"
rkyv = { package = "rkyv", version = "=0.7.42", optional = true, features = [
"strict",
"validation",
] }
rustc-hash = "1.1.0"
serde = "1"
string_cache = "0.8.7"
triomphe = "0.1.8"


[build-dependencies]
string_cache_codegen = "0.5.2"
29 changes: 0 additions & 29 deletions crates/swc_atoms/build.rs

This file was deleted.

25 changes: 15 additions & 10 deletions crates/swc_atoms/src/lib.rs
Expand Up @@ -26,7 +26,7 @@ pub use self::{atom as js_word, Atom as JsWord};
#[derive(Clone, Default)]
#[cfg_attr(feature = "rkyv-impl", derive(rkyv::bytecheck::CheckBytes))]
#[cfg_attr(feature = "rkyv-impl", repr(C))]
pub struct Atom(string_cache::Atom<InternalWordStaticSet>);
pub struct Atom(hstr::Atom);

/// Safety: We do not perform slicing of single [Atom] from multiple threads.
/// In other words, typically [Atom] is created in a single thread (and in the
Expand All @@ -39,11 +39,11 @@ unsafe impl Sync for Atom {}

impl Atom {
/// Creates a new [Atom] from a string.
pub fn new<S>(s: S) -> Self
pub fn new<'i, S>(s: S) -> Self
where
S: AsRef<str>,
S: Into<Cow<'i, str>>,
{
Atom(s.as_ref().into())
Atom(hstr::Atom::from(s.into()))
}

#[inline]
Expand All @@ -52,9 +52,6 @@ impl Atom {
}
}

/// API wrappers for [tendril].
impl Atom {}

impl Deref for Atom {
type Target = str;

Expand Down Expand Up @@ -236,9 +233,17 @@ where
#[doc(hidden)]
pub type CahcedAtom = Lazy<Atom>;

include!(concat!(env!("OUT_DIR"), "/internal_word.rs"));

/// This should be used as a key for hash maps and hash sets.
///
/// This will be replaced with [Atom] in the future.
pub type StaticString = String;
pub type StaticString = Atom;

#[derive(Default)]
pub struct AtomStore(hstr::AtomStore);

impl AtomStore {
#[inline]
pub fn atom<'a>(&mut self, s: impl Into<Cow<'a, str>>) -> Atom {
Atom(self.0.atom(s))
}
}
6 changes: 3 additions & 3 deletions crates/swc_css_minifier/src/compressor/color.rs
Expand Up @@ -351,12 +351,12 @@ impl Compressor {
value,
span,
..
})) => match &*value.to_ascii_lowercase() {
"transparent" => {
})) => match value.to_ascii_lowercase() {
ref s if *s == "transparent" => {
*color = make_color!(*span, 0.0_f64, 0.0_f64, 0.0_f64, 0.0_f64);
}
name => {
if let Some(value) = NAMED_COLORS.get(name) {
if let Some(value) = NAMED_COLORS.get(&name) {
*color = make_color!(
*span,
value.rgb[0] as f64,
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_css_prefixer/src/prefixer.rs
Expand Up @@ -5,7 +5,7 @@ use std::mem::take;

use once_cell::sync::Lazy;
use preset_env_base::{query::targets_to_versions, version::Version, BrowserData, Versions};
use swc_atoms::{JsWord, StaticString};
use swc_atoms::JsWord;
use swc_common::{collections::AHashMap, EqIgnoreSpan, DUMMY_SP};
use swc_css_ast::*;
use swc_css_utils::{
Expand All @@ -16,9 +16,9 @@ use swc_css_visit::{VisitMut, VisitMutWith};

use crate::options::Options;

static PREFIXES_AND_BROWSERS: Lazy<AHashMap<StaticString, [BrowserData<Option<Version>>; 2]>> =
static PREFIXES_AND_BROWSERS: Lazy<AHashMap<String, [BrowserData<Option<Version>>; 2]>> =
Lazy::new(|| {
let map: AHashMap<StaticString, [BrowserData<Option<Version>>; 2]> =
let map: AHashMap<String, [BrowserData<Option<Version>>; 2]> =
serde_json::from_str(include_str!("../data/prefixes_and_browsers.json"))
.expect("failed to parse json");

Expand Down
12 changes: 7 additions & 5 deletions crates/swc_ecma_parser/src/lexer/jsx.rs
@@ -1,5 +1,4 @@
use either::Either;
use swc_atoms::Atom;

use super::*;
use crate::token::Token;
Expand Down Expand Up @@ -48,7 +47,7 @@ impl<'a> Lexer<'a> {
});

return Ok(Some(Token::JSXText {
raw: Atom::new(out),
raw: self.atoms.borrow_mut().atom(out),
}));
}
'>' => {
Expand Down Expand Up @@ -323,9 +322,10 @@ impl<'a> Lexer<'a> {

raw.push(quote);

let mut b = self.atoms.borrow_mut();
Ok(Token::Str {
value: out.into(),
raw: Atom::new(raw),
value: b.atom(out),
raw: b.atom(raw),
})
}

Expand All @@ -350,7 +350,9 @@ impl<'a> Lexer<'a> {
}
});

Ok(Token::JSXName { name: slice.into() })
Ok(Token::JSXName {
name: self.atoms.borrow_mut().atom(slice),
})
}
}

Expand Down

0 comments on commit aa22746

Please sign in to comment.