Skip to content

Commit

Permalink
Another attempt at codspeed
Browse files Browse the repository at this point in the history
  • Loading branch information
ricohageman committed Dec 10, 2024
1 parent 70fcac7 commit c124d39
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 40 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CodSpeed

on:
push:
branches:
- "main"
pull_request:
# `workflow_dispatch` allows CodSpeed to trigger backtest
# performance analysis in order to generate initial data.
workflow_dispatch:

jobs:
benchmarks:
name: Run benchmarks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup rust toolchain, cache and cargo-codspeed binary
uses: moonrepo/setup-rust@v1
with:
cache-target: release
bins: cargo-codspeed, aoc-cli

- name: Download available input files
env:
ADVENT_OF_CODE_SESSION: ${{ secrets.AOC_SESSION }}
run: |
mkdir -p inputs
for i in {1..25}; do
aoc download -I -i inputs/day$i.txt --year 2024 -d $i || true
done
- name: Build the benchmark target(s)
run: cargo codspeed build

- name: Run the benchmarks
uses: CodSpeedHQ/action@v3
with:
run: cargo codspeed run
token: ${{ secrets.CODSPEED_TOKEN }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ Cargo.lock
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/

input/

12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ edition = "2021"
[lib]
bench = false

[profile.release]
debug = true

[dependencies]
aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0"
Expand All @@ -16,4 +19,11 @@ strum = "0.26.3"
strum_macros = "0.26.4"

[dev-dependencies]
criterion = { version = "2.7.2", package = "codspeed-criterion-compat" }
criterion = { version = "2.7.2", package = "codspeed-criterion-compat", default-features = false }
indoc = "2.0.5"
paste = "1.0.15"


[[bench]]
name = "bench_days"
harness = false
41 changes: 41 additions & 0 deletions benches/bench_days.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use criterion::{criterion_group, criterion_main, Criterion};
use paste::paste;

/// Get input for a single day
macro_rules! get_day_input {
($day_num:literal) => {
include_str!(concat!("../inputs/day", $day_num, ".txt"))
};
}

/// Define benchmarks for a single day with part1 and part2
macro_rules! benches_day {
($day_num:literal) => {
paste! {
use advent_of_code_2024::[<day $day_num>];

pub fn [<bench_day $day_num>](c: &mut Criterion) {
let mut group = c.benchmark_group(concat!("day", $day_num));
let input = get_day_input!($day_num);
group.bench_function(format!("day{}_part1", $day_num), |b| b.iter(|| [<day $day_num>]::part1(input)));
group.bench_function(format!("day{}_part2", $day_num), |b| b.iter(|| [<day $day_num>]::part2(input)));
}
}
};
}

/// Create benchmarks for included days
macro_rules! benches {
($($day_num:literal),*) => {
paste! {
$(
benches_day!($day_num);
)*

criterion_group!(benches, $([<bench_day $day_num>]),*);
criterion_main!(benches);
}
};
}

benches!(1, 2, 3, 4, 5, 7, 8, 9, 10);
2 changes: 0 additions & 2 deletions src/day1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub fn input_generator(input: &str) -> Input {
.unzip()
}

#[aoc(day1, part1)]
pub fn part1(input: &str) -> Output {
let (left, right) = input_generator(input);

Expand All @@ -28,7 +27,6 @@ pub fn part1(input: &str) -> Output {
.sum::<Output>()
}

#[aoc(day1, part2)]
pub fn part2(input: &str) -> Output {
let (left, right) = input_generator(input);

Expand Down
5 changes: 1 addition & 4 deletions src/day10/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use itertools::*;
use rustc_hash::FxHashSet;
use std::collections::VecDeque;
use std::fmt::Display;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

Expand Down Expand Up @@ -114,7 +113,6 @@ impl Grid {
}
}

#[aoc(day10, part1)]
pub fn part1(input: &str) -> usize {
let grid = Grid::from_input(input);

Expand All @@ -131,15 +129,14 @@ pub fn part1(input: &str) -> usize {
.sum()
}

#[aoc(day10, part2)]
pub fn part2(input: &str) -> usize {
let grid = Grid::from_input(input);

grid.trail_heads()
.map(|trail_head| {
let mut rating = 0;

grid.find_trails_starting_at(trail_head, |trail_end| {
grid.find_trails_starting_at(trail_head, |_| {
rating += 1;
});

Expand Down
5 changes: 1 addition & 4 deletions src/day2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ fn is_valid_report(report: &[u32]) -> bool {
})
}

#[aoc(day2, part1)]
pub fn part1(input: &str) -> Output {
input_generator(input)
.filter(|report| is_valid_report(report))
.count()
}

#[aoc(day2, part2)]
pub fn part2(input: &str) -> Output {
let mut workhorse = Vec::with_capacity(5);

Expand All @@ -61,14 +59,13 @@ pub fn part2(input: &str) -> Output {
workhorse.extend_from_slice(report);

let mut removed_element = workhorse.pop().unwrap();
let mut temp = removed_element;

if is_valid_report(&workhorse) {
return true;
}

for index in (0..workhorse.len()).rev() {
temp = workhorse[index];
let temp = workhorse[index];
workhorse[index] = removed_element;
removed_element = temp;

Expand Down
2 changes: 0 additions & 2 deletions src/day3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ fn parse_multiplication(input: &str) -> Output {
left.parse::<usize>().unwrap() * right.parse::<usize>().unwrap()
}

#[aoc(day3, part1)]
pub fn part1(input: &str) -> Output {
let regex = Regex::new(r"(mul\(\d*,\d*\))").unwrap();
regex
Expand All @@ -23,7 +22,6 @@ pub fn part1(input: &str) -> Output {
.sum()
}

#[aoc(day3, part2)]
pub fn part2(input: &str) -> Output {
let regex = Regex::new(r"(mul\(\d*,\d*\))|(do\(\))|(don\'t\(\))").unwrap();
let (_, sum) = regex
Expand Down
2 changes: 0 additions & 2 deletions src/day4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ impl Grid {
}
}

#[aoc(day4, part1)]
pub fn part1(input: &str) -> Output {
let grid = Grid::from_input(input);

Expand Down Expand Up @@ -151,7 +150,6 @@ pub fn part1(input: &str) -> Output {
.sum()
}

#[aoc(day4, part2)]
pub fn part2(input: &str) -> Output {
let grid = Grid::from_input(input);

Expand Down
6 changes: 0 additions & 6 deletions src/day5/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use itertools::Itertools;

type Element = u8;
type Output = u16;

Expand All @@ -24,14 +22,12 @@ impl BitSet {

pub struct BitSetIterator {
value: u128,
cur_index: u8,
}

impl BitSetIterator {
pub fn new(value: u128) -> Self {
Self {
value,
cur_index: 0,
}
}
}
Expand Down Expand Up @@ -78,7 +74,6 @@ fn parse_input(input: &str) -> ([BitSet; 100], Vec<Vec<Element>>) {
(rules, updates)
}

#[aoc(day5, part1)]
pub fn part1(input: &str) -> Output {
let (rules, updates) = parse_input(input);

Expand Down Expand Up @@ -113,7 +108,6 @@ pub fn part1(input: &str) -> Output {
.sum()
}

#[aoc(day5, part2)]
pub fn part2(input: &str) -> Output {
let (rules, mut updates) = parse_input(input);
let mut index_of_element: [Option<u8>; 100] = [None; 100];
Expand Down
3 changes: 0 additions & 3 deletions src/day7/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use itertools::Itertools;
use std::collections::VecDeque;

pub fn solve_with_operations(input: &str, allow_concatenation: bool) -> usize {
Expand Down Expand Up @@ -52,12 +51,10 @@ pub fn solve_with_operations(input: &str, allow_concatenation: bool) -> usize {
.sum()
}

#[aoc(day7, part1)]
pub fn part1(input: &str) -> usize {
solve_with_operations(input, false)
}

#[aoc(day7, part2)]
pub fn part2(input: &str) -> usize {
solve_with_operations(input, true)
}
Expand Down
2 changes: 0 additions & 2 deletions src/day8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ fn parse_input(input: &str) -> (isize, isize, HashMap<char, Vec<(isize, isize)>>
(width, height, antennas)
}

#[aoc(day8, part1)]
pub fn part1(input: &str) -> usize {
let (width, height, antennas) = parse_input(input);

Expand Down Expand Up @@ -53,7 +52,6 @@ pub fn part1(input: &str) -> usize {
antinodes.len()
}

#[aoc(day8, part2)]
pub fn part2(input: &str) -> usize {
let (width, height, antennas) = parse_input(input);

Expand Down
13 changes: 0 additions & 13 deletions src/day9/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::fmt::{Display, Formatter};

fn format_disk(disk: &[Option<usize>]) {
for element in disk {
let element = match element {
None => ".".to_string(),
Some(index) => index.to_string(),
};
print!("{}", element);
}
println!();
}

fn parse_input(input: &str) -> Disk {
let mut disk = Disk::default();
let mut empty = false;
Expand All @@ -33,7 +22,6 @@ fn parse_input(input: &str) -> Disk {
disk
}

#[aoc(day9, part1)]
pub fn part1(input: &str) -> usize {
let mut disk: Vec<Option<usize>> = Vec::new();
let mut next_id = 0;
Expand Down Expand Up @@ -159,7 +147,6 @@ impl Block {
}
}

#[aoc(day9, part2)]
pub fn part2(input: &str) -> usize {
let mut disk = parse_input(input);

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod day2;
pub mod day3;
pub mod day4;
pub mod day5;
pub mod day7;
pub mod day8;
pub mod day9;

Expand Down

0 comments on commit c124d39

Please sign in to comment.