Skip to content

Commit

Permalink
Follow-up #4985 (#4995)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Jun 3, 2024
1 parent fffa624 commit 24761aa
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 67 deletions.
55 changes: 43 additions & 12 deletions provider/datagen/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use elsa::sync::FrozenMap;
use icu_provider::datagen::IterableDataProvider;
use icu_provider::prelude::*;
use source::{AbstractFs, SerdeCache};
use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt::Debug;
use std::path::PathBuf;
Expand Down Expand Up @@ -41,8 +42,17 @@ pub struct DatagenProvider {
trie_type: TrieType,
collation_han_database: CollationHanDatabase,
#[allow(clippy::type_complexity)] // not as complex as it appears
supported_locales_cache:
Arc<FrozenMap<DataKey, Box<Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError>>>>,
supported_requests_cache: Arc<
FrozenMap<
DataKey,
Box<
Result<
HashSet<(Cow<'static, DataLocale>, Cow<'static, DataKeyAttributes>)>,
DataError,
>,
>,
>,
>,
}

macro_rules! cb {
Expand Down Expand Up @@ -109,7 +119,7 @@ impl DatagenProvider {
segmenter_lstm_paths: None,
trie_type: Default::default(),
collation_han_database: Default::default(),
supported_locales_cache: Default::default(),
supported_requests_cache: Default::default(),
}
}

Expand Down Expand Up @@ -348,30 +358,51 @@ impl std::fmt::Display for TrieType {
}

trait IterableDataProviderCached<M: KeyedDataMarker>: DataProvider<M> {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError>;
}

impl DatagenProvider {
#[allow(clippy::type_complexity)] // not as complex as it appears
fn populate_supported_requests_cache<M: KeyedDataMarker>(
&self,
) -> Result<&HashSet<(Cow<'static, DataLocale>, Cow<'static, DataKeyAttributes>)>, DataError>
where
DatagenProvider: IterableDataProviderCached<M>,
{
self.supported_requests_cache
.insert_with(M::KEY, || {
Box::new(self.supported_requests_cached().map(|m| {
m.into_iter()
.map(|(k, v)| (Cow::Owned(k), Cow::Owned(v)))
.collect()
}))
})
.as_ref()
.map_err(|&e| e)
}
}

impl<M: KeyedDataMarker> IterableDataProvider<M> for DatagenProvider
where
DatagenProvider: IterableDataProviderCached<M>,
{
fn supported_requests(&self) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
self.supported_locales_cache
.insert_with(M::KEY, || Box::new(self.supported_locales_cached()))
.clone()
Ok(self
.populate_supported_requests_cache()?
.iter()
.map(|(k, v)| (k.clone().into_owned(), v.clone().into_owned()))
.collect())
}

fn supports_request(
&self,
locale: &DataLocale,
key_attributes: &DataKeyAttributes,
) -> Result<bool, DataError> {
self.supported_locales_cache
.insert_with(M::KEY, || Box::new(self.supported_locales_cached()))
.as_ref()
.map_err(|e| *e)
.map(|v| v.contains(&(locale.clone(), key_attributes.clone())))
Ok(self
.populate_supported_requests_cache()?
.contains(&(Cow::Borrowed(locale), Cow::Borrowed(key_attributes))))
}
}
2 changes: 1 addition & 1 deletion provider/datagen/src/transform/cldr/characters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ macro_rules! exemplar_chars_impls {
}

impl IterableDataProviderCached<$data_marker_name> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/transform/cldr/currency/essentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl DataProvider<CurrencyEssentialsV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<CurrencyEssentialsV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/transform/cldr/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ macro_rules! impl_data_provider {
}

impl IterableDataProviderCached<$marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
let mut r = HashSet::new();
Expand Down
35 changes: 18 additions & 17 deletions provider/datagen/src/transform/cldr/datetime/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,27 @@ impl DatagenProvider {
})
}

fn supported_locales_neo(
fn supported_requests_neo(
&self,
calendar: Value,
keylengths: &'static [TinyAsciiStr<8>],
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
let mut r = HashSet::new();

let cldr_cal = supported_cals()
.get(&calendar)
.ok_or_else(|| DataErrorKind::MissingLocale.into_error())?;
r.extend(self.cldr()?.dates(cldr_cal).list_langs()?.flat_map(|lid| {
keylengths.iter().map(move |&length| {
(
DataLocale::from(lid.clone()),
DataKeyAttributes::from_tinystr(length),
)
Ok(self
.cldr()?
.dates(cldr_cal)
.list_langs()?
.flat_map(|lid| {
keylengths.iter().map(move |&length| {
(
DataLocale::from(lid.clone()),
DataKeyAttributes::from_tinystr(length),
)
})
})
}));

Ok(r)
.collect())
}
}

Expand Down Expand Up @@ -671,7 +672,7 @@ impl DataProvider<TimePatternV1Marker> for DatagenProvider {
// and we can use a union of the H12/H24 key lengths arrays, instead checking for preferred hc
// in timepattern_convert
impl IterableDataProviderCached<TimePatternV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
let calendar = value!("gregory");
Expand Down Expand Up @@ -736,10 +737,10 @@ macro_rules! impl_symbols_datagen {
}

impl IterableDataProviderCached<$marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
self.supported_locales_neo(value!($calendar), $lengths)
self.supported_requests_neo(value!($calendar), $lengths)
}
}
};
Expand All @@ -754,10 +755,10 @@ macro_rules! impl_pattern_datagen {
}

