From daec76d3d40a5053435cfb269b5611146c05b0ae Mon Sep 17 00:00:00 2001 From: Xiretza Date: Thu, 9 Dec 2021 21:39:13 +0100 Subject: [PATCH] 2021 day8/rust: speed up v3 to unsafe levels, remove v4 --- 2021/day8/day8_rs/benches/unscramble.rs | 9 +--- 2021/day8/day8_rs/src/lib.rs | 1 - 2021/day8/day8_rs/src/v3.rs | 5 +- 2021/day8/day8_rs/src/v4.rs | 63 ------------------------- 2021/day8/day8_rs/tests/unscramble.rs | 11 +---- 5 files changed, 5 insertions(+), 84 deletions(-) delete mode 100644 2021/day8/day8_rs/src/v4.rs diff --git a/2021/day8/day8_rs/benches/unscramble.rs b/2021/day8/day8_rs/benches/unscramble.rs index 335a1f3..9882899 100644 --- a/2021/day8/day8_rs/benches/unscramble.rs +++ b/2021/day8/day8_rs/benches/unscramble.rs @@ -1,7 +1,7 @@ use std::{fs, path::PathBuf}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use day8_rs::{v1, v2, v3, v4}; +use day8_rs::{v1, v2, v3}; pub fn criterion_benchmark(c: &mut Criterion) { let mut group = c.benchmark_group("unscramble"); @@ -33,13 +33,6 @@ pub fn criterion_benchmark(c: &mut Criterion) { } }) }); - group.bench_function("v4", |b| { - b.iter(|| { - for line in &lines { - let _ = unsafe { v4::unscramble(black_box(line)) }; - } - }) - }); group.finish(); } diff --git a/2021/day8/day8_rs/src/lib.rs b/2021/day8/day8_rs/src/lib.rs index 8eaacaf..8a2a47a 100644 --- a/2021/day8/day8_rs/src/lib.rs +++ b/2021/day8/day8_rs/src/lib.rs @@ -10,4 +10,3 @@ pub struct LineResult { pub mod v1; pub mod v2; pub mod v3; -pub mod v4; diff --git a/2021/day8/day8_rs/src/v3.rs b/2021/day8/day8_rs/src/v3.rs index ff5adc6..65d0028 100644 --- a/2021/day8/day8_rs/src/v3.rs +++ b/2021/day8/day8_rs/src/v3.rs @@ -50,9 +50,10 @@ pub fn unscramble(line: &str) -> LineResult { break; } } - Some(c) => { + Some(c @ b'a'..=b'g') => { freq += counts[c as usize - b'a' as usize]; - } + }, + Some(_) => {} } } diff --git a/2021/day8/day8_rs/src/v4.rs b/2021/day8/day8_rs/src/v4.rs deleted file mode 100644 index 89ec18f..0000000 --- a/2021/day8/day8_rs/src/v4.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::LineResult; - -const FREQ_TABLE: [usize; 256] = { - let mut tab = [0; 256]; - tab[17] = 1; - tab[25] = 7; - tab[30] = 4; - tab[34] = 2; - tab[37] = 5; - tab[39] = 3; - tab[41] = 6; - tab[42] = 0; - tab[45] = 9; - tab[49] = 8; - tab -}; - -#[must_use] -#[inline] -pub unsafe fn unscramble(line: &str) -> LineResult { - let mut bytes = line.bytes(); - - let mut counts = [0; 7]; - loop { - match bytes.next().unwrap() { - c @ b'a'..=b'g' => { - *unsafe { counts.get_unchecked_mut(c as usize - b'a' as usize) } += 1; - } - b'|' => break, - _ => {} - } - } - - bytes.next(); - - let mut freq = 0; - let mut unique_digits = 0; - let mut number = 0; - loop { - let c = bytes.next(); - match c { - Some(b' ') | None => { - let digit = FREQ_TABLE[freq & 0xff]; - if [1, 4, 7, 8].contains(&digit) { - unique_digits += 1; - } - number = number * 10 + digit; - freq = 0; - if c.is_none() { - break; - } - } - Some(c) => { - freq += unsafe { counts.get_unchecked(c as usize - b'a' as usize) }; - } - } - } - - LineResult { - unique_digits, - number, - } -} diff --git a/2021/day8/day8_rs/tests/unscramble.rs b/2021/day8/day8_rs/tests/unscramble.rs index 5688005..1615bb8 100644 --- a/2021/day8/day8_rs/tests/unscramble.rs +++ b/2021/day8/day8_rs/tests/unscramble.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use day8_rs::{v1, v2, v3, v4, LineResult}; +use day8_rs::{v1, v2, v3, LineResult}; fn test_unscramble_with_input(f: fn(&str) -> LineResult, input: &[&str]) -> LineResult { let mut result = LineResult { @@ -70,12 +70,3 @@ pub fn test_unscramble_v2() { pub fn test_unscramble_v3() { test_unscramble(v3::unscramble) } - -#[test] -pub fn test_unscramble_v4() { - fn safe_v4(input: &str) -> LineResult { - unsafe { v4::unscramble(input) } - } - - test_unscramble(safe_v4) -}