Repository: toml-rs/toml
Pull request #437 (2023-01-13 17:58:20)

test: Update benches for future API


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 7b4a40e0..1bec3327 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -950,7 +950,6 @@ dependencies = [
 name = "toml_edit"
 version = "0.17.1"
 dependencies = [
- "criterion",
  "indexmap",
  "itertools",
  "kstring",
@@ -960,7 +959,6 @@ dependencies = [
  "serde_json",
  "serde_spanned",
  "snapbox",
- "toml 0.5.10",
  "toml-test-data",
  "toml-test-harness",
  "toml_datetime",
@@ -982,8 +980,10 @@ dependencies = [
  "serde",
  "serde_derive",
  "serde_json",
+ "toml 0.5.10",
  "toml 0.5.9",
  "toml-test-harness",
+ "toml_edit",
 ]
 
 [[package]]
diff --git a/crates/toml_edit/benches/cargo.rs b/crates/test-suite/benches/cargo.rs
similarity index 89%
rename from crates/toml_edit/benches/cargo.rs
rename to crates/test-suite/benches/cargo.rs
index e595e422..c31e5572 100644
--- a/crates/toml_edit/benches/cargo.rs
+++ b/crates/test-suite/benches/cargo.rs
@@ -10,28 +10,22 @@ fn cargo_manifest(c: &mut Criterion) {
             sample.parse::<toml_edit::Document>().unwrap();
             b.iter(|| sample.parse::<toml_edit::Document>());
         });
-        #[cfg(feature = "easy")]
-        group.bench_with_input(
-            BenchmarkId::new("toml_edit::easy::de", name),
-            &len,
-            |b, _| {
-                toml_edit::easy::from_str::<manifest::Manifest>(sample).unwrap();
-                b.iter(|| toml_edit::easy::from_str::<manifest::Manifest>(sample).unwrap())
-            },
-        );
-        #[cfg(feature = "easy")]
-        group.bench_with_input(
-            BenchmarkId::new("toml_edit::easy::Value", name),
-            &len,
-            |b, _| {
-                sample.parse::<toml_edit::easy::Value>().unwrap();
-                b.iter(|| sample.parse::<toml_edit::easy::Value>());
-            },
-        );
-        group.bench_with_input(BenchmarkId::new("toml", name), &len, |b, _| {
+        group.bench_with_input(BenchmarkId::new("toml::Value", name), &len, |b, _| {
             sample.parse::<toml::Value>().unwrap();
             b.iter(|| sample.parse::<toml::Value>());
         });
+        group.bench_with_input(BenchmarkId::new("toml::de", name), &len, |b, _| {
+            toml::from_str::<manifest::Manifest>(sample).unwrap();
+            b.iter(|| toml::from_str::<manifest::Manifest>(sample));
+        });
+        group.bench_with_input(BenchmarkId::new("toml_old::Value", name), &len, |b, _| {
+            sample.parse::<toml_old::Value>().unwrap();
+            b.iter(|| sample.parse::<toml_old::Value>());
+        });
+        group.bench_with_input(BenchmarkId::new("toml_old::de", name), &len, |b, _| {
+            toml_old::from_str::<manifest::Manifest>(sample).unwrap();
+            b.iter(|| toml_old::from_str::<manifest::Manifest>(sample));
+        });
     }
     group.finish();
 }
@@ -172,7 +166,6 @@ vendored-openssl = ["openssl/vendored"]
 pretty-env-logger = ["pretty_env_logger"]
 "#;
 
-#[cfg(feature = "easy")]
 mod manifest {
     use std::collections::HashMap;
 
diff --git a/crates/toml_edit/Cargo.toml b/crates/toml_edit/Cargo.toml
index 515ee0e9..12d29868 100644
--- a/crates/toml_edit/Cargo.toml
+++ b/crates/toml_edit/Cargo.toml
@@ -61,8 +61,6 @@ toml-test-harness = "0.4.2"
 toml-test-data = "1.2.0"
 libtest-mimic = "0.6.0"
 snapbox = { version = "0.4.3", features = ["harness"] }
-criterion = "0.4.0"
-toml = "0.5.10"
 
 [[test]]
 name = "decoder_compliance"
@@ -84,14 +82,6 @@ harness = false
 name = "invalid"
 harness = false
 
-[[bench]]
-name = "cargo"
-harness = false
-
-[[bench]]
-name = "linear"
-harness = false
-
 [[example]]
 name = "visit"
 test = true
diff --git a/crates/toml_edit/benches/linear.rs b/crates/toml_edit/benches/linear.rs
deleted file mode 100644
index e506ead1..00000000
--- a/crates/toml_edit/benches/linear.rs
+++ /dev/null
@@ -1,81 +0,0 @@
-use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
-
-fn map(c: &mut Criterion) {
-    let mut group = c.benchmark_group("map");
-    let samples = [10, 100];
-    for sample in samples {
-        let mut s = String::new();
-        for i in 0..sample {
-            s += &format!("[header_no_{}]\n", i);
-            s += "entry = 42\n"
-        }
-        let len = s.len();
-        group.throughput(Throughput::Bytes(len as u64));
-
-        group.bench_with_input(BenchmarkId::new("toml_edit", sample), &sample, |b, _| {
-            let s = s.clone();
-            s.parse::<toml_edit::Document>().unwrap();
-            let s = black_box(s);
-            b.iter(|| {
-                black_box(s.parse::<toml_edit::Document>().unwrap());
-            })
-        });
-        #[cfg(feature = "easy")]
-        group.bench_with_input(
-            BenchmarkId::new("toml_edit::easy", sample),
-            &sample,
-            |b, _| {
-                let s = s.clone();
-                s.parse::<toml_edit::easy::Value>().unwrap();
-                let s = black_box(s);
-                b.iter(|| {
-                    black_box(s.parse::<toml_edit::easy::Value>().unwrap());
-                })
-            },
-        );
-        group.bench_with_input(BenchmarkId::new("toml", sample), &sample, |b, _| {
-            let s = s.clone();
-            s.parse::<toml::Value>().unwrap();
-            let s = black_box(s);
-            b.iter(|| {
-                black_box(s.parse::<toml::Value>().unwrap());
-            })
-        });
-    }
-    group.finish();
-}
-
-fn array(c: &mut Criterion) {
-    let mut group = c.benchmark_group("array");
-    let samples = [10, 100];
-    for sample in samples {
-        let mut s = String::new();
-        for _ in 0..sample {
-            s += "[[header]]\n";
-            s += "entry = 42\n"
-        }
-        let len = s.len();
-        group.throughput(Throughput::Bytes(len as u64));
-
-        group.bench_with_input(BenchmarkId::new("toml_edit", sample), &sample, |b, _| {
-            let s = s.clone();
-            s.parse::<toml_edit::Document>().unwrap();
-            let s = black_box(s);
-            b.iter(|| {
-                black_box(s.parse::<toml_edit::Document>().unwrap());
-            })
-        });
-        group.bench_with_input(BenchmarkId::new("toml", sample), &sample, |b, _| {
-            let s = s.clone();
-            s.parse::<toml::Value>().unwrap();
-            let s = black_box(s);
-            b.iter(|| {
-                black_box(s.parse::<toml::Value>().unwrap());
-            })
-        });
-    }
-    group.finish();
-}
-
-criterion_group!(benches, map, array);
-criterion_main!(benches);
diff --git a/crates/toml_edit/src/ser/mod.rs b/crates/toml_edit/src/ser/mod.rs
index 497da194..c829d7a2 100644
--- a/crates/toml_edit/src/ser/mod.rs
+++ b/crates/toml_edit/src/ser/mod.rs
@@ -131,7 +131,7 @@ where
 ///     },
 /// };
 ///
-/// let toml = toml::to_string(&config).unwrap();
+/// let toml = toml_edit::ser::to_string(&config).unwrap();
 /// println!("{}", toml)
 /// ```
 pub fn to_string<T: ?Sized>(value: &T) -> Result<String, Error>


Test patch:
diff --git a/crates/test-suite/Cargo.toml b/crates/test-suite/Cargo.toml
index 2fda776f..8a0c64a4 100644
--- a/crates/test-suite/Cargo.toml
+++ b/crates/test-suite/Cargo.toml
@@ -1,23 +1,28 @@
 [package]
 name = "toml_test_suite"
 version = "0.0.0"
-authors = ["Alex Crichton <alex@alexcrichton.com>"]
 publish = false
 edition = "2021"
 rust-version = "1.60.0"  # MSRV
 
-[[bench]]
-name = "linear"
-harness = false
-
 [dev-dependencies]
 criterion = "0.4.0"
 toml = { path = "../toml" }
+toml_edit = { path = "../toml_edit" }
+toml_old = { version = "0.5.10", package = "toml" }
 serde = { version = "1.0.152", features = ["derive"] }
 serde_derive = "1.0.152"
 serde_json = "1.0.91"
 toml-test-harness = "0.4.2"
 
+[[bench]]
+name = "cargo"
+harness = false
+
+[[bench]]
+name = "linear"
+harness = false
+
 [[test]]
 name = "decoder_compliance"
 harness = false
diff --git a/crates/test-suite/benches/linear.rs b/crates/test-suite/benches/linear.rs
index 18d84b1c..90a8dcb9 100644
--- a/crates/test-suite/benches/linear.rs
+++ b/crates/test-suite/benches/linear.rs
@@ -1,37 +1,84 @@
-// Regressoion test for https://github.com/alexcrichton/toml-rs/issues/342
+use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
 
-use criterion::{black_box, criterion_group, criterion_main, Criterion};
-use toml::Value;
+fn map(c: &mut Criterion) {
+    let mut group = c.benchmark_group("map");
+    let samples = [10, 100];
+    for sample in samples {
+        let mut s = String::new();
+        for i in 0..sample {
+            s += &format!("[header_no_{}]\n", i);
+            s += "entry = 42\n"
+        }
+        let len = s.len();
+        group.throughput(Throughput::Bytes(len as u64));
 
-fn parse(bench: &mut Criterion, name: &str, entries: usize, f: impl Fn(usize) -> String) {
-    let mut s = String::new();
-    for i in 0..entries {
-        s += &f(i);
-        s += "entry = 42\n"
+        group.bench_with_input(BenchmarkId::new("toml_edit", sample), &sample, |b, _| {
+            let s = s.clone();
+            s.parse::<toml_edit::Document>().unwrap();
+            let s = black_box(s);
+            b.iter(|| {
+                black_box(s.parse::<toml_edit::Document>().unwrap());
+            })
+        });
+        group.bench_with_input(BenchmarkId::new("toml", sample), &sample, |b, _| {
+            let s = s.clone();
+            s.parse::<toml::Value>().unwrap();
+            let s = black_box(s);
+            b.iter(|| {
+                black_box(s.parse::<toml::Value>().unwrap());
+            })
+        });
+        group.bench_with_input(BenchmarkId::new("toml-v0.5", sample), &sample, |b, _| {
+            let s = s.clone();
+            s.parse::<toml_old::Value>().unwrap();
+            let s = black_box(s);
+            b.iter(|| {
+                black_box(s.parse::<toml_old::Value>().unwrap());
+            })
+        });
     }
-    let s = black_box(s);
-    bench.bench_function(name, |b| {
-        b.iter(|| {
-            black_box(s.parse::<Value>().unwrap());
-        })
-    });
+    group.finish();
 }
 
-fn map_10(bench: &mut Criterion) {
-    parse(bench, "map_10", 10, |i| format!("[header_no_{}]\n", i))
-}
-
-fn map_100(bench: &mut Criterion) {
-    parse(bench, "map_100", 100, |i| format!("[header_no_{}]\n", i))
-}
+fn array(c: &mut Criterion) {
+    let mut group = c.benchmark_group("array");
+    let samples = [10, 100];
+    for sample in samples {
+        let mut s = String::new();
+        for _ in 0..sample {
+            s += "[[header]]\n";
+            s += "entry = 42\n"
+        }
+        let len = s.len();
+        group.throughput(Throughput::Bytes(len as u64));
 
-fn array_10(bench: &mut Criterion) {
-    parse(bench, "array_10", 10, |_i| "[[header]]\n".to_owned())
-}
-
-fn array_100(bench: &mut Criterion) {
-    parse(bench, "array_100", 100, |_i| "[[header]]\n".to_owned())
+        group.bench_with_input(BenchmarkId::new("toml_edit", sample), &sample, |b, _| {
+            let s = s.clone();
+            s.parse::<toml_edit::Document>().unwrap();
+            let s = black_box(s);
+            b.iter(|| {
+                black_box(s.parse::<toml_edit::Document>().unwrap());
+            })
+        });
+        group.bench_with_input(BenchmarkId::new("toml", sample), &sample, |b, _| {
+            let s = s.clone();
+            s.parse::<toml::Value>().unwrap();
+            let s = black_box(s);
+            b.iter(|| {
+                black_box(s.parse::<toml::Value>().unwrap());
+            })
+        });
+        group.bench_with_input(BenchmarkId::new("toml-v0.5", sample), &sample, |b, _| {
+            let s = s.clone();
+            s.parse::<toml_old::Value>().unwrap();
+            let s = black_box(s);
+            b.iter(|| {
+                black_box(s.parse::<toml_old::Value>().unwrap());
+            })
+        });
+    }
+    group.finish();
 }
 
-criterion_group!(benches, map_10, map_100, array_10, array_100);
+criterion_group!(benches, map, array);
 criterion_main!(benches);