Skip to content

Commit 8f4788f

Browse files
committed
Use Vec instead of HashMap to speed up perf and retain order
1 parent 9a5ea3a commit 8f4788f

File tree

5 files changed

+19
-26
lines changed

5 files changed

+19
-26
lines changed

benches/negotiate.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ use fluent_locale::convert_vec_str_to_langids_lossy;
66
use fluent_locale::negotiate_languages;
77

88
fn negotiate_bench(c: &mut Criterion) {
9-
let requested = vec!["de", "it", "ru"];
10-
let available = vec![
9+
let requested = &["de", "it", "ru"];
10+
let available = &[
1111
"en-US", "fr", "de", "en-GB", "it", "pl", "ru", "sr-Cyrl", "sr-Latn", "zh-Hant", "zh-Hans",
1212
"ja-JP", "he-IL", "de-DE", "de-IT",
1313
];
1414

15-
let requested = convert_vec_str_to_langids_lossy(&requested);
16-
let available = convert_vec_str_to_langids_lossy(&available);
15+
let requested = convert_vec_str_to_langids_lossy(requested);
16+
let available = convert_vec_str_to_langids_lossy(available);
1717

18-
c.bench_function("negotiate", move |b| {
18+
c.bench_function("negotiate", |b| {
1919
b.iter(|| {
2020
negotiate_languages(
2121
&requested,

src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ where
2929
I: IntoIterator<Item = J>,
3030
J: AsRef<str> + 'a,
3131
{
32-
let mut result = vec![];
33-
for elem in input.into_iter() {
34-
result.push(elem.as_ref().parse()?);
35-
}
36-
Ok(result)
32+
input.into_iter().map(|s| s.as_ref().parse()).collect()
3733
}
3834

3935
pub fn convert_vec_str_to_langids_lossy<'a, I, J>(input: I) -> Vec<unic_langid::LanguageIdentifier>

src/negotiate/likely_subtags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ impl MockLikelySubtags for LanguageIdentifier {
4242
} else {
4343
self.clear_region();
4444
}
45-
return true;
45+
true
4646
}
4747
}

src/negotiate/mod.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
//! ```
120120
//!
121121
122-
use std::collections::HashMap;
123122
use unic_langid::LanguageIdentifier;
124123

125124
#[cfg(not(feature = "cldr"))]
@@ -141,11 +140,8 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
141140
) -> Vec<&'a A> {
142141
let mut supported_locales = vec![];
143142

144-
let mut av_map: HashMap<&'a LanguageIdentifier, &'a A> = HashMap::new();
145-
146-
for av in available.iter() {
147-
av_map.insert(av.as_ref(), av);
148-
}
143+
let mut available_locales: Vec<(&LanguageIdentifier, &A)> =
144+
available.iter().map(|a| (a.as_ref(), a)).collect();
149145

150146
for req in requested {
151147
let mut req = req.as_ref().to_owned();
@@ -156,7 +152,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
156152
let mut match_found = false;
157153

158154
// 1) Try to find a simple (case-insensitive) string match for the request.
159-
av_map.retain(|key, value| {
155+
available_locales.retain(|(key, value)| {
160156
if strategy != NegotiationStrategy::Filtering && match_found {
161157
return true;
162158
}
@@ -180,7 +176,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
180176
match_found = false;
181177

182178
// 2) Try to match against the available locales treated as ranges.
183-
av_map.retain(|key, value| {
179+
available_locales.retain(|(key, value)| {
184180
if strategy != NegotiationStrategy::Filtering && match_found {
185181
return true;
186182
}
@@ -205,7 +201,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
205201

206202
// 3) Try to match against a maximized version of the requested locale
207203
if req.add_likely_subtags() {
208-
av_map.retain(|key, value| {
204+
available_locales.retain(|(key, value)| {
209205
if strategy != NegotiationStrategy::Filtering && match_found {
210206
return true;
211207
}
@@ -231,7 +227,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
231227

232228
// 4) Try to match against a variant as a range
233229
req.clear_variants();
234-
av_map.retain(|key, value| {
230+
available_locales.retain(|(key, value)| {
235231
if strategy != NegotiationStrategy::Filtering && match_found {
236232
return true;
237233
}
@@ -257,7 +253,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
257253
// 5) Try to match against the likely subtag without region
258254
req.clear_region();
259255
if req.add_likely_subtags() {
260-
av_map.retain(|key, value| {
256+
available_locales.retain(|(key, value)| {
261257
if strategy != NegotiationStrategy::Filtering && match_found {
262258
return true;
263259
}
@@ -283,7 +279,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
283279

284280
// 6) Try to match against a region as a range
285281
req.clear_region();
286-
av_map.retain(|key, value| {
282+
available_locales.retain(|(key, value)| {
287283
if strategy != NegotiationStrategy::Filtering && match_found {
288284
return true;
289285
}
@@ -311,7 +307,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
311307
pub fn negotiate_languages<
312308
'a,
313309
R: 'a + AsRef<LanguageIdentifier>,
314-
A: 'a + AsRef<LanguageIdentifier>,
310+
A: 'a + AsRef<LanguageIdentifier> + PartialEq,
315311
>(
316312
requested: &[R],
317313
available: &'a [A],
@@ -325,7 +321,7 @@ pub fn negotiate_languages<
325321
if supported.is_empty() {
326322
supported.push(default);
327323
}
328-
} else if !supported.iter().any(|s| s.as_ref() == default.as_ref()) {
324+
} else if !supported.contains(&default) {
329325
supported.push(default);
330326
}
331327
}

tests/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ fn cldr_feature() {
178178
&[langid!("mn-Latn"), langid!("mn-Cyrl")],
179179
None,
180180
NegotiationStrategy::Filtering
181-
).len(),
181+
)
182+
.len(),
182183
2
183184
);
184185
}

0 commit comments

Comments
 (0)