From e65471c7a1a1a7b8195f40447549158953883985 Mon Sep 17 00:00:00 2001 From: greathongtu <69706194+greathongtu@users.noreply.github.com> Date: Sun, 2 Mar 2025 03:13:38 +0800 Subject: [PATCH] Optimize JSON merging by removing redundant key clones in serde_json operations (#25866) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hi Zed team! 👋 As a fan of Zed Editor who's excited to contribute, I noticed a small optimization opportunity in the JSON merging utilities. Changes: Removed redundant key.clone() calls in insert() operations within 2 functions: 1. merge_json_value_into 2. merge_non_null_json_value_into Why: Since we're already moving ownership of `source_object` 's contents and key is no longer used further, we could directly pass `key` to `insert` without cloning. Eliminates redundant allocations, improving performance slightly for JSON-heavy operations. Testing: I have tested this locally and all existing tests passed. The change preserves behavior while removing redundancy. Love using Zed and happy to contribute! Let me know if any adjustments are needed! Release Notes: - N/A --- crates/util/src/util.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/util/src/util.rs b/crates/util/src/util.rs index c8ce5ade0c1444402e12d6971b5cae04d4e1e704..bda78e9ddcfd3d9d67168b0c1ad204d91e73035f 100644 --- a/crates/util/src/util.rs +++ b/crates/util/src/util.rs @@ -332,7 +332,7 @@ pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json: if let Some(target) = target.get_mut(&key) { merge_json_value_into(value, target); } else { - target.insert(key.clone(), value); + target.insert(key, value); } } } @@ -360,7 +360,7 @@ pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut se if let Some(target) = target_object.get_mut(&key) { merge_non_null_json_value_into(value, target); } else if !value.is_null() { - target_object.insert(key.clone(), value); + target_object.insert(key, value); } } } else if !source.is_null() {