Skip to content

Commit

Permalink
refactor(rust): replace once_cell with std::sync::LazyLock (#7403)
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter authored Aug 1, 2024
1 parent af5a2a2 commit fa2cb3e
Show file tree
Hide file tree
Showing 74 changed files with 355 additions and 358 deletions.
16 changes: 0 additions & 16 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/rspack_base64/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ version = "0.1.0"

[dependencies]
base64-simd = { version = "0.8.0", features = ["alloc"] }
once_cell = { workspace = true }
regex = { workspace = true }
6 changes: 3 additions & 3 deletions crates/rspack_base64/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod base64 {
use std::borrow::Cow;
use std::sync::LazyLock;

use base64_simd::{Base64 as Raw, Error, STANDARD};
use once_cell::sync::Lazy;
use regex::Regex;

pub struct Base64(Raw);
Expand Down Expand Up @@ -37,8 +37,8 @@ pub mod base64 {
BASE64.0.decode_to_vec(data)
}

static INVALID_BASE64_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[^+/0-9A-Za-z-_]").expect("Invalid RegExp"));
static INVALID_BASE64_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"[^+/0-9A-Za-z-_]").expect("Invalid RegExp"));

// modified from https://github.com/feross/buffer/blob/795bbb5bda1b39f1370ebd784bea6107b087e3a7/index.js#L1942
// Buffer.from in nodejs will clean base64 first, which causes some inconsistent behavior with base64_simd
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_binding_values/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ futures = { workspace = true }
heck = { workspace = true }
napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow"] }
napi-derive = { workspace = true }
once_cell = { workspace = true }
rspack_collections = { path = "../rspack_collections" }
rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
Expand Down
7 changes: 4 additions & 3 deletions crates/rspack_binding_values/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::sync::LazyLock;

use heck::{ToLowerCamelCase, ToSnakeCase};
use napi_derive::napi;
use once_cell::sync::Lazy;
use rspack_core::RuntimeGlobals;
use rustc_hash::FxHashMap;

use crate::JsChunk;

static RUNTIME_GLOBAL_MAP: Lazy<(
static RUNTIME_GLOBAL_MAP: LazyLock<(
FxHashMap<RuntimeGlobals, String>,
FxHashMap<String, RuntimeGlobals>,
)> = Lazy::new(|| {
)> = LazyLock::new(|| {
let mut to_js_map = FxHashMap::default();
let mut from_js_map = FxHashMap::default();

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
collections::hash_map::{DefaultHasher, Entry},
fmt::Debug,
hash::{BuildHasherDefault, Hash, Hasher},
sync::{Arc, Mutex},
sync::{Arc, LazyLock, Mutex},
};

use dashmap::DashMap;
Expand Down Expand Up @@ -140,7 +140,7 @@ impl ConnectionOrModuleIdent {
}
}

pub static REGEX: once_cell::sync::Lazy<Regex> = once_cell::sync::Lazy::new(|| {
pub static REGEX: LazyLock<Regex> = LazyLock::new(|| {
let pattern = r"\.+\/|(\/index)?\.([a-zA-Z0-9]{1,4})($|\s|\?)|\s*\+\s*\d+\s*modules";
Regex::new(pattern).expect("should construct the regex")
});
Expand Down
14 changes: 7 additions & 7 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::LazyLock;
use std::{
borrow::Cow,
fs,
Expand All @@ -8,7 +9,6 @@ use std::{

use indoc::formatdoc;
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use rspack_collections::{Identifiable, Identifier};
use rspack_error::{impl_empty_diagnosable_trait, miette::IntoDiagnostic, Diagnostic, Result};
Expand Down Expand Up @@ -990,12 +990,12 @@ impl Hash for ContextModule {
}
}

static WEBPACK_CHUNK_NAME_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[index|request\]").expect("regexp init failed"));
static WEBPACK_CHUNK_NAME_INDEX_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[index\]").expect("regexp init failed"));
static WEBPACK_CHUNK_NAME_REQUEST_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[request\]").expect("regexp init failed"));
static WEBPACK_CHUNK_NAME_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[index|request\]").expect("regexp init failed"));
static WEBPACK_CHUNK_NAME_INDEX_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[index\]").expect("regexp init failed"));
static WEBPACK_CHUNK_NAME_REQUEST_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[request\]").expect("regexp init failed"));

impl ContextModule {
fn visit_dirs(
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::hash::Hasher;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::Arc;
use std::sync::LazyLock;

use itertools::Itertools;
use once_cell::sync::Lazy;
use rspack_collections::impl_item_ukey;
use rspack_collections::Ukey;
use rspack_collections::UkeyDashMap;
Expand All @@ -33,7 +33,8 @@ pub trait ExportsHash {
);
}

static EXPORTS_INFO_HASH: Lazy<UkeyDashMap<ExportsInfo, u64>> = Lazy::new(UkeyDashMap::default);
static EXPORTS_INFO_HASH: LazyLock<UkeyDashMap<ExportsInfo, u64>> =
LazyLock::new(UkeyDashMap::default);

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
pub struct ExportsInfo(Ukey);
Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::LazyLock;
use std::{borrow::Cow, sync::Arc};

use once_cell::sync::Lazy;
use regex::Regex;
use rspack_error::{error, Result};
use rspack_hook::define_hook;
Expand Down Expand Up @@ -73,15 +73,15 @@ impl ModuleFactory for NormalModuleFactory {
}
}

static MATCH_RESOURCE_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new("^([^!]+)!=!").expect("Failed to initialize `MATCH_RESOURCE_REGEX`"));
static MATCH_RESOURCE_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new("^([^!]+)!=!").expect("Failed to initialize `MATCH_RESOURCE_REGEX`"));

static MATCH_WEBPACK_EXT_REGEX: Lazy<Regex> = Lazy::new(|| {
static MATCH_WEBPACK_EXT_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"\.webpack\[([^\]]+)\]$"#).expect("Failed to initialize `MATCH_WEBPACK_EXT_REGEX`")
});

static ELEMENT_SPLIT_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"!+").expect("Failed to initialize `ELEMENT_SPLIT_REGEX`"));
static ELEMENT_SPLIT_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"!+").expect("Failed to initialize `ELEMENT_SPLIT_REGEX`"));

