From 276324ae09f4fa60c65518991cf89ce6bd7900ac Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Fri, 30 Sep 2022 07:35:55 +0000 Subject: [PATCH] Make remove_json_null_values O(n) instead of O(n^2) --- google-apis-common/src/lib.rs | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/google-apis-common/src/lib.rs b/google-apis-common/src/lib.rs index 3540fc9b46..a9c696cf05 100644 --- a/google-apis-common/src/lib.rs +++ b/google-apis-common/src/lib.rs @@ -752,32 +752,14 @@ where // TODO(ST): Allow sharing common code between program types pub fn remove_json_null_values(value: &mut json::value::Value) { - match *value { - json::value::Value::Object(ref mut map) => { - let mut for_removal = Vec::new(); - - for (key, mut value) in map.iter_mut() { - if value.is_null() { - for_removal.push(key.clone()); - } else { - remove_json_null_values(&mut value); - } - } - - for key in &for_removal { - map.remove(key); - } + match value { + json::value::Value::Object(map) => { + map.retain(|_, value| !value.is_null()); + map.values_mut().for_each(remove_json_null_values); } - json::value::Value::Array(ref mut arr) => { - let mut i = 0; - while i < arr.len() { - if arr[i].is_null() { - arr.remove(i); - } else { - remove_json_null_values(&mut arr[i]); - i += 1; - } - } + json::value::Value::Array(arr) => { + arr.retain(|value| !value.is_null()); + arr.iter_mut().for_each(remove_json_null_values); } _ => {} }