Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ path = "src/main.rs"

[dependencies]
chrono = "0.4.38"
diff = "0.1.13"
itoa = "1.0.11"
regex = "1.10.4"
same-file = "1.0.6"
similar = { version = "3.2.1", default-features = false }
unicode-width = "0.2.0"

[dev-dependencies]
Expand Down
79 changes: 58 additions & 21 deletions benches/bench-diffutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,43 @@ mod diffutils_cmp {

#[cfg(not(feature = "feat_bench_not_diff"))]
mod diffutils_diff {
// use std::hint::black_box;
use std::hint::black_box;

use crate::{binary, prepare::*, FILE_SIZE_KILO_BYTES};
// use diffutilslib::params;
use diffutilslib::{normal_diff, params};
use divan::Bencher;

// bench the actual compare
// TODO diff does not have a diff function
// #[divan::bench(args = [100_000,10_000])]
// fn diff_compare_files(bencher: Bencher, bytes: u64) {
// let (from, to) = gen_testfiles(lines, 0, "id");
// let cmd = format!("cmp {from} {to}");
// let opts = str_to_options(&cmd).into_iter().peekable();
// let params = params::parse_params(opts).unwrap();
//
// bencher
// // .with_inputs(|| prepare::cmp_params_identical_testfiles(lines))
// .with_inputs(|| params.clone())
// .bench_refs(|params| diff::diff(&params).unwrap());
// }

// bench original GNU diff
// bench the actual diff comparison in-process on identical files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn diff_compare_files_equal(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_equal(bytes);
let from_content = std::fs::read(from).expect("Failed to read from file");
let to_content = std::fs::read(to).expect("Failed to read to file");
let cmd = format!("diff {from} {to}");
let opts = str_to_options(&cmd).into_iter().peekable();
let (params, _) = params::parse_params(opts).unwrap();

bencher
.with_inputs(|| (&from_content, &to_content, &params))
.bench_refs(|(from, to, params)| black_box(normal_diff(from, to, params)));
}

// bench the actual diff comparison in-process on differing files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn diff_compare_files_different(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_different(bytes);
let from_content = std::fs::read(from).expect("Failed to read from file");
let to_content = std::fs::read(to).expect("Failed to read to file");
let cmd = format!("diff {from} {to}");
let opts = str_to_options(&cmd).into_iter().peekable();
let (params, _) = params::parse_params(opts).unwrap();

bencher
.with_inputs(|| (&from_content, &to_content, &params))
.bench_refs(|(from, to, params)| black_box(normal_diff(from, to, params)));
}

// bench original GNU diff on equal files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn cmd_diff_gnu_equal(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_equal(bytes);
Expand All @@ -113,7 +128,7 @@ mod diffutils_diff {
.bench_refs(|cmd_args| binary::bench_binary("diff", cmd_args));
}

// bench the compiled release version
// bench the compiled release version on equal files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn cmd_diff_release_equal(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_equal(bytes);
Expand All @@ -124,6 +139,27 @@ mod diffutils_diff {
.with_inputs(|| args_str.clone())
.bench_refs(|cmd_args| binary::bench_binary("target/release/diffutils", cmd_args));
}

// bench original GNU diff on differing files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn cmd_diff_gnu_different(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_different(bytes);
let args_str = format!("{from} {to}");
bencher
.with_inputs(|| args_str.clone())
.bench_refs(|cmd_args| binary::bench_binary("diff", cmd_args));
}

// bench the compiled release version on differing files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn cmd_diff_release_different(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_different(bytes);
let args_str = format!("diff {from} {to}");

bencher
.with_inputs(|| args_str.clone())
.bench_refs(|cmd_args| binary::bench_binary("target/release/diffutils", cmd_args));
}
}

mod parser {
Expand Down Expand Up @@ -204,7 +240,6 @@ mod prepare {
&self.files_equal[p]
}

#[allow(unused)]
pub fn get_test_files_different(&self, kb: u64) -> &(String, String) {
let p = FILE_SIZE_KILO_BYTES.iter().position(|f| *f == kb).unwrap();
&self.files_different[p]
Expand Down Expand Up @@ -358,14 +393,16 @@ mod prepare {
}

mod binary {
use std::process::Command;
use std::process::{Command, Stdio};

use crate::prepare::str_to_options;

pub fn bench_binary(program: &str, cmd_args: &str) -> std::process::ExitStatus {
let args = str_to_options(cmd_args);
Command::new(program)
.args(args)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.expect("Failed to execute binary")
}
Expand Down
9 changes: 5 additions & 4 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::collections::VecDeque;
use std::io::Write;

use crate::diff_engine::{diff_slices, Change};
use crate::params::Params;
use crate::utils::do_write_line;
use crate::utils::{
Expand Down Expand Up @@ -129,9 +130,9 @@ fn make_diff(

let mut block: Option<ChangeBlock> = None;

for result in diff::slice(&expected_lines, &actual_lines) {
for result in diff_slices(&expected_lines, &actual_lines) {
match result {
diff::Result::Left(str) => {
Change::Left(str) => {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
Expand All @@ -153,7 +154,7 @@ fn make_diff(
line_number_expected += 1;
lines_since_mismatch = 0;
}
diff::Result::Right(str) => {
Change::Right(str) => {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
Expand All @@ -175,7 +176,7 @@ fn make_diff(
line_number_actual += 1;
lines_since_mismatch = 0;
}
diff::Result::Both(str, _) => {
Change::Both(str, _) => {
// if one of them is missing a newline and the other isn't, then they don't actually match
if (line_number_actual > actual_lines_count)
&& (line_number_expected > expected_lines_count)
Expand Down
Loading
Loading