impl IterableDataProviderCached<$marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
self.supported_locales_neo(value!($calendar), $lengths)
self.supported_requests_neo(value!($calendar), $lengths)
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions provider/datagen/src/transform/cldr/datetime/neo_skeleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl DataProvider<TimeNeoSkeletonPatternsV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<TimeNeoSkeletonPatternsV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
self.neo_time_skeleton_supported_locales()
Expand All @@ -230,7 +230,7 @@ macro_rules! impl_neo_skeleton_datagen {
}

impl IterableDataProviderCached<$marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
self.neo_date_skeleton_supported_locales(&value!($calendar))
Expand Down
6 changes: 3 additions & 3 deletions provider/datagen/src/transform/cldr/datetime/week_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use icu_provider::prelude::*;
use std::collections::HashSet;

impl IterableDataProviderCached<WeekDataV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
let week_data: &cldr_serde::week_data::Resource = self
Expand Down Expand Up @@ -200,10 +200,10 @@ impl DataProvider<WeekDataV2Marker> for DatagenProvider {
}

impl IterableDataProviderCached<WeekDataV2Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
IterableDataProviderCached::<WeekDataV1Marker>::supported_locales_cached(self)
IterableDataProviderCached::<WeekDataV1Marker>::supported_requests_cached(self)
}
}

Expand Down
8 changes: 4 additions & 4 deletions provider/datagen/src/transform/cldr/decimal/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ impl DataProvider<LongCompactDecimalFormatDataV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<ShortCompactDecimalFormatDataV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
self.supported_locales_for_numbers()
self.supported_requests_for_numbers()
}
}

impl IterableDataProviderCached<LongCompactDecimalFormatDataV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
self.supported_locales_for_numbers()
self.supported_requests_for_numbers()
}
}

Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/transform/cldr/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl DatagenProvider {
.collect())
}

fn supported_locales_for_numbers(
fn supported_requests_for_numbers(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
4 changes: 2 additions & 2 deletions provider/datagen/src/transform/cldr/decimal/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl DataProvider<DecimalSymbolsV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<DecimalSymbolsV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
self.supported_locales_for_numbers()
self.supported_requests_for_numbers()
}
}

Expand Down
4 changes: 2 additions & 2 deletions provider/datagen/src/transform/cldr/displaynames/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl DataProvider<LocaleDisplayNamesV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<LanguageDisplayNamesV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand All @@ -73,7 +73,7 @@ impl IterableDataProviderCached<LanguageDisplayNamesV1Marker> for DatagenProvide
}

impl IterableDataProviderCached<LocaleDisplayNamesV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/transform/cldr/displaynames/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl DataProvider<RegionDisplayNamesV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<RegionDisplayNamesV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/transform/cldr/displaynames/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl DataProvider<ScriptDisplayNamesV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<ScriptDisplayNamesV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl DataProvider<VariantDisplayNamesV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<VariantDisplayNamesV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/transform/cldr/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ macro_rules! implement {
}

impl IterableDataProviderCached<$marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/transform/cldr/percent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl DataProvider<PercentEssentialsV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<PercentEssentialsV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::borrow::Cow;
use std::collections::HashSet;

use icu_experimental::personnames::provider::*;
use icu_provider::datagen::IterableDataProvider;
use icu_provider::prelude::*;
use zerovec::VarZeroVec;

use crate::provider::transform::cldr::cldr_serde::personnames::person_name_format_json_struct::Resource;
use crate::provider::IterableDataProviderCached;

impl DataProvider<PersonNamesFormatV1Marker> for crate::DatagenProvider {
fn load(&self, req: DataRequest) -> Result<DataResponse<PersonNamesFormatV1Marker>, DataError> {
Expand All @@ -34,8 +34,10 @@ impl DataProvider<PersonNamesFormatV1Marker> for crate::DatagenProvider {
}
}

impl IterableDataProvider<PersonNamesFormatV1Marker> for crate::DatagenProvider {
fn supported_requests(&self) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
impl IterableDataProviderCached<PersonNamesFormatV1Marker> for crate::DatagenProvider {
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
.cldr()?
.personnames()
Expand Down
4 changes: 2 additions & 2 deletions provider/datagen/src/transform/cldr/plurals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ macro_rules! implement {
}

impl IterableDataProviderCached<$marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down Expand Up @@ -124,7 +124,7 @@ impl DataProvider<PluralRangesV1Marker> for DatagenProvider {
}

impl IterableDataProviderCached<PluralRangesV1Marker> for DatagenProvider {
fn supported_locales_cached(
fn supported_requests_cached(
&self,
) -> Result<HashSet<(DataLocale, DataKeyAttributes)>, DataError> {
Ok(self
Expand Down
Loading

0 comments on commit 24761aa

Please sign in to comment.