From 5d4b561827a1a55da9dfdc1ad8fc6358cfdaff9f Mon Sep 17 00:00:00 2001 From: Ykong <66518156+yk0n9@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:30:14 +0800 Subject: [PATCH] Reduce unnecessary memory copying (#89) --- crates/support/src/backend.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/support/src/backend.rs b/crates/support/src/backend.rs index c3ead73..54fd031 100644 --- a/crates/support/src/backend.rs +++ b/crates/support/src/backend.rs @@ -70,16 +70,15 @@ impl SimpleBackend { /// ``` pub fn add_translations(&mut self, locale: &str, data: &HashMap<&str, &str>) { let data = data - .clone() - .into_iter() - .map(|(k, v)| (k.into(), v.into())) + .iter() + .map(|(k, v)| ((*k).into(), (*v).into())) .collect::>(); - if let Some(trs) = self.translations.get_mut(locale) { - trs.extend(data.clone()); - } else { - self.translations.insert(locale.into(), data.clone()); - } + let trs = self + .translations + .entry(locale.into()) + .or_insert(HashMap::new()); + trs.extend(data); } }