Skip to content

Commit

Permalink
Use once_cell instead of lazy_static (#3781)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth authored Aug 3, 2023
1 parent db870ae commit d6a9d85
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 69 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions components/collections/codepointtrie_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ independent = true
[features]
# Use the wasm builder
default = ["wasm"]
wasm = ["wasmer", "wasmer-wasi"]
wasm = ["wasmer", "wasmer-wasi", "once_cell"]
# Use the ICU4C builder
# needs the ICU4C_LIB_PATH variable set and pointing to an ICU4C lib folder
# containing dylibs. If you want to use staticlibs, set ICU4C_LINK_STATICALLY.
Expand All @@ -42,8 +42,8 @@ icu4c = ["zerovec"]
[dependencies]
icu_collections = { version = "1.2.0", path = "..", features = ["serde"] }
zerovec = { version = "0.9.4", path = "../../../utils/zerovec", optional = true }
lazy_static = "1.4.0"
toml = "0.5"
once_cell = { version = "1.18.0", optional = true }

[dependencies.wasmer]
version = "2.2.1"
Expand Down
17 changes: 10 additions & 7 deletions components/collections/codepointtrie_builder/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use crate::CodePointTrieBuilder;
use crate::CodePointTrieBuilderData;
use icu_collections::codepointtrie::TrieType;
use icu_collections::codepointtrie::TrieValue;
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use wasmer::{Instance, Module, Store};
use wasmer_wasi::{Pipe, WasiState};

const WASM_BYTES: &[u8] = include_bytes!("../list_to_ucptrie.wasm");

lazy_static! {
static ref STORE: Store = Store::default();
static ref MODULE: Module = Module::new(&STORE, WASM_BYTES).expect("valid WASM");
}
static STORE: OnceCell<Store> = OnceCell::new();
static MODULE: OnceCell<Module> = OnceCell::new();

pub(crate) fn run_wasm<T>(builder: &CodePointTrieBuilder<T>) -> String
where
Expand All @@ -39,9 +37,14 @@ where
.finalize()
.expect("valid arguments + in-memory filesystem");

let module = MODULE.get_or_init(|| {
let store = STORE.get_or_init(Store::default);
Module::new(store, WASM_BYTES).expect("valid WASM")
});

// Create the WebAssembly instance with the module and the WasiState
let import_object = wasi_env.import_object(&MODULE).expect("walid wasm file");
let instance = Instance::new(&MODULE, &import_object).expect("valid wasm file");
let import_object = wasi_env.import_object(module).expect("walid wasm file");
let instance = Instance::new(module, &import_object).expect("valid wasm file");

// To write to the stdin, we need a mutable reference to the pipe
//
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ syn = {version = "2", features = ["parsing"], optional = true }
displaydoc = { version = "0.2.3", default-features = false }
elsa = "1.7"
itertools = "0.10"
lazy_static = "1"
log = "0.4"
memchr = "2.5.0"
ndarray = { version = "0.15.5", default-features = false }
Expand All @@ -89,6 +88,7 @@ ureq = { version = "2", optional = true }
clap = { version = "4", optional = true, features = ["derive"] }
eyre = { version = "0.6", optional = true }
simple_logger = { version = "4.1.0", default-features = false, optional = true }
once_cell = "1.18.0"

[dev-dependencies]
crlify = { version = "1.0.1", path = "../../utils/crlify" }
Expand Down
21 changes: 13 additions & 8 deletions provider/datagen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,25 @@ impl DatagenProvider {

#[cfg(test)]
pub fn for_test() -> Self {
use once_cell::sync::OnceCell;

static TEST_PROVIDER: OnceCell<DatagenProvider> = OnceCell::new();
// Singleton so that all instantiations share the same cache.
lazy_static::lazy_static! {
static ref TEST_PROVIDER: DatagenProvider = {
let data_root = std::path::Path::new(core::env!("CARGO_MANIFEST_DIR")).join("tests/data");
TEST_PROVIDER
.get_or_init(|| {
let data_root =
std::path::Path::new(core::env!("CARGO_MANIFEST_DIR")).join("tests/data");
DatagenProvider {
// This is equivalent to `latest_tested` for the files defined in
// `tools/testdata-scripts/globs.rs.data`.
source: SourceData::offline()
.with_cldr(data_root.join("cldr"), Default::default()).unwrap()
.with_icuexport(data_root.join("icuexport")).unwrap(),
.with_cldr(data_root.join("cldr"), Default::default())
.unwrap()
.with_icuexport(data_root.join("icuexport"))
.unwrap(),
}
};
}
TEST_PROVIDER.clone()
})
.clone()
}

pub(crate) fn filter_data_locales(
Expand Down
12 changes: 7 additions & 5 deletions provider/datagen/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ macro_rules! registry {
/// );
/// ```
pub fn key<S: AsRef<str>>(string: S) -> Option<DataKey> {
lazy_static::lazy_static! {
static ref LOOKUP: std::collections::HashMap<&'static str, Result<DataKey, &'static str>> = [
use once_cell::sync::OnceCell;
static LOOKUP: OnceCell<std::collections::HashMap<&'static str, Result<DataKey, &'static str>>> = OnceCell::new();
let lookup = LOOKUP.get_or_init(|| {
[
("core/helloworld@1", Ok(icu_provider::hello_world::HelloWorldV1Marker::KEY)),
$(
$(
Expand All @@ -55,10 +57,10 @@ macro_rules! registry {
)+
]
.into_iter()
.collect();
}
.collect()
});
let path = string.as_ref();
match LOOKUP.get(path).copied() {
match lookup.get(path).copied() {
None => {
log::warn!("Unknown key {path:?}");
None
Expand Down
42 changes: 23 additions & 19 deletions provider/datagen/src/transform/cldr/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use icu_locid::{
};
use icu_provider::datagen::IterableDataProvider;
use icu_provider::prelude::*;
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use std::collections::HashMap;
use std::collections::HashSet;
use std::str::FromStr;
Expand All @@ -21,21 +21,25 @@ mod skeletons;
mod symbols;
pub mod week_data;

lazy_static! {
// BCP-47 value -> CLDR identifier
static ref SUPPORTED_CALS: HashMap<icu_locid::extensions::unicode::Value, &'static str> = [
(value!("gregory"), "gregorian"),
(value!("buddhist"), "buddhist"),
(value!("japanese"), "japanese"),
(value!("japanext"), "japanese"),
(value!("coptic"), "coptic"),
(value!("indian"), "indian"),
(value!("persian"), "persian"),
(value!("ethiopic"), "ethiopic"),
(value!("roc"), "roc"),
]
.into_iter()
.collect();
pub static SUPPORTED_CALS: OnceCell<HashMap<icu_locid::extensions::unicode::Value, &'static str>> =
OnceCell::new();

fn supported_cals() -> &'static HashMap<icu_locid::extensions::unicode::Value, &'static str> {
SUPPORTED_CALS.get_or_init(|| {
[
(value!("gregory"), "gregorian"),
(value!("buddhist"), "buddhist"),
(value!("japanese"), "japanese"),
(value!("japanext"), "japanese"),
(value!("coptic"), "coptic"),
(value!("indian"), "indian"),
(value!("persian"), "persian"),
(value!("ethiopic"), "ethiopic"),
(value!("roc"), "roc"),
]
.into_iter()
.collect()
})
}

macro_rules! impl_data_provider {
Expand All @@ -58,7 +62,7 @@ macro_rules! impl_data_provider {
value!($calendared)
};

let cldr_cal = SUPPORTED_CALS
let cldr_cal = supported_cals()
.get(&calendar)
.ok_or_else(|| DataErrorKind::MissingLocale.into_error())?;

Expand Down Expand Up @@ -230,7 +234,7 @@ macro_rules! impl_data_provider {
fn supported_locales(&self) -> Result<Vec<DataLocale>, DataError> {
let mut r = Vec::new();
if $calendared == "locale" {
for (cal_value, cldr_cal) in &*SUPPORTED_CALS {
for (cal_value, cldr_cal) in supported_cals() {
r.extend(
self.source
.cldr()?
Expand All @@ -253,7 +257,7 @@ macro_rules! impl_data_provider {
} else {
value!($calendared)
};
let cldr_cal = SUPPORTED_CALS
let cldr_cal = supported_cals()
.get(&calendar)
.ok_or_else(|| DataErrorKind::MissingLocale.into_error())?;
r.extend(
Expand Down
36 changes: 20 additions & 16 deletions provider/datagen/src/transform/cldr/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use icu_list::provider::*;
use icu_locid::subtags::language;
use icu_provider::datagen::IterableDataProvider;
use icu_provider::prelude::*;
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use std::borrow::Cow;

fn load<M: KeyedDataMarker<Yokeable = ListFormatterPatternsV1<'static>>>(
Expand Down Expand Up @@ -51,29 +51,33 @@ fn load<M: KeyedDataMarker<Yokeable = ListFormatterPatternsV1<'static>>>(

if langid.language == language!("es") {
if M::KEY == AndListV1Marker::KEY || M::KEY == UnitListV1Marker::KEY {
lazy_static! {
// Starts with i or (hi but not hia/hie)
static ref I_SOUND: SerdeDFA<'static> = SerdeDFA::new(
Cow::Borrowed("i|hi([^ae]|$)")
).expect("Valid regex");
}
static I_SOUND: OnceCell<SerdeDFA<'static>> = OnceCell::new();

// Starts with i or (hi but not hia/hie)
let i_sound = I_SOUND.get_or_init(|| {
SerdeDFA::new(Cow::Borrowed("i|hi([^ae]|$)")).expect("Valid regex")
});

// Replace " y " with " e " before /i/ sounds.
// https://unicode.org/reports/tr35/tr35-general.html#:~:text=important.%20For%20example%3A-,Spanish,AND,-Use%20%E2%80%98e%E2%80%99%20instead
patterns
.make_conditional("{0} y {1}", &I_SOUND, "{0} e {1}")
.make_conditional("{0} y {1}", i_sound, "{0} e {1}")
.expect("valid pattern");
} else if M::KEY == OrListV1Marker::KEY {
lazy_static! {
// Starts with o, ho, 8 (including 80, 800, ...), or 11 either alone or followed
// by thousand groups and/or decimals (excluding e.g. 110, 1100, ...)
static ref O_SOUND: SerdeDFA<'static> = SerdeDFA::new(
Cow::Borrowed(r"o|ho|8|(11([\.  ]?[0-9]{3})*(,[0-9]*)?([^\.,[0-9]]|$))")
).expect("Valid regex");
}
static O_SOUND: OnceCell<SerdeDFA<'static>> = OnceCell::new();
// Starts with o, ho, 8 (including 80, 800, ...), or 11 either alone or followed
// by thousand groups and/or decimals (excluding e.g. 110, 1100, ...)
let o_sound = O_SOUND.get_or_init(|| {
SerdeDFA::new(Cow::Borrowed(
r"o|ho|8|(11([\.  ]?[0-9]{3})*(,[0-9]*)?([^\.,[0-9]]|$))",
))
.expect("Valid regex")
});

// Replace " o " with " u " before /o/ sound.
// https://unicode.org/reports/tr35/tr35-general.html#:~:text=agua%20e%20hielo-,OR,-Use%20%E2%80%98u%E2%80%99%20instead
patterns
.make_conditional("{0} o {1}", &O_SOUND, "{0} u {1}")
.make_conditional("{0} o {1}", o_sound, "{0} u {1}")
.expect("valid pattern");
}
}
Expand Down
13 changes: 7 additions & 6 deletions provider/datagen/src/transform/cldr/relativetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ use crate::transform::cldr::cldr_serde;
use icu_provider::datagen::IterableDataProvider;
use icu_provider::prelude::*;
use icu_relativetime::provider::*;
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use std::collections::{BTreeMap, HashMap};

lazy_static! {
static ref DATAKEY_FILTERS: HashMap<DataKey, &'static str> = {
pub static DATAKEY_FILTERS: OnceCell<HashMap<DataKey, &'static str>> = OnceCell::new();

fn datakey_filters() -> &'static HashMap<DataKey, &'static str> {
DATAKEY_FILTERS.get_or_init(|| {
[
(LongSecondRelativeTimeFormatDataV1Marker::KEY, "second"),
(
Expand Down Expand Up @@ -62,9 +64,8 @@ lazy_static! {
]
.into_iter()
.collect()
};
})
}

macro_rules! make_data_provider {
($($marker: ident),+ $(,)?) => {
$(
Expand All @@ -86,7 +87,7 @@ macro_rules! make_data_provider {
.dates
.fields;

let field = DATAKEY_FILTERS
let field = datakey_filters()
.get(&$marker::KEY)
.ok_or(DataErrorKind::MissingDataKey.into_error())?;

Expand Down
2 changes: 1 addition & 1 deletion tools/depcheck/src/allowlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ pub const EXTRA_DATAGEN_DEPS: &[&str] = &[
"icu_codepointtrie_builder",
"itertools",
"itoa",
"lazy_static",
"matrixmultiply",
"ndarray",
"num-complex",
"num-integer",
"num-traits",
"once_cell",
"rawpointer",
"regex-syntax",
"ryu",
Expand Down

0 comments on commit d6a9d85

Please sign in to comment.