Skip to content

Commit cfc9c3d

Browse files
committed
Faster day 6
1 parent c019274 commit cfc9c3d

File tree

7 files changed

+64
-13
lines changed

7 files changed

+64
-13
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ today = "run --quiet --release --features today -- today"
33
scaffold = "run --quiet --release -- scaffold"
44
download = "run --quiet --release -- download"
55
read = "run --quiet --release -- read"
6+
flame = "run --quiet --release --features dhat-heap -- flame"
67

78
solve = "run --quiet --release -- solve"
89
all = "run --quiet --release -- all"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ dhat-heap.json
2828
# Benchmarks
2929

3030
data/timings.json
31+
32+
flamegraph.svg

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1111

1212
| Day | Part 1 | Part 2 |
1313
| :---: | :---: | :---: |
14-
| [Day 1](./src/bin/01.rs) | `66.9µs` | `82.5µs` |
15-
| [Day 2](./src/bin/02.rs) | `128.2µs` | `224.5µs` |
16-
| [Day 3](./src/bin/03.rs) | `717.8µs` | `830.1µs` |
17-
| [Day 4](./src/bin/04.rs) | `810.2µs` | `298.7µs` |
18-
| [Day 5](./src/bin/05.rs) | `67.7µs` | `184.5µs` |
19-
| [Day 6](./src/bin/06.rs) | `71.1µs` | `24.8ms` |
20-
| [Day 7](./src/bin/07.rs) | `566.4µs` | `7.3ms` |
21-
22-
**Total: 36.15ms**
14+
| [Day 1](./src/bin/01.rs) | `31.7µs` | `39.7µs` |
15+
| [Day 2](./src/bin/02.rs) | `46.5µs` | `67.4µs` |
16+
| [Day 3](./src/bin/03.rs) | `213.3µs` | `238.4µs` |
17+
| [Day 4](./src/bin/04.rs) | `148.6µs` | `33.1µs` |
18+
| [Day 5](./src/bin/05.rs) | `65.6µs` | `181.1µs` |
19+
| [Day 6](./src/bin/06.rs) | `44.6µs` | `12.3ms` |
20+
| [Day 7](./src/bin/07.rs) | `554.7µs` | `7.1ms` |
21+
22+
**Total: 21.06ms**
2323
<!--- benchmarking table --->
2424

2525
---

src/bin/06.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use mygrid::point::Point;
55
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
66
use rustc_hash::FxHashSet;
77

8-
#[derive(Default, Clone, PartialEq, Eq, Hash)]
8+
#[derive(Default, Clone, PartialEq, Eq, Hash, Debug)]
99
struct Guard {
1010
position: Point,
1111
direction: Direction,
@@ -51,7 +51,7 @@ fn get_guard_path_positions_assuming_no_loops(
5151
position: start_pos,
5252
direction: UP,
5353
};
54-
let mut visited = FxHashSet::default();
54+
let mut visited = FxHashSet::with_capacity_and_hasher(10_000, Default::default());
5555
visited.insert(guard.position);
5656

5757
while let Some(new_guard) = guard.turn(grid) {
@@ -61,12 +61,14 @@ fn get_guard_path_positions_assuming_no_loops(
6161
visited
6262
}
6363

64+
#[inline]
6465
fn does_start_pos_loop(grid: &Grid<char>, start_pos: Point) -> bool {
6566
let mut guard = Guard {
6667
position: start_pos,
6768
direction: UP,
6869
};
69-
let mut visited = FxHashSet::default();
70+
let mut visited = FxHashSet::with_capacity_and_hasher(10_000, Default::default());
71+
//let mut visited = FnvIndexSet::<Guard, 10_000>::new();
7072
visited.insert(guard.clone());
7173

7274
while let Some(new_guard) = guard.turn(grid) {

src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use advent_of_code::template::commands::{all, download, read, scaffold, solve, time};
1+
use advent_of_code::template::commands::{all, download, flame, read, scaffold, solve, time};
22
use args::{parse, AppArguments};
33

44
#[cfg(feature = "today")]
@@ -28,6 +28,11 @@ mod args {
2828
dhat: bool,
2929
submit: Option<u8>,
3030
},
31+
Flame {
32+
day: Day,
33+
release: bool,
34+
dhat: bool,
35+
},
3136
All {
3237
release: bool,
3338
},
@@ -68,6 +73,11 @@ mod args {
6873
download: args.contains("--download"),
6974
overwrite: args.contains("--overwrite"),
7075
},
76+
Some("flame") => AppArguments::Flame {
77+
day: args.free_from_str()?,
78+
release: args.contains("--release"),
79+
dhat: args.contains("--dhat"),
80+
},
7181
Some("solve") => AppArguments::Solve {
7282
day: args.free_from_str()?,
7383
release: args.contains("--release"),
@@ -122,6 +132,7 @@ fn main() {
122132
dhat,
123133
submit,
124134
} => solve::handle(day, release, dhat, submit),
135+
AppArguments::Flame { day, release, dhat } => flame::handle(day, release, dhat),
125136
#[cfg(feature = "today")]
126137
AppArguments::Today => {
127138
match Day::today() {

src/template/commands/flame.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::process::{Command, Stdio};
2+
3+
use crate::template::Day;
4+
5+
pub fn handle(day: Day, release: bool, dhat: bool) {
6+
let mut cmd_args = vec![
7+
"flamegraph".to_string(),
8+
"--bin".to_string(),
9+
day.to_string(),
10+
];
11+
12+
if dhat {
13+
cmd_args.extend([
14+
"--profile".to_string(),
15+
"dhat".to_string(),
16+
"--features".to_string(),
17+
"dhat-heap".to_string(),
18+
]);
19+
} else if release {
20+
cmd_args.push("--release".to_string());
21+
}
22+
23+
cmd_args.push("--".to_string());
24+
25+
let mut cmd = Command::new("cargo")
26+
.args(&cmd_args)
27+
.env("CARGO_PROFILE_RELEASE_DEBUG", "true")
28+
.stdout(Stdio::inherit())
29+
.stderr(Stdio::inherit())
30+
.spawn()
31+
.unwrap();
32+
33+
cmd.wait().unwrap();
34+
}

src/template/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod all;
22
pub mod download;
3+
pub mod flame;
34
pub mod read;
45
pub mod scaffold;
56
pub mod solve;

0 commit comments

Comments
 (0)