From 3d3baeed824bedf367bb0e632303d66355ae7a55 Mon Sep 17 00:00:00 2001 From: bakaq Date: Sat, 12 Oct 2024 19:08:45 -0300 Subject: [PATCH] Migrate benches --- benches/run_iai.rs | 4 +-- benches/setup.rs | 31 +++++++++++++------- src/lib.rs | 6 ++-- src/machine/lib_machine/lib_machine_tests.rs | 7 +++-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/benches/run_iai.rs b/benches/run_iai.rs index c29645c2..27f00339 100644 --- a/benches/run_iai.rs +++ b/benches/run_iai.rs @@ -5,7 +5,7 @@ mod setup; mod iai { use iai_callgrind::{library_benchmark, library_benchmark_group, main}; - use scryer_prolog::QueryResolution; + use scryer_prolog::LeafAnswer; use super::setup; @@ -13,7 +13,7 @@ mod iai { #[bench::count_edges(setup::prolog_benches()["count_edges"].setup())] #[bench::numlist(setup::prolog_benches()["numlist"].setup())] #[bench::csv_codename(setup::prolog_benches()["csv_codename"].setup())] - fn bench(mut run: impl FnMut() -> QueryResolution) -> QueryResolution { + fn bench(mut run: impl FnMut() -> Vec) -> Vec { run() } diff --git a/benches/setup.rs b/benches/setup.rs index 6087be44..ac055c08 100644 --- a/benches/setup.rs +++ b/benches/setup.rs @@ -1,7 +1,7 @@ use std::{collections::BTreeMap, fs, path::Path}; use maplit::btreemap; -use scryer_prolog::{Machine, QueryResolution, Value}; +use scryer_prolog::{LeafAnswer, Machine, MachineBuilder, Term}; pub fn prolog_benches() -> BTreeMap<&'static str, PrologBenchmark> { [ @@ -10,21 +10,21 @@ pub fn prolog_benches() -> BTreeMap<&'static str, PrologBenchmark> { "benches/edges.pl", // name of the prolog module file to load. use the same file in multiple benchmarks "independent_set_count(ky, Count).", // query to benchmark in the context of the loaded module. consider making the query adjustable to tune the run time to ~0.1s Strategy::Reuse, - btreemap! { "Count" => Value::Integer(2869176.into()) }, + btreemap! { "Count" => Term::integer(2869176) }, ), ( "numlist", "benches/numlist.pl", "run_numlist(1000000, Head).", Strategy::Reuse, - btreemap! { "Head" => Value::Integer(1.into())}, + btreemap! { "Head" => Term::integer(1) }, ), ( "csv_codename", "benches/csv.pl", "get_codename(\"0020\",Name).", Strategy::Reuse, - btreemap! { "Name" => Value::String("SPACE".into())}, + btreemap! { "Name" => Term::string("SPACE") }, ), ] .map(|b| { @@ -54,7 +54,7 @@ pub struct PrologBenchmark { pub filename: &'static str, pub query: &'static str, pub strategy: Strategy, - pub bindings: BTreeMap<&'static str, Value>, + pub bindings: BTreeMap<&'static str, Term>, } impl PrologBenchmark { @@ -64,28 +64,34 @@ impl PrologBenchmark { .file_stem() .and_then(|s| s.to_str()) .unwrap(); - let mut machine = Machine::new_lib(); + let mut machine = MachineBuilder::default().build(); machine.load_module_string(module_name, program); machine } #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] - pub fn setup(&self) -> impl FnMut() -> QueryResolution { + pub fn setup(&self) -> impl FnMut() -> Vec { let mut machine = self.make_machine(); let query = self.query; move || { use criterion::black_box; - black_box(machine.run_query(black_box(query.to_string()))).unwrap() + black_box( + machine + .run_query(black_box(query)) + .collect::, _>>() + .unwrap(), + ) } } } #[cfg(test)] mod test { + #[test] fn validate_benchmarks() { use super::prolog_benches; - use scryer_prolog::{QueryMatch, QueryResolution}; + use scryer_prolog::LeafAnswer; use std::{fmt::Write, fs}; struct BenchResult { @@ -100,10 +106,13 @@ mod test { let mut machine = r.make_machine(); let setup_inference_count = machine.get_inference_count(); - let result = machine.run_query(r.query.to_string()).unwrap(); + let result: Vec<_> = machine + .run_query(r.query) + .collect::>() + .unwrap(); let query_inference_count = machine.get_inference_count() - setup_inference_count; - let expected = QueryResolution::Matches(vec![QueryMatch::from(r.bindings.clone())]); + let expected = [LeafAnswer::from_bindings(r.bindings.clone())]; assert_eq!(result, expected, "validating benchmark {}", r.name); results.push(BenchResult { diff --git a/src/lib.rs b/src/lib.rs index 9a7a797e..85ee726a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,9 +4,6 @@ #[macro_use] extern crate static_assertions; -#[cfg(test)] -#[macro_use] -extern crate maplit; #[macro_use] pub(crate) mod macros; @@ -50,6 +47,7 @@ pub use machine::config::*; pub use machine::lib_machine::*; pub use machine::Machine; +/// Eval a source file in Wasm. #[cfg(target_arch = "wasm32")] #[wasm_bindgen] pub fn eval_code(s: &str) -> String { @@ -57,7 +55,7 @@ pub fn eval_code(s: &str) -> String { console_error_panic_hook::set_once(); - let mut wam = Machine::with_test_streams(); + let mut wam = MachineBuilder::default().build(); let bytes = wam.test_load_string(s); String::from_utf8_lossy(&bytes).to_string() } diff --git a/src/machine/lib_machine/lib_machine_tests.rs b/src/machine/lib_machine/lib_machine_tests.rs index e865f9e2..c05fccf6 100644 --- a/src/machine/lib_machine/lib_machine_tests.rs +++ b/src/machine/lib_machine/lib_machine_tests.rs @@ -420,7 +420,7 @@ fn complicated_term() { Term::String("asdf".into()), // String Term::List(vec![ Term::Integer(42.into()), // Fixnum - Term::Float(2.54.into()), // Float + Term::Float(2.54), // Float Term::Atom("asdf".into()), // Atom Term::Atom("a".into()), // Char Term::Compound( @@ -542,7 +542,10 @@ fn differentiate_anonymous_variables() { fn order_of_variables_in_binding() { let mut machine = MachineBuilder::default().build(); - let complete_answer: Vec<_> = machine.run_query("X = Y, Z = W.").collect::>().unwrap(); + let complete_answer: Vec<_> = machine + .run_query("X = Y, Z = W.") + .collect::>() + .unwrap(); assert_eq!( complete_answer, -- 2.54.0