Skip to content

Commit

Permalink
Fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Nov 9, 2023
1 parent 3427acd commit 71cb6c5
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ jobs:
fail_on: nothing
comment_mode: off

- run: cargo bench --bench run_ci
- run: cargo bench --bench run_iai

# Publish binaries when building for a tag
release:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ bytes = "1"
dashu = "0.4.0"
num-order = { version = "1.2.0" }
rand = "0.8.5"
thiserror = "1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libffi = { version = "3.2.0", optional = true }
Expand Down Expand Up @@ -121,5 +122,5 @@ modular-bitfield = { git = "https://github.com/mthom/modular-bitfield" }
name = "run"

[[bench]]
name = "run_ci"
name = "run_iai"
harness = false
42 changes: 23 additions & 19 deletions benches/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
#![feature(test)]

use iai_callgrind::black_box;
use maplit::btreemap;
use ordered_float::{Float, OrderedFloat};
use scryer_prolog::machine::{
parsed_results::{QueryResolution, QueryResult, Value},
parsed_results::{QueryMatch, QueryResolution, QueryResult, Value},
Machine,
};

extern crate test;
use test::Bencher;
use test::{bench, Bencher};

pub fn edges(query: String) -> QueryResult {
fn edges(query: String) -> QueryResult {
let mut machine = Machine::new_lib();

machine.load_module_string(
Expand All @@ -21,6 +23,7 @@ pub fn edges(query: String) -> QueryResult {
:- use_module(library(clpb)).
:- use_module(library(assoc)).
:- use_module(library(lists)).
:- use_module(library(pairs)).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Contiguous United States and DC as they appear in SGB:
Expand Down Expand Up @@ -135,10 +138,6 @@ pub fn edges(query: String) -> QueryResult {
edge(ut, wy).
edge(va, wv).
pairs_keys_values([], [], []).
pairs_keys_values([A-B|ABs], [A|As], [B|Bs]) :-
pairs_keys_values(ABs, As, Bs).
independent_set(*(NBs)) :-
findall(U-V, edge(U, V), Edges),
setof(U, V^(member(U-V, Edges);member(V-U, Edges)), Nodes),
Expand All @@ -149,25 +148,30 @@ pub fn edges(query: String) -> QueryResult {
not_both(Assoc, U-V, ~BU + ~BV) :-
get_assoc(U, Assoc, BU),
get_assoc(V, Assoc, BV).
independent_set_count(Count) :- independent_set(Sat), sat_count(Sat, Count).
"#,
),
);

machine.run_query(query)
}

#[bench]
fn bench_edges(b: &mut Bencher) {
pub fn bench_edges() {
let result = black_box(edges(black_box(String::from(
r#"independent_set(Sat), sat_count(Sat, Count)."#,
r#"independent_set_count(Count)."#,
))));
if let Ok(QueryResolution::Matches(ms)) = result {
if let Some(m) = ms.first() {
let count = &m.bindings["Count"];
println!("{:?}", count);
assert_eq!(*count, Value::from("217968"));
return;
}
}
panic!("failed to match")
assert_eq!(
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"Count" => Value::try_from("211954906".to_string()).unwrap(), // 217968?
}
)]))
);
}

#[bench]
fn bench_edges_test(_b: &mut Bencher) {
bench_edges()
}
30 changes: 0 additions & 30 deletions benches/run_ci.rs

This file was deleted.

16 changes: 16 additions & 0 deletions benches/run_iai.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(test)]
use iai_callgrind::{library_benchmark, library_benchmark_group, main};

mod run;

#[library_benchmark]
fn bench_edges() {
run::bench_edges();
}

library_benchmark_group!(
name = bench_scryer;
benchmarks = bench_edges
);

main!(library_benchmark_groups = bench_scryer);
10 changes: 2 additions & 8 deletions src/machine/lib_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,7 @@ mod tests {
}

// Check if the block is a query
if block.starts_with("query") {
// Extract the query from the block
let query = &block[5..];

if let Some(query) = block.strip_prefix("query") {
i += 1;
println!("query #{}: {}", i, query);
// Parse and execute the query
Expand All @@ -458,10 +455,7 @@ mod tests {

// Print the result
println!("{:?}", result);
} else if block.starts_with("consult") {
// Extract the code from the block
let code = &block[7..];

} else if let Some(code) = block.strip_prefix("consult") {
println!("load code: {}", code);

// Load the code into the machine
Expand Down
36 changes: 28 additions & 8 deletions src/machine/parsed_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::atom_table::*;
use dashu::*;
use ordered_float::OrderedFloat;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::{collections::HashMap, error};
use thiserror::Error;

pub type QueryResult = Result<QueryResolution, String>;

Expand Down Expand Up @@ -169,15 +170,15 @@ fn parse_prolog_response(input: &str) -> HashMap<String, String> {
}

impl TryFrom<String> for QueryResolutionLine {
type Error = ();
type Error = ValueFromStringError;
fn try_from(string: String) -> Result<Self, Self::Error> {
match string.as_str() {
"true" => Ok(QueryResolutionLine::True),
"false" => Ok(QueryResolutionLine::False),
_ => Ok(QueryResolutionLine::Match(
parse_prolog_response(&string)
.iter()
.map(|(k, v)| -> Result<(String, Value), ()> {
.map(|(k, v)| -> Result<(String, Value), ValueFromStringError> {
let key = k.to_string();
let value = v.to_string();
Ok((key, Value::try_from(value)?))
Expand Down Expand Up @@ -210,11 +211,23 @@ fn split_nested_list(input: &str) -> Vec<String> {
result
}

#[derive(Debug, Error)]
pub enum ValueFromStringError {
#[error("could not parse list because `{1}`, original string: `{0}`")]
ParseListError(String, Box<dyn error::Error>),
#[error("could not parse structure because `{1}`, original string: `{0}`")]
ParseStructureError(String, Box<dyn error::Error>),
#[error("could not parse it didn't match anything: `{0}`")]
NotMatchedError(String),
}

impl TryFrom<String> for Value {
type Error = ();
type Error = ValueFromStringError;
fn try_from(string: String) -> Result<Self, Self::Error> {
let trimmed = string.trim();

use ValueFromStringError::*;

if let Ok(float_value) = string.parse::<f64>() {
Ok(Value::Float(OrderedFloat(float_value)))
} else if let Ok(int_value) = string.parse::<i128>() {
Expand All @@ -229,7 +242,8 @@ impl TryFrom<String> for Value {
let values = split
.into_iter()
.map(Value::try_from)
.collect::<Result<Vec<_>, _>>()?;
.collect::<Result<Vec<_>, _>>()
.map_err(|e| ParseListError(string, Box::new(e)))?;

Ok(Value::List(values))
} else if trimmed.starts_with('{') && trimmed.ends_with('}') {
Expand All @@ -241,7 +255,10 @@ impl TryFrom<String> for Value {
if items.len() == 2 {
let _key = items[0].to_string();
let value = items[1].to_string();
values.push(Value::try_from(value)?);
match Value::try_from(value) {
Ok(value) => values.push(value),
Err(e) => return Err(ParseStructureError(string, Box::new(e))),
}
}
}

Expand All @@ -255,15 +272,18 @@ impl TryFrom<String> for Value {
if items.len() == 2 {
let _key = items[0].to_string();
let value = items[1].to_string();
values.push(Value::try_from(value)?);
match Value::try_from(value) {
Ok(value) => values.push(value),
Err(e) => return Err(ParseStructureError(string, Box::new(e))),
}
}
}

Ok(Value::Structure(atom!("<<>>"), values))
} else if !trimmed.contains(',') && !trimmed.contains('\'') && !trimmed.contains('"') {
Ok(Value::String(trimmed.into()))
} else {
Err(())
Err(ValueFromStringError::NotMatchedError(string))
}
}
}
Expand Down

0 comments on commit 71cb6c5

Please sign in to comment.