Skip to content

Commit eb5b7db

Browse files
committed
reimplement criterion benchmarks with new parser
1 parent 18ce262 commit eb5b7db

File tree

7 files changed

+90
-62
lines changed

7 files changed

+90
-62
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "aoc-main"
33
description = "A macro which defines a handful main for the Advent of Code"
4+
version = "0.2.0"
45
authors = ["Rémi Dupré <[email protected]>"]
56
repository = "https://github.com/remi-dupre/aoc"
67
license = "Apache-2.0"
7-
version = "0.1.0"
88
readme = "README.md"
99
keywords = ["advent", "aoc", "macro"]
1010
edition = "2018"

src/bin/example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mod day4 {
6565
}
6666
}
6767

68-
aoc::main! {
68+
aoc_main::main! {
6969
year 2019;
7070
day1 : generator => part_1, part_2;
7171
day2 => part_1_option?, part_1_result?, part_2_option?, part_2_result?;

src/lib.rs

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pub mod input;
22
pub mod parse;
3-
pub mod run;
43
pub mod utils;
54

65
use std::path::PathBuf;
@@ -36,28 +35,40 @@ pub struct Opt {
3635
pub bench: bool,
3736
}
3837

38+
impl Opt {
39+
pub fn day_enabled(&self, day: &str) -> bool {
40+
day.starts_with("day") && self.days.iter().any(|d| d == &day[3..])
41+
}
42+
}
43+
3944
#[macro_export]
4045
macro_rules! main {
4146
( year $year: expr; $( $tail: tt )* ) => {
4247
use std::fs::read_to_string;
4348
use std::io::Read;
4449
use std::time::Instant;
4550

46-
use $crate::{clap::Clap, extract_day, parse, run_day};
51+
use $crate::clap::Clap;
52+
use $crate::criterion::*;
53+
use $crate::{bench_day, extract_day, parse, run_day};
4754

4855
const YEAR: u16 = $year;
4956

5057
fn main() {
5158
let mut opt = $crate::Opt::parse();
52-
let days = parse! { extract_day {}; $( $tail )* };
5359

54-
// if opt.bench {
55-
// bench::run_benchs();
56-
// }
60+
if opt.bench {
61+
bench();
62+
}
5763

5864
if opt.days.is_empty() {
59-
opt.days = days.iter().map(|s| s[3..].to_string()).collect();
65+
opt.days = parse!(extract_day {}; $( $tail )*)
66+
.iter()
67+
.map(|s| s[3..].to_string())
68+
.collect();
6069
} else {
70+
let days = parse! { extract_day {}; $( $tail )* };
71+
6172
let ignored_days: Vec<_> = opt.days
6273
.iter()
6374
.filter(|day| !days.contains(&format!("day{}", day).as_str()))
@@ -81,56 +92,22 @@ macro_rules! main {
8192
}
8293

8394
for (i, day) in opt.days.iter().enumerate() {
84-
let module_name = format!("day{}", day);
85-
86-
if !days.contains(&module_name.as_str()) {
87-
eprintln!(
88-
"Module `{}` was not registered, available are: {}",
89-
module_name,
90-
days.join(", "),
91-
);
92-
}
93-
9495
parse! {
95-
run_day { i, module_name, YEAR, opt };
96+
run_day { i, format!("day{}", day), YEAR, opt };
9697
$( $tail )*
9798
};
9899
}
99100
}
100101

102+
fn bench() {
103+
let mut criterion = Criterion::default().configure_from_args();
101104

102-
// mod bench {
103-
// use $crate::criterion::*;
104-
//
105-
// pub fn run_benchs() {
106-
// main();
107-
// }
108-
//
109-
// $(
110-
// fn $day(c: &mut Criterion) {
111-
// let mut group = c.benchmark_group(stringify!($day));
112-
// let day = stringify!($day)[3..].parse().expect("dayX expected for module");
113-
//
114-
// let data = $crate::input::get_input(crate::YEAR, day)
115-
// .expect("could not fetch input");
116-
//
117-
// let input = data.as_str();
118-
// $( let input = crate::$day::$generator(&data); )?
119-
//
120-
//
121-
// $(
122-
// group.bench_function(
123-
// stringify!($solution),
124-
// |b| b.iter(|| crate::$day::$solution(&input)),
125-
// );
126-
// )+
127-
//
128-
// group.finish();
129-
// }
130-
// )+
131-
//
132-
// criterion_group!(benches, $($day),+);
133-
// criterion_main!(benches);
134-
// }
105+
parse! {
106+
bench_day { &mut criterion, YEAR };
107+
$( $tail )*
108+
};
109+
110+
criterion.final_summary();
111+
}
135112
};
136113
}

src/parse/gen_bench.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! Generate code for criterion benchmarks.
2+
3+
#[macro_export]
4+
macro_rules! bench_day {
5+
(
6+
{ $criterion: expr, $year: expr },
7+
{ day $day: ident { $gen: tt { $( $sol: tt )* } } }
8+
) => {{
9+
let day = stringify!($day)[3..].parse().expect("days must be integers");
10+
let data = $crate::input::get_input($year, day).expect("could not fetch input");
11+
let input = $crate::bench_gen!($day, &data, $gen);
12+
13+
let mut group = $criterion.benchmark_group(stringify!($day));
14+
$( $crate::bench_sol!(&mut group, $day, &input, $sol); )+
15+
group.finish();
16+
}}
17+
}
18+
19+
// This is just a silent version of run_gen with more agressive exceptions.
20+
#[macro_export]
21+
macro_rules! bench_gen {
22+
( $day: ident, $data: expr, { gen_default } ) => {{
23+
$data
24+
}};
25+
( $day: ident, $data: expr, { gen $generator: ident } ) => {{
26+
$day::$generator($data)
27+
}};
28+
( $day: ident, $data: expr, { gen_fallible $generator: ident } ) => {{
29+
use std::fmt::*;
30+
$day::$generator($data).expect("failed to parse input")
31+
}};
32+
}
33+
34+
#[macro_export]
35+
macro_rules! bench_sol {
36+
( $group: expr, $day: ident, $input: expr, { $kind: tt $solution: ident } ) => {{
37+
$group.bench_function(stringify!($solution), |b| {
38+
b.iter(|| $day::$solution($input))
39+
});
40+
}};
41+
}
File renamed without changes.

src/parse.rs renamed to src/parse/mod.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Macro used to parse input tokens.
22
3+
mod gen_bench;
4+
mod gen_run;
5+
36
// Call `apply` macro on this generated form of token tree;
47
// $ctx, { day DAY { { gen GENERATOR } { { sol SOLUTION } { sol SOLUTION } } } }
58
#[macro_export]
@@ -9,23 +12,32 @@ macro_rules! parse {
912
day $apply: ident, $ctx: tt, $val: expr;
1013
$day: ident => $( $tail: tt )*
1114
) => {
12-
$crate::parse!( sol $apply, $ctx, $val; { day $day { { gen_default } { } } }; $( $tail )* )
15+
$crate::parse!(
16+
sol $apply, $ctx, $val;
17+
{ day $day { { gen_default } { } } }; $( $tail )*
18+
)
1319
};
1420

1521
// Read day: regular generator
1622
(
1723
day $apply: ident, $ctx: tt, $val: expr;
1824
$day: ident : $generator: ident => $( $tail: tt )*
1925
) => {
20-
$crate::parse!( sol $apply, $ctx, $val; { day $day { { gen $generator } { } } }; $( $tail )* )
26+
$crate::parse!(
27+
sol $apply, $ctx, $val;
28+
{ day $day { { gen $generator } { } } }; $( $tail )*
29+
)
2130
};
2231

2332
// Read day: fallible generator
2433
(
2534
day $apply: ident, $ctx: tt, $val: expr;
2635
$day: ident : $generator: ident ? => $( $tail: tt )*
2736
) => {
28-
$crate::parse!( sol $apply, $ctx, $val; { day $day { { gen_fallible $generator } { } } }; $( $tail )* )
37+
$crate::parse!(
38+
sol $apply, $ctx, $val;
39+
{ day $day { { gen_fallible $generator } { } } }; $( $tail )*
40+
)
2941
};
3042

3143
// Empty rules
@@ -39,8 +51,7 @@ macro_rules! parse {
3951
) => {
4052
$crate::parse!(
4153
post_sol $apply, $ctx, $val;
42-
{ day $day { $gen { $( $acc )* { sol_fallible $sol } } } };
43-
$( $tail )*
54+
{ day $day { $gen { $( $acc )* { sol_fallible $sol } } } }; $( $tail )*
4455
)
4556
};
4657

@@ -52,8 +63,7 @@ macro_rules! parse {
5263
) => {
5364
$crate::parse!(
5465
post_sol $apply, $ctx, $val;
55-
{ day $day { $gen { $( $acc )* { sol $sol } } } };
56-
$( $tail )*
66+
{ day $day { $gen { $( $acc )* { sol $sol } } } }; $( $tail )*
5767
)
5868
};
5969

@@ -62,7 +72,7 @@ macro_rules! parse {
6272
post_sol $apply: ident, $ctx: tt, $val: expr;
6373
$curr: tt ; , $( $tail: tt )*
6474
) => {
65-
$crate::parse!( sol $apply, $ctx, $val; $curr; $( $tail )* )
75+
$crate::parse!(sol $apply, $ctx, $val; $curr; $( $tail )* )
6676
};
6777

6878
// After solution: end of day

0 commit comments

Comments
 (0)