From fbf76caa2019eadcdacc4259ff51a1fe6d8bc654 Mon Sep 17 00:00:00 2001 From: javalsai Date: Tue, 2 Dec 2025 21:35:02 +0100 Subject: [PATCH] feat: measure buffering --- .gitignore | 1 + 2025/02/p1.rs | 10 +++++----- 2025/02/p2.rs | 5 ++--- 2025/tester.rs | 34 +++++++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 917c5d6..02c2fc9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *demo*.txt input.txt *-large.txt +2025-*.txt # binaries main diff --git a/2025/02/p1.rs b/2025/02/p1.rs index 580061b..a3bdeb6 100644 --- a/2025/02/p1.rs +++ b/2025/02/p1.rs @@ -3,7 +3,6 @@ fn challenge_usize(b: &[u8]) -> usize { let mut total = 0; let s = unsafe { str::from_utf8_unchecked(b) }; - dbg!(is_repeating(b"11")); let ranges = s.trim_end().split(','); for range in ranges { let (start, end) = range.split_once('-').unwrap(); @@ -11,14 +10,15 @@ fn challenge_usize(b: &[u8]) -> usize { continue; } let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap()); - println!("{start}-{end}"); + // println!("{start}-{end}"); for id in start..=end { - let id_s = id.to_string(); + let mut id_buf = [0u8; MAX_USIZE_LEN]; + let id_s = fast_to_string(id, &mut id_buf); - if is_repeating(id_s.as_bytes()) { + if is_repeating(id_s) { total += id; - println!("-->> invalid {id_s}"); + // println!("-->> invalid {id_s}"); } } } diff --git a/2025/02/p2.rs b/2025/02/p2.rs index 389ac65..e65d26f 100644 --- a/2025/02/p2.rs +++ b/2025/02/p2.rs @@ -3,7 +3,6 @@ fn challenge_usize(b: &[u8]) -> usize { let mut total = 0; let s = unsafe { str::from_utf8_unchecked(b) }; - dbg!(is_repeating(b"11")); let ranges = s.trim_end().split(','); for range in ranges { let (start, end) = range.split_once('-').unwrap(); @@ -11,14 +10,14 @@ fn challenge_usize(b: &[u8]) -> usize { continue; } let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap()); - println!("{start}-{end}"); + // println!("{start}-{end}"); for id in start..=end { let id_s = id.to_string(); if is_repeating(id_s.as_bytes()) { total += id; - println!("-->> invalid {id_s}"); + // println!("-->> invalid {id_s}"); } } } diff --git a/2025/tester.rs b/2025/tester.rs index 229cf40..5571f3d 100644 --- a/2025/tester.rs +++ b/2025/tester.rs @@ -31,11 +31,7 @@ pub mod dl { /// To get the error call [`ffi::error()`]. pub fn try_drop(self) -> Result<(), (Self, i32)> { let err = unsafe { ffi::close(self.0) }; - if err == 0 { - Ok(()) - } else { - Err((self, err)) - } + if err == 0 { Ok(()) } else { Err((self, err)) } } /// # Safety @@ -180,8 +176,14 @@ pub mod loader { // drop. untry!(unsafe { handle.symfn::>(c"challenge_isize") }.map(V::Isize)); untry!(unsafe { handle.symfn::>(c"challenge_usize") }.map(V::Usize)); - untry!(unsafe { handle.symfn::>(c"challenge_isize_duple") }.map(V::UsizeDuple)); - untry!(unsafe { handle.symfn::>(c"challenge_usize_duple") }.map(V::IsizeDuple)); + untry!( + unsafe { handle.symfn::>(c"challenge_isize_duple") } + .map(V::UsizeDuple) + ); + untry!( + unsafe { handle.symfn::>(c"challenge_usize_duple") } + .map(V::IsizeDuple) + ); None } @@ -202,6 +204,16 @@ pub mod performer { } } +macro_rules! info { + ($fmt:literal) => { + println!(concat!("\x1b[34m inf: ", $fmt, "\x1b[0m")); + }; + + ($fmt:literal, $($arg:tt)*) => { + println!(concat!("\x1b[34m inf: ", $fmt, "\x1b[0m"), $($arg)*); + }; +} + pub type ChallengeFn = unsafe extern "Rust" fn(&[u8]) -> isize; fn main() -> io::Result<()> { @@ -213,8 +225,9 @@ fn main() -> io::Result<()> { }; let (noop_overhead_cold, noop_overhead_hot) = measure_noop_overhead(); - println!( - "\x1b[34minf: noop fn takes {noop_overhead_hot:#?} hot and {noop_overhead_cold:#?} cold\x1b[0m", + info!( + "noop fn takes {:#?} hot and {:#?} cold", + noop_overhead_hot, noop_overhead_cold ); let rs_path = PathBuf::from(&day); @@ -310,8 +323,11 @@ fn buffer_input(input_path: &str) -> io::Result> { io::stdin().read_to_end(&mut input)?; Ok(input) } else { + let t = Instant::now(); let mut f = File::open(input_path)?; f.read_to_end(&mut input)?; + info!("\x1b[34mbuffering input {:#?}\x1b[0m", t.elapsed()); + Ok(input) } }