const HYPHEN: char = '-';
const EXCLAMATION: char = '!';
Expand Down
62 changes: 31 additions & 31 deletions crates/rspack_core/src/options/filename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::LazyLock;
use std::{borrow::Cow, convert::Infallible, ptr};

use once_cell::sync::Lazy;
use regex::{Captures, NoExpand, Regex};
use rspack_error::error;
use rspack_macros::MergeFrom;
Expand All @@ -15,37 +15,37 @@ use rspack_util::MergeFrom;

use crate::{parse_resource, AssetInfo, PathData, ResourceParsedData};

pub static FILE_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[file\]").expect("Should generate regex"));
pub static BASE_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[base\]").expect("Should generate regex"));
pub static NAME_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[name\]").expect("Should generate regex"));
pub static PATH_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[path\]").expect("Should generate regex"));
pub static EXT_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[ext\]").expect("Should generate regex"));
pub static QUERY_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[query\]").expect("Should generate regex"));
pub static FRAGMENT_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[fragment\]").expect("Should generate regex"));
pub static ID_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[id\]").expect("Should generate regex"));
pub static RUNTIME_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[runtime\]").expect("Should generate regex"));
pub static URL_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[url\]").expect("Should generate regex"));
pub static HASH_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[hash(:(\d*))?]").expect("Invalid regex"));
pub static CHUNK_HASH_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[chunkhash(:(\d*))?]").expect("Invalid regex"));
pub static CONTENT_HASH_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[contenthash(:(\d*))?]").expect("Invalid regex"));
pub static FULL_HASH_PLACEHOLDER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\[fullhash(:(\d*))?]").expect("Invalid regex"));
pub static FILE_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[file\]").expect("Should generate regex"));
pub static BASE_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[base\]").expect("Should generate regex"));
pub static NAME_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[name\]").expect("Should generate regex"));
pub static PATH_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[path\]").expect("Should generate regex"));
pub static EXT_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[ext\]").expect("Should generate regex"));
pub static QUERY_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[query\]").expect("Should generate regex"));
pub static FRAGMENT_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[fragment\]").expect("Should generate regex"));
pub static ID_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[id\]").expect("Should generate regex"));
pub static RUNTIME_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[runtime\]").expect("Should generate regex"));
pub static URL_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[url\]").expect("Should generate regex"));
pub static HASH_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[hash(:(\d*))?]").expect("Invalid regex"));
pub static CHUNK_HASH_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[chunkhash(:(\d*))?]").expect("Invalid regex"));
pub static CONTENT_HASH_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[contenthash(:(\d*))?]").expect("Invalid regex"));
pub static FULL_HASH_PLACEHOLDER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[fullhash(:(\d*))?]").expect("Invalid regex"));

static DATA_URI_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^data:([^;,]+)").expect("Invalid regex"));
static DATA_URI_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^data:([^;,]+)").expect("Invalid regex"));

#[derive(PartialEq, Debug, Hash, Eq, Clone, PartialOrd, Ord, MergeFrom)]
enum FilenameKind<F> {
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_core/src/options/output.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::LazyLock;
use std::{
borrow::Cow,
fmt::Debug,
Expand All @@ -8,7 +9,6 @@ use std::{
};

use derivative::Derivative;
use once_cell::sync::Lazy;
use regex::Regex;
use rspack_hash::RspackHash;
pub use rspack_hash::{HashDigest, HashFunction, HashSalt};
Expand Down Expand Up @@ -185,8 +185,8 @@ pub struct PathData<'a> {
pub id: Option<&'a str>,
}

static PREPARE_ID_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(^[.-]|[^a-zA-Z0-9_-])+").expect("invalid Regex"));
static PREPARE_ID_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(^[.-]|[^a-zA-Z0-9_-])+").expect("invalid Regex"));

impl<'a> PathData<'a> {
pub fn prepare_id(v: &str) -> Cow<str> {
Expand Down
Loading

2 comments on commit fa2cb3e

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rsbuild ❌ failure
examples ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-08-01 2f6696e) Current Change
10000_development-mode + exec 2.24 s ± 26 ms 2.26 s ± 15 ms +0.85 %
10000_development-mode_hmr + exec 699 ms ± 9.2 ms 716 ms ± 2.7 ms +2.40 %
10000_production-mode + exec 2.79 s ± 22 ms 2.8 s ± 20 ms +0.44 %
arco-pro_development-mode + exec 1.9 s ± 77 ms 1.9 s ± 77 ms -0.01 %
arco-pro_development-mode_hmr + exec 432 ms ± 3 ms 434 ms ± 2.2 ms +0.43 %
arco-pro_production-mode + exec 3.39 s ± 64 ms 3.42 s ± 105 ms +0.66 %
threejs_development-mode_10x + exec 1.77 s ± 19 ms 1.79 s ± 9.9 ms +1.45 %
threejs_development-mode_10x_hmr + exec 878 ms ± 10 ms 916 ms ± 6.1 ms +4.36 %
threejs_production-mode_10x + exec 5.49 s ± 37 ms 5.59 s ± 31 ms +1.74 %

Please sign in to comment.