Skip to content

Commit

Permalink
Merge pull request #9 from tiatomee/lifetimes
Browse files Browse the repository at this point in the history
Add support for lifetimes in relations
  • Loading branch information
ekzhang authored Dec 18, 2020
2 parents 2090d22 + affe32c commit 0144075
Show file tree
Hide file tree
Showing 14 changed files with 582 additions and 138 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Add support for lifetimes in relations and the `ref` keyword (#9)
- Add benchmarks and support for custom hashers (#9)

## 0.1.3 - 2020-11-21

- Add shorter syntax for defining fact-rules (#6)
Expand Down
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ edition = "2018"

[lib]
proc-macro = true
bench = false

[[bench]]
name = "graph_walking"
harness = false

[[bench]]
name = "fibonacci"
harness = false


[dev-dependencies]
trybuild = "1.0"
criterion = "0.3"
fnv = "1.0"

[dependencies]
syn = { version = "1.0", features = ["full"] }
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ closure for large graphs (~1000 nodes) run at comparable speed to compiled
[Souffle](https://souffle-lang.github.io/), and at a fraction of the
compilation time.

For benchmarks, see the [`benches/` directory](benches/).
The benchmarks can be run using `cargo bench`.

This macro generates a `Crepe` struct in the current module, as well as structs
for all of the declared relations. This means that to integrate Crepe inside a
larger program, you should put it in its own module with related code. See the
Expand Down
41 changes: 41 additions & 0 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use crepe::crepe;

crepe! {
@input
struct Depth(u128);

@output
struct Fib(u128, u128);

Fib(0, 0) <- (true);
Fib(1, 1) <- (true);

Fib(n + 2, x + y) <-
Depth(depth),
Fib(n, x), Fib(n + 1, y), (n <= depth);
}

// Doesn't return the fibonacci number but instead the number of relations generated.
fn fibonacci_length(n: u128) -> usize {
let mut rt = Crepe::new();

rt.extend(&[Depth(n)]);

let (fibs,) = rt.run_with_hasher::<fnv::FnvBuildHasher>();
fibs.len()
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function_over_inputs(
"fibonacci",
|b, n| {
b.iter(|| fibonacci_length(black_box(*n)));
},
vec![50, 100, 150],
);
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
59 changes: 59 additions & 0 deletions benches/graph_walking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use crepe::crepe;

const MAX_PATH_LEN: u32 = 50;

crepe! {
@input
struct Edge(i32, i32, u32);

@output
struct Walk(i32, i32, u32);

@output
struct NoWalk(i32, i32);

struct Node(i32);

Node(x) <- Edge(x, _, _);
Node(x) <- Edge(_, x, _);

Walk(x, x, 0) <- Node(x);
Walk(x, z, len1 + len2) <-
Edge(x, y, len1),
Walk(y, z, len2),
(len1 + len2 <= MAX_PATH_LEN);

NoWalk(x, y) <- Node(x), Node(y), !Walk(x, y, _);
}

fn walk(n: usize) -> (usize, usize) {
let n = n as i32;
let mut edges = Vec::new();
for i in 0..n {
for j in 0..n {
if i + j % 50 < 2 {
edges.push(Edge(i, j, 5));
}
}
}

let mut runtime = Crepe::new();
runtime.extend(edges);
let (walk, nowalk) = runtime.run_with_hasher::<fnv::FnvBuildHasher>();
(walk.len(), nowalk.len())
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function_over_inputs(
"walk",
|b, input| {
b.iter(|| walk(black_box(*input)));
},
vec![128, 256, 512],
);
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading

0 comments on commit 0144075

Please sign in to comment.