Skip to content

Commit

Permalink
perf: optimize words_needed
Browse files Browse the repository at this point in the history
  • Loading branch information
threadexio committed Aug 29, 2024
1 parent 080050c commit 2e095e5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
12 changes: 3 additions & 9 deletions channels-packet/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,10 @@ fn write_u48_to_slice(x: u48, out: &mut [u8]) {
out[..6].copy_from_slice(&x[..6]);
}

#[allow(clippy::cast_possible_truncation)]
const fn words_needed(len: u48) -> u2 {
let mut len = len.get();
let mut x = 0;

while len != 0 {
x += 1;
len >>= 16;
}

u2::new_truncate(x)
let leading_words = len.get().leading_zeros() >> 4;
u2::new_truncate((4 - leading_words) as u8)
}

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion perf/src/bin/recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ fn main() {
.config(Config::default().with_verify_order(false))
.build();

perf::run_for_default_duration(|| {
let r = perf::run_for_default_duration(|| {
#[allow(clippy::unit_arg)]
black_box(rx.recv_blocking().unwrap());
});

eprintln!("{r}");
}
4 changes: 3 additions & 1 deletion perf/src/bin/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ fn main() {
.config(Config::default())
.build();

perf::run_for_default_duration(|| {
let r = perf::run_for_default_duration(|| {
#[allow(clippy::unit_arg)]
black_box(tx.send_blocking(black_box(())).unwrap());
});

eprintln!("{r}");
}
57 changes: 48 additions & 9 deletions perf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::Duration;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Run {
runs: u64,
}

/// Run _f_ repeatedly until `x` amount of time has passed.
pub fn run_for<F>(x: Duration, mut f: F) -> Run
pub fn run_for<F>(x: Duration, mut f: F) -> Report
where
F: FnMut(),
{
Expand All @@ -25,14 +21,57 @@ where
runs += 1;
}

Run { runs }
Report { runs, dur: x }
})
}

/// Run _f_ repeatedly for 30 seconds.
pub fn run_for_default_duration<F>(f: F) -> Run
pub fn run_for_default_duration<F>(f: F) -> Report
where
F: FnMut(),
{
run_for(Duration::from_secs(30), f)
run_for(Duration::from_secs(10), f)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Report {
dur: Duration,
runs: u64,
}

impl fmt::Display for Report {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let dur = self.dur.as_secs_f64();
let runs = self.runs;

let avg_dur_per_run = dur / (runs as f64);

writeln!(
f,
r#"took {dur} for {runs} runs (avg {avg_dur_per_run} / run)"#,
dur = NormalizedTime(dur),
avg_dur_per_run = NormalizedTime(avg_dur_per_run)
)
}
}

static TIME_UNITS_SUFFIXES: &[&str] = &["s", "ms", "μs", "ns", "ps"];

struct NormalizedTime(f64);

impl fmt::Display for NormalizedTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut x = self.0;

for unit in TIME_UNITS_SUFFIXES {
if x < 1.0 {
x *= 1000.0;
} else {
write!(f, "{x:.3}{unit}")?;
break;
}
}

Ok(())
}
}

0 comments on commit 2e095e5

Please sign in to comment.