Skip to content

Commit

Permalink
update kellpossible#131: Make dashmap optional.
Browse files Browse the repository at this point in the history
You can activate the dashmap implementation by activating the feature flag `performance` in your `Cargo.toml` file. This will cause an additional usage of dashmap.

Alternatively, you may compile directly with
```
cargo build --features performance
```
  • Loading branch information
mrtryhard committed Jan 25, 2025
1 parent 412487e commit 7598f94
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
6 changes: 5 additions & 1 deletion i18n-embed-fl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "MIT"
proc-macro = true

[dependencies]
dashmap = "6.0"
dashmap = { version = "6.0", optional = true }
find-crate = { workspace = true }
fluent = { workspace = true }
fluent-syntax = { workspace = true }
Expand All @@ -32,3 +32,7 @@ doc-comment = { workspace = true }
env_logger = { workspace = true }
pretty_assertions = { workspace = true }
rust-embed = { workspace = true }

[features]
# Enables more performant version of the crate. May download extra dependencies.
performance = ["dep:dashmap"]
49 changes: 45 additions & 4 deletions i18n-embed-fl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ use std::{
path::Path,
sync::OnceLock,
};

#[cfg(not(feature = "performance"))]
use std::sync::{Arc, RwLock};

#[cfg(feature = "performance")]
use dashmap::{DashMap, Entry, mapref::one::Ref};

use syn::{parse::Parse, parse_macro_input, spanned::Spanned};
use unic_langid::LanguageIdentifier;

Expand Down Expand Up @@ -165,10 +172,44 @@ struct DomainSpecificData {
_assets: FileSystemAssets,
}

fn domains() -> &'static dashmap::DashMap<String, DomainSpecificData> {
static DOMAINS: OnceLock<dashmap::DashMap<String, DomainSpecificData>> = OnceLock::new();
#[derive(Default)]
struct DomainsMap {
#[cfg(not(feature = "performance"))]
map: RwLock<HashMap<String, Arc<DomainSpecificData>>>,

#[cfg(feature = "performance")]
map: dashmap::DashMap<String, DomainSpecificData>
}

impl DomainsMap {
#[cfg(not(feature = "performance"))]
fn get(&self, domain: &String) -> Option<Arc<DomainSpecificData>> {
match self.map.read().unwrap().get(domain) {
None => { None}
Some(data) => { Some(data.clone()) }
}
}

#[cfg(not(feature = "performance"))]
fn entry_or_insert(&self, domain: &String, data: DomainSpecificData) -> Arc<DomainSpecificData> {
self.map.write().unwrap().entry(domain.clone()).or_insert(Arc::new(data)).clone()
}

#[cfg(feature = "performance")]
fn get(&self, domain: &String) -> Option<Ref<String, DomainSpecificData>> {
self.map.get(domain)
}

#[cfg(feature = "performance")]
fn entry_or_insert(&self, domain: &String, data: DomainSpecificData) -> Ref<String, DomainSpecificData> {
self.map.entry(domain.clone()).or_insert(data).downgrade()
}
}

fn domains() -> &'static DomainsMap {
static DOMAINS: OnceLock<DomainsMap> = OnceLock::new();

DOMAINS.get_or_init(|| dashmap::DashMap::new())
DOMAINS.get_or_init(|| DomainsMap::default())
}

/// A macro to obtain localized messages and optionally their attributes, and check the `message_id`, `attribute_id`
Expand Down Expand Up @@ -421,7 +462,7 @@ pub fn fl(input: TokenStream) -> TokenStream {
_assets: assets,
};

domains().entry(domain.clone()).or_insert(data).downgrade()
domains().entry_or_insert(&domain, data)
};

let message_id_string = match &message_id {
Expand Down

0 comments on commit 7598f94

Please sign in to comment.