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

Datagen: Cache supported_locales impls #4470

Merged
merged 10 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

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

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,7 @@ proc-macro2 = {version = "1", optional = true }

# Other external dependencies
displaydoc = { version = "0.2.3", default-features = false }
elsa = "1.9"
elsa = "1.10"
itertools = "0.10"
log = "0.4"
memchr = "2.5.0"
Expand Down
64 changes: 25 additions & 39 deletions provider/datagen/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{CollationHanDatabase, CoverageLevel};
use elsa::sync::FrozenMap;
use icu_provider::datagen::IterableDataProvider;
use icu_provider::prelude::*;
use std::collections::HashSet;
use std::fmt::Debug;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -80,7 +81,7 @@ impl DatagenProvider {
icuexport_dictionary_fallback: None,
#[cfg(feature = "legacy_api")]
collations: Default::default(),
supported_locales_cache_vec: Default::default(),
supported_locales_cache: Default::default(),
},
}
}
Expand Down Expand Up @@ -261,6 +262,24 @@ impl DatagenProvider {
) -> Result<impl IntoIterator<Item = icu_locid::LanguageIdentifier>, DataError> {
self.cldr()?.locales(levels)
}

pub(crate) fn supported_locales_vec<M>(&self) -> Result<&HashSet<DataLocale>, DataError>
where
M: KeyedDataMarker,
Self: IterableDataProviderInternal<M>,
{
#[allow(deprecated)] // SourceData
self.source
.supported_locales_cache
.insert_with(M::KEY, || {
Box::new(
self.supported_locales_impl()
.map(|v| v.into_iter().collect()),
)
})
.as_ref()
.map_err(|e| *e)
}
}

/// Specifies the trie type to use.
Expand Down Expand Up @@ -309,28 +328,8 @@ pub struct SourceData {
pub(crate) icuexport_dictionary_fallback: Option<Arc<SerdeCache>>,
#[cfg(feature = "legacy_api")]
pub(crate) collations: Vec<String>,
pub(crate) supported_locales_cache_vec:
FrozenMapThrowawayClone<DataKey, Box<Result<Vec<DataLocale>, DataError>>>,
}

pub(crate) struct FrozenMapThrowawayClone<K, V>(pub(crate) FrozenMap<K, V>);

impl<K, V> Default for FrozenMapThrowawayClone<K, V> {
fn default() -> Self {
Self(FrozenMap::new())
}
}

impl<K, V> Clone for FrozenMapThrowawayClone<K, V> {
fn clone(&self) -> Self {
Default::default()
}
}

impl<K, V> Debug for FrozenMapThrowawayClone<K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "FrozenMap")
}
pub(crate) supported_locales_cache:
Arc<FrozenMap<DataKey, Box<Result<HashSet<DataLocale>, DataError>>>>,
}

#[cfg(feature = "legacy_api")]
Expand Down Expand Up @@ -520,24 +519,11 @@ where
DatagenProvider: IterableDataProviderInternal<M>,
{
fn supported_locales(&self) -> Result<Vec<DataLocale>, DataError> {
#[allow(deprecated)] // SourceData
self.source
.supported_locales_cache_vec
.0
.insert_with(M::KEY, || Box::from(self.supported_locales_impl()))
.as_ref()
.cloned()
.map_err(|e| *e)
self.supported_locales_vec()
.map(|v| v.iter().cloned().collect())
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
}

fn supports_locale(&self, locale: &DataLocale) -> Result<bool, DataError> {
Copy link
Member

Choose a reason for hiding this comment

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

supports_locale is called n times per key, whereas supported_locales is only called once. It probably makes more sense to cache a HashSet and convert it to a Vec for the one call, than to linearly scan a Vec n times.

Copy link
Member Author

Choose a reason for hiding this comment

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

Performance on cargo make bakeddata datetime (run time only, not build time):

Commit Trial 1 Trial 2 Trial 3
11a1ed1 (Vec) 53.18 61.74 53.82
f2de286 (HashSet) 58.64 51.89 51.03

So it looks like HashSet wins.

#[allow(deprecated)] // SourceData
self.source
.supported_locales_cache_vec
.0
.insert_with(M::KEY, || Box::from(self.supported_locales_impl()))
.as_ref()
.map_err(|e| *e)
.map(|v| v.contains(locale))
self.supported_locales_vec().map(|v| v.contains(locale))
}
}
Loading