From 436d2032c4aeac23434732ad374c3a46883aa5cd Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Mon, 11 Apr 2022 15:40:07 -0700 Subject: [PATCH 1/2] Add benchmarks to the flatten-serde-json subcrate --- flatten-serde-json/Cargo.toml | 9 +++-- flatten-serde-json/benches/benchmarks.rs | 42 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 flatten-serde-json/benches/benchmarks.rs diff --git a/flatten-serde-json/Cargo.toml b/flatten-serde-json/Cargo.toml index 7c18656f1..0220e8ceb 100644 --- a/flatten-serde-json/Cargo.toml +++ b/flatten-serde-json/Cargo.toml @@ -5,7 +5,12 @@ edition = "2021" description = "Flatten serde-json objects like elastic search" readme = "README.md" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] serde_json = "1.0" + +[dev-dependencies] +criterion = { version = "0.3", features = ["html_reports"] } + +[[bench]] +name = "benchmarks" +harness = false diff --git a/flatten-serde-json/benches/benchmarks.rs b/flatten-serde-json/benches/benchmarks.rs new file mode 100644 index 000000000..6536bb513 --- /dev/null +++ b/flatten-serde-json/benches/benchmarks.rs @@ -0,0 +1,42 @@ +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use flatten_serde_json::flatten; +use serde_json::json; + +pub fn flatten_simple(c: &mut Criterion) { + let mut input = json!({ + "a": { + "b": "c", + "d": "e", + "f": "g" + } + }); + let object = input.as_object_mut().unwrap(); + + c.bench_with_input(BenchmarkId::new("flatten", "simple"), &object, |b, input| { + b.iter(|| flatten(input)) + }); +} + +pub fn flatten_complex(c: &mut Criterion) { + let mut input = json!({ + "a": [ + "b", + ["c", "d"], + { "e": ["f", "g"] }, + [ + { "h": "i" }, + { "e": ["j", { "z": "y" }] }, + ], + ["l"], + "m", + ] + }); + let object = input.as_object_mut().unwrap(); + + c.bench_with_input(BenchmarkId::new("flatten", "complex"), &object, |b, input| { + b.iter(|| flatten(input)) + }); +} + +criterion_group!(benches, flatten_simple, flatten_complex); +criterion_main!(benches); From b3cec1a3832a4663635c873a700fbb4cc1089108 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Mon, 11 Apr 2022 16:12:56 -0700 Subject: [PATCH 2/2] Prefer using direct method calls instead of using the json macros --- flatten-serde-json/src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/flatten-serde-json/src/lib.rs b/flatten-serde-json/src/lib.rs index 734ae2a24..8312f5bd6 100644 --- a/flatten-serde-json/src/lib.rs +++ b/flatten-serde-json/src/lib.rs @@ -1,6 +1,6 @@ #![doc = include_str!("../README.md")] -use serde_json::{json, Map, Value}; +use serde_json::{Map, Value}; pub fn flatten(json: &Map) -> Map { let mut obj = Map::new(); @@ -42,7 +42,7 @@ fn insert_value(base_json: &mut Map, key: &str, to_insert: Value) debug_assert!(!to_insert.is_object()); debug_assert!(!to_insert.is_array()); - // does the field aleardy exists? + // does the field already exists? if let Some(value) = base_json.get_mut(key) { // is it already an array if let Some(array) = value.as_array_mut() { @@ -50,16 +50,18 @@ fn insert_value(base_json: &mut Map, key: &str, to_insert: Value) // or is there a collision } else { let value = std::mem::take(value); - base_json[key] = json!([value, to_insert]); + base_json[key] = Value::Array(vec![value, to_insert]); } // if it does not exist we can push the value untouched } else { - base_json.insert(key.to_string(), json!(to_insert)); + base_json.insert(key.to_string(), to_insert); } } #[cfg(test)] mod tests { + use serde_json::json; + use super::*; #[test]