feat: measure buffering

This commit is contained in:
2025-12-02 21:35:02 +01:00
parent 23c52cab0e
commit fbf76caa20
4 changed files with 33 additions and 17 deletions

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
*demo*.txt *demo*.txt
input.txt input.txt
*-large.txt *-large.txt
2025-*.txt
# binaries # binaries
main main

View File

@@ -3,7 +3,6 @@ fn challenge_usize(b: &[u8]) -> usize {
let mut total = 0; let mut total = 0;
let s = unsafe { str::from_utf8_unchecked(b) }; let s = unsafe { str::from_utf8_unchecked(b) };
dbg!(is_repeating(b"11"));
let ranges = s.trim_end().split(','); let ranges = s.trim_end().split(',');
for range in ranges { for range in ranges {
let (start, end) = range.split_once('-').unwrap(); let (start, end) = range.split_once('-').unwrap();
@@ -11,14 +10,15 @@ fn challenge_usize(b: &[u8]) -> usize {
continue; continue;
} }
let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap()); let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap());
println!("{start}-{end}"); // println!("{start}-{end}");
for id in 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; total += id;
println!("-->> invalid {id_s}"); // println!("-->> invalid {id_s}");
} }
} }
} }

View File

@@ -3,7 +3,6 @@ fn challenge_usize(b: &[u8]) -> usize {
let mut total = 0; let mut total = 0;
let s = unsafe { str::from_utf8_unchecked(b) }; let s = unsafe { str::from_utf8_unchecked(b) };
dbg!(is_repeating(b"11"));
let ranges = s.trim_end().split(','); let ranges = s.trim_end().split(',');
for range in ranges { for range in ranges {
let (start, end) = range.split_once('-').unwrap(); let (start, end) = range.split_once('-').unwrap();
@@ -11,14 +10,14 @@ fn challenge_usize(b: &[u8]) -> usize {
continue; continue;
} }
let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap()); let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap());
println!("{start}-{end}"); // println!("{start}-{end}");
for id in start..=end { for id in start..=end {
let id_s = id.to_string(); let id_s = id.to_string();
if is_repeating(id_s.as_bytes()) { if is_repeating(id_s.as_bytes()) {
total += id; total += id;
println!("-->> invalid {id_s}"); // println!("-->> invalid {id_s}");
} }
} }
} }

View File

@@ -31,11 +31,7 @@ pub mod dl {
/// To get the error call [`ffi::error()`]. /// To get the error call [`ffi::error()`].
pub fn try_drop(self) -> Result<(), (Self, i32)> { pub fn try_drop(self) -> Result<(), (Self, i32)> {
let err = unsafe { ffi::close(self.0) }; let err = unsafe { ffi::close(self.0) };
if err == 0 { if err == 0 { Ok(()) } else { Err((self, err)) }
Ok(())
} else {
Err((self, err))
}
} }
/// # Safety /// # Safety
@@ -180,8 +176,14 @@ pub mod loader {
// drop. // drop.
untry!(unsafe { handle.symfn::<C<isize>>(c"challenge_isize") }.map(V::Isize)); untry!(unsafe { handle.symfn::<C<isize>>(c"challenge_isize") }.map(V::Isize));
untry!(unsafe { handle.symfn::<C<usize>>(c"challenge_usize") }.map(V::Usize)); untry!(unsafe { handle.symfn::<C<usize>>(c"challenge_usize") }.map(V::Usize));
untry!(unsafe { handle.symfn::<C<(usize, usize)>>(c"challenge_isize_duple") }.map(V::UsizeDuple)); untry!(
untry!(unsafe { handle.symfn::<C<(isize, isize)>>(c"challenge_usize_duple") }.map(V::IsizeDuple)); unsafe { handle.symfn::<C<(usize, usize)>>(c"challenge_isize_duple") }
.map(V::UsizeDuple)
);
untry!(
unsafe { handle.symfn::<C<(isize, isize)>>(c"challenge_usize_duple") }
.map(V::IsizeDuple)
);
None 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; pub type ChallengeFn = unsafe extern "Rust" fn(&[u8]) -> isize;
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
@@ -213,8 +225,9 @@ fn main() -> io::Result<()> {
}; };
let (noop_overhead_cold, noop_overhead_hot) = measure_noop_overhead(); let (noop_overhead_cold, noop_overhead_hot) = measure_noop_overhead();
println!( info!(
"\x1b[34minf: noop fn takes {noop_overhead_hot:#?} hot and {noop_overhead_cold:#?} cold\x1b[0m", "noop fn takes {:#?} hot and {:#?} cold",
noop_overhead_hot, noop_overhead_cold
); );
let rs_path = PathBuf::from(&day); let rs_path = PathBuf::from(&day);
@@ -310,8 +323,11 @@ fn buffer_input(input_path: &str) -> io::Result<Vec<u8>> {
io::stdin().read_to_end(&mut input)?; io::stdin().read_to_end(&mut input)?;
Ok(input) Ok(input)
} else { } else {
let t = Instant::now();
let mut f = File::open(input_path)?; let mut f = File::open(input_path)?;
f.read_to_end(&mut input)?; f.read_to_end(&mut input)?;
info!("\x1b[34mbuffering input {:#?}\x1b[0m", t.elapsed());
Ok(input) Ok(input)
} }
} }