From bf006579115ec47b8d74a5fe7b97dcffecfb3fff Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Thu, 12 Dec 2024 14:01:04 -0800 Subject: [PATCH] multiple: Use FxHashMap for s p e e d --- Cargo.lock | 7 +++++++ Cargo.toml | 3 ++- src/day1.rs | 6 ++++-- src/day11.rs | 6 +++--- utils/grid/Cargo.lock | 7 +++++++ 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 utils/grid/Cargo.lock diff --git a/Cargo.lock b/Cargo.lock index ea5800a..4248dbf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,7 @@ dependencies = [ "itertools", "rayon", "regex", + "rustc-hash", "thread_local", ] @@ -195,6 +196,12 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "rustc-hash" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" + [[package]] name = "ryu" version = "1.0.18" diff --git a/Cargo.toml b/Cargo.toml index 51b9ee9..89dc1e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,8 @@ grid = { version = "0.1.0", path = "utils/grid" } itertools = "0.13.0" rayon = "1.10.0" regex = "1.11.1" +rustc-hash = "2.1.0" thread_local = "1.1.8" [profile.release] -lto = true \ No newline at end of file +lto = true diff --git a/src/day1.rs b/src/day1.rs index bd99662..b1aaa5b 100644 --- a/src/day1.rs +++ b/src/day1.rs @@ -1,7 +1,9 @@ use aoc_runner_derive::{aoc, aoc_generator}; -use std::collections::HashMap; +use rustc_hash::FxHashMap; use std::io::{BufRead, Lines}; +type HashMap = FxHashMap; + #[aoc_generator(day1)] pub fn get_input(input: &[u8]) -> Locations { Locations::from(input.lines()) @@ -32,7 +34,7 @@ impl Locations { self.right.sort(); } fn right_count(&self) -> HashMap { - let mut right_count: HashMap = HashMap::new(); + let mut right_count: HashMap = HashMap::default(); for rval in &self.right { right_count.insert(*rval, *right_count.get(rval).unwrap_or(&0) + 1); } diff --git a/src/day11.rs b/src/day11.rs index 77c6a7c..04426e8 100644 --- a/src/day11.rs +++ b/src/day11.rs @@ -1,10 +1,10 @@ use aoc_runner_derive::aoc; use itertools::Itertools; -use std::collections::HashMap; +use rustc_hash::FxHashMap; use std::iter::repeat; type IntType = u64; -type CacheType = HashMap; +type CacheType = FxHashMap; #[derive(Clone, Debug, Hash, PartialEq, Eq)] struct Stone(IntType); @@ -66,7 +66,7 @@ fn count_blinks(stone: &Stone, blink: usize, cache: &mut Vec) -> IntT } fn blink_stones(stones: Stones, blinks: usize) -> IntType { - let mut cache = Vec::from_iter(repeat(CacheType::new()).take(blinks)); + let mut cache = Vec::from_iter(repeat(CacheType::default()).take(blinks)); stones .0 .iter() diff --git a/utils/grid/Cargo.lock b/utils/grid/Cargo.lock new file mode 100644 index 0000000..1c1a197 --- /dev/null +++ b/utils/grid/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "grid" +version = "0.1.0"