From c8b1ec11315a8bc5dbb1cdf37f58f971b40638e2 Mon Sep 17 00:00:00 2001 From: Jacob Latonis Date: Sun, 17 May 2026 12:25:03 -0400 Subject: [PATCH 1/2] perf: only push prefix when not empty --- src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 26949ac..60af339 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,8 +96,10 @@ impl<'a> Flattener<'a> { /// A formatted key string with the separator inserted between prefix and suffix. fn build_key(&self, prefix: &str, suffix: &str) -> String { let mut key = String::with_capacity(prefix.len() + self.separator.len() + suffix.len()); - key.push_str(prefix); - key.push_str(self.separator); + if !prefix.is_empty() { + key.push_str(prefix); + key.push_str(self.separator); + } key.push_str(suffix); key } @@ -560,7 +562,7 @@ mod tests { }); let result: Value = flattener.flatten(&input); - assert_eq!(result, json!({"": ["a", "b"], ".b": "1"})); + assert_eq!(result, json!({"": ["a", "b"], "b": "1"})); } #[test] From 4c9aaef92eced0f2eb5bf3a59d08b39ed321ef51 Mon Sep 17 00:00:00 2001 From: Jacob Latonis Date: Sun, 17 May 2026 12:31:55 -0400 Subject: [PATCH 2/2] refactor: no deep clone for val --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 60af339..4e4f0c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -215,7 +215,7 @@ impl<'a> Flattener<'a> { if let Some(array) = value.as_array_mut() { array.push(obj.clone()); } else { - let existing = value.clone(); + let existing = std::mem::take(value); *value = json!(vec![existing, obj.clone()]); } }