From 4fe47b7be4f531789c9e8a4ea4c46f495265f25e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 16 Feb 2020 20:07:52 +1000 Subject: [PATCH] cache i18n categories --- rslib/src/i18n/mod.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/rslib/src/i18n/mod.rs b/rslib/src/i18n/mod.rs index 9e7a43caa..2156822c6 100644 --- a/rslib/src/i18n/mod.rs +++ b/rslib/src/i18n/mod.rs @@ -25,6 +25,7 @@ macro_rules! tr_strs { } }; } +use std::collections::HashMap; pub use tr_strs; /// The folder containing ftl files for the provided language. @@ -124,13 +125,13 @@ impl I18n { inner: Arc::new(Mutex::new(I18nInner { langs, available_ftl_folders: supported, + cache: Default::default(), })), } } - pub fn get(&self, group: StringsGroup) -> I18nCategory { - let inner = self.inner.lock().unwrap(); - I18nCategory::new(&*inner.langs, &*inner.available_ftl_folders, group) + pub fn get(&self, group: StringsGroup) -> Arc { + self.inner.lock().unwrap().get(group) } } @@ -139,6 +140,19 @@ struct I18nInner { langs: Vec, // the available ftl folder subset of the user's preferred languages available_ftl_folders: Vec, + cache: HashMap>, +} + +impl I18nInner { + pub fn get(&mut self, group: StringsGroup) -> Arc { + let langs = &self.langs; + let avail = &self.available_ftl_folders; + + self.cache + .entry(group) + .or_insert_with(|| Arc::new(I18nCategory::new(langs, avail, group))) + .clone() + } } pub struct I18nCategory {