perf: d02, careful multithreading for my arch

This commit is contained in:
2025-12-04 08:50:32 +01:00
parent 30fbeac41f
commit 8262f759bf
2 changed files with 36 additions and 36 deletions

BIN
2025/03/sped Executable file

Binary file not shown.

72
2025/03/sped.rs Executable file → Normal file
View File

@@ -1,3 +1,4 @@
#![feature(mpmc_channel)]
/* /*
rustc --edition 2024 sped.rs -O -Copt-level=3 -Cstrip=symbols -Cdebuginfo=0 -Cdebug-assertions=off -Coverflow-checks=false -Cpanic=abort -Ctarget-cpu=native -Ccodegen-units=1 rustc --edition 2024 sped.rs -O -Copt-level=3 -Cstrip=symbols -Cdebuginfo=0 -Cdebug-assertions=off -Coverflow-checks=false -Cpanic=abort -Ctarget-cpu=native -Ccodegen-units=1
*/ */
@@ -5,11 +6,11 @@ rustc --edition 2024 sped.rs -O -Copt-level=3 -Cstrip=symbols -Cdebuginfo=0 -Cde
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
fs::File, fs::File,
io::Read, io::{BufRead, BufReader},
mem::MaybeUninit, mem::MaybeUninit,
sync::{ sync::{
Mutex,
atomic::{AtomicUsize, Ordering::Relaxed}, atomic::{AtomicUsize, Ordering::Relaxed},
mpmc::channel,
}, },
thread, thread,
time::Instant, time::Instant,
@@ -22,52 +23,51 @@ fn main() {
let total_joltage1 = AtomicUsize::new(0); let total_joltage1 = AtomicUsize::new(0);
let total_joltage2 = AtomicUsize::new(0); let total_joltage2 = AtomicUsize::new(0);
let f = Mutex::new(File::open("2025-3.txt").unwrap()); let f = File::open("2025-3.txt").unwrap();
let (tx, rx) = channel::<String>();
thread::scope(|s| { thread::scope(|s| {
let c = || { let c = || {
#[allow(clippy::uninit_assumed_init, invalid_value)] for ln in rx.iter() {
let mut lnbuf: [u8; 50100] = unsafe { MaybeUninit::uninit().assume_init() }; let bats = ln.as_bytes();
while { #[allow(clippy::uninit_assumed_init, invalid_value)]
let mut flock = f.lock().unwrap(); let mut digs: [u8; DIGS] = unsafe { MaybeUninit::uninit().assume_init() };
let flock_read = flock.read_exact(&mut lnbuf); digs.iter_mut()
flock_read.is_ok() .enumerate()
} { .fold(0, |max_left, (i, dig): (_, &mut u8)| {
for buf in lnbuf.chunks_exact(501) { let max_bat = unsafe {
let bats = &buf[0..=500]; bats[max_left..(bats.len() - DIGS + i + 1)]
.iter()
.enumerate()
.max_by(|a, b| cmp_with_eq_as_gt(a.1, b.1))
.unwrap_unchecked()
};
#[allow(clippy::uninit_assumed_init, invalid_value)] *dig = *max_bat.1;
let mut digs: [u8; DIGS] = unsafe { MaybeUninit::uninit().assume_init() }; max_left + max_bat.0 + 1
digs.iter_mut() });
.enumerate()
.fold(0, |max_left, (i, dig): (_, &mut u8)| {
let max_bat = unsafe {
bats[max_left..(bats.len() - DIGS + i + 1)]
.iter()
.enumerate()
.max_by(|a, b| cmp_with_eq_as_gt(a.1, b.1))
.unwrap_unchecked()
};
*dig = *max_bat.1; let n = digs
max_left + max_bat.0 + 1 .iter()
}); .map(|d| d - b'0')
.fold(0_usize, |left, r| left * 10 + Into::<usize>::into(r));
let n = digs total_joltage1.fetch_add(p1(digs), Relaxed);
.iter() total_joltage2.fetch_add(n, Relaxed);
.map(|d| d - b'0')
.fold(0_usize, |left, r| left * 10 + Into::<usize>::into(r));
total_joltage1.fetch_add(p1(digs), Relaxed);
total_joltage2.fetch_add(n, Relaxed);
}
} }
}; };
for _ in 0..16 { // carefully optimally choosen for my pc by manual binary search
for _ in 0..4 {
s.spawn(c); s.spawn(c);
} }
for ln in BufReader::new(f).lines() {
tx.send(ln.unwrap()).unwrap();
}
drop(tx);
}); });
println!( println!(