Skip to content

Commit b058654

Browse files
authored
Put performance profiling for bidiff in optional feature (#12)
1 parent ce97add commit b058654

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

crates/bidiff/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repository = "https://github.com/divvun/bidiff"
1111
default = ["enc"]
1212
enc = ["byteorder", "integer-encoding"]
1313
instructions = []
14+
profiling = []
1415

1516
[dependencies]
1617
# for enc

crates/bidiff/src/lib.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ use std::{
66
cmp::min,
77
error::Error,
88
io::{self, Write},
9-
time::Instant,
109
};
1110

11+
#[cfg(feature = "profiling")]
12+
use std::time::Instant;
13+
1214
#[cfg(feature = "enc")]
1315
pub mod enc;
1416

@@ -347,14 +349,20 @@ where
347349
F: FnMut(Match) -> Result<(), E>,
348350
{
349351
info!("building suffix array...");
352+
#[cfg(feature = "profiling")]
350353
let before_suffix = Instant::now();
354+
351355
let sa = PartitionedSuffixArray::new(obuf, params.sort_partitions, divsufsort::sort);
356+
357+
#[cfg(feature = "profiling")]
352358
info!(
353359
"sorting took {}",
354360
DurationSpeed(obuf.len() as u64, before_suffix.elapsed())
355361
);
356362

363+
#[cfg(feature = "profiling")]
357364
let before_scan = Instant::now();
365+
358366
if let Some(chunk_size) = params.scan_chunk_size {
359367
// +1 to make sure we don't have > num_partitions
360368
let num_chunks = (nbuf.len() + chunk_size - 1) / chunk_size;
@@ -396,6 +404,7 @@ where
396404
}
397405
}
398406

407+
#[cfg(feature = "profiling")]
399408
info!(
400409
"scanning took {}",
401410
DurationSpeed(obuf.len() as u64, before_scan.elapsed())
@@ -404,43 +413,49 @@ where
404413
Ok(())
405414
}
406415

407-
use std::fmt;
416+
#[cfg(feature = "profiling")]
417+
mod profiling {
418+
use std::fmt;
408419

409-
struct DurationSpeed(u64, std::time::Duration);
420+
pub struct DurationSpeed(pub u64, pub std::time::Duration);
410421

411-
impl fmt::Display for DurationSpeed {
412-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
413-
let (size, duration) = (self.0, self.1);
414-
write!(f, "{:?} ({})", duration, Speed(size, duration))
422+
impl fmt::Display for DurationSpeed {
423+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
424+
let (size, duration) = (self.0, self.1);
425+
write!(f, "{:?} ({})", duration, Speed(size, duration))
426+
}
415427
}
416-
}
417428

418-
struct Speed(u64, std::time::Duration);
429+
pub struct Speed(u64, std::time::Duration);
419430

420-
impl fmt::Display for Speed {
421-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
422-
let (size, duration) = (self.0, self.1);
423-
let per_sec = size as f64 / duration.as_secs_f64();
424-
write!(f, "{} / s", Size(per_sec as u64))
431+
impl fmt::Display for Speed {
432+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
433+
let (size, duration) = (self.0, self.1);
434+
let per_sec = size as f64 / duration.as_secs_f64();
435+
write!(f, "{} / s", Size(per_sec as u64))
436+
}
425437
}
426-
}
427438

428-
struct Size(u64);
439+
pub struct Size(u64);
429440

430-
impl fmt::Display for Size {
431-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
432-
let x = self.0;
441+
impl fmt::Display for Size {
442+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
443+
let x = self.0;
433444

434-
if x > 1024 * 1024 {
435-
write!(f, "{:.2} MiB", x as f64 / (1024.0 * 1024.0))
436-
} else if x > 1024 {
437-
write!(f, "{:.1} KiB", x as f64 / (1024.0))
438-
} else {
439-
write!(f, "{} B", x)
445+
if x > 1024 * 1024 {
446+
write!(f, "{:.2} MiB", x as f64 / (1024.0 * 1024.0))
447+
} else if x > 1024 {
448+
write!(f, "{:.1} KiB", x as f64 / (1024.0))
449+
} else {
450+
write!(f, "{} B", x)
451+
}
440452
}
441453
}
442454
}
443455

456+
#[cfg(feature = "profiling")]
457+
use profiling::DurationSpeed;
458+
444459
#[cfg(feature = "enc")]
445460
pub fn simple_diff(older: &[u8], newer: &[u8], out: &mut dyn Write) -> Result<(), io::Error> {
446461
simple_diff_with_params(older, newer, out, &Default::default())

0 commit comments

Comments
 (0)