Skip to content

Commit 71e63f3

Browse files
author
Gunter Schmidt
committed
Feat: cmp with faster mmap compare
1 parent 46f0713 commit 71e63f3

7 files changed

Lines changed: 200 additions & 59 deletions

File tree

.vscode/cspell.dictionaries/jargon.wordlist.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# * diffutils project
2-
2+
mmap
3+
memmap
34

45
# *** the following part is a copy of coreutils ***
56
AFAICT

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ license = "MIT"
9191
readme = "README.package.md"
9292
version = "0.5.1"
9393

94-
9594
[workspace.dependencies]
9695
assert_cmd = "2.2.0"
9796
bytecount = "0.6.9"
@@ -104,6 +103,7 @@ divan = { version = "4.3.0", package = "codspeed-divan-compat" }
104103
itertools = "0.14.0"
105104
itoa = "1.0.18"
106105
libc = "0.2.183"
106+
memmap2 = "0.9.10"
107107
nix = { version = "0.31.2", default-features = false }
108108
num-traits = "0.2.19"
109109
phf = "0.13.1"

src/uu/cmp/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ path = "src/main.rs"
2525
path = "src/cmp.rs"
2626

2727
[features]
28-
default = ["feat_run_binary_bench" ]
28+
default = ["feat_fast_mmap2_compare","feat_run_binary_bench"]
29+
# default = ["feat_run_binary_bench"]
30+
# default = ["feat_run_binary_bench"]
2931
# The cmd benchmarks start the binaries and take a lot of runtime on the github checks.
3032
# Only run them locally.
3133
feat_run_binary_bench = []
34+
feat_fast_mmap2_compare = []
3235

3336

3437
[dependencies]
@@ -37,6 +40,7 @@ clap.workspace = true
3740
diff_crate.workspace = true
3841
fluent.workspace = true
3942
itoa.workspace = true
43+
memmap2.workspace = true
4044
regex.workspace = true
4145
same-file.workspace = true
4246
uucore = { workspace = true, features = [

src/uu/cmp/benches/cmp_bench.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
//! File generation up to 1 GB is really fast, Benchmarking above 100 MB takes very long.
1212
1313
/// Generate test files with these sizes in KB.
14-
const FILE_SIZES_IN_KILO_BYTES: [u64; 4] = [100, 1 * MB, 10 * MB, 25 * MB];
14+
const FILE_SIZES_IN_KILO_BYTES: [u64; 5] = [100, 1 * MB, 10 * MB, 25 * MB, 100 * MB];
1515
const NUM_DIFF: u64 = 4;
1616
// Empty String to use TempDir (files will be removed after test) or specify dir to keep generated files
1717
const TEMP_DIR: &str = "";
18+
// const TEMP_DIR: &str = "/home/gunnar/Downloads/tmp";
1819
// just for FILE_SIZE_KILO_BYTES
1920
const MB: u64 = 1_000;
2021

@@ -60,11 +61,30 @@ fn cmp_compare_files_equal(bencher: Bencher, kb: u64) {
6061
let fp = get_context().get_files_equal_kb(kb).unwrap();
6162
let cmd = format!("cmp {} {}", fp.from, fp.to);
6263
let args = str_to_args(&cmd).into_iter();
64+
let matches =
65+
uudiff::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 2).unwrap();
66+
let params: Params = matches.try_into().unwrap();
6367

6468
bencher
6569
// .with_inputs(|| prepare::cmp_params_identical_testfiles(lines))
66-
.with_inputs(|| args.clone())
67-
.bench_refs(|params| uu_cmp::uumain(params.peekable()));
70+
.with_inputs(|| params.clone())
71+
.bench_refs(|params| uu_cmp::cmp_compare(params));
72+
}
73+
74+
// bench equal, full file read
75+
#[divan::bench(args = FILE_SIZES_IN_KILO_BYTES)]
76+
fn mmap_cmp_compare_files_equal(bencher: Bencher, kb: u64) {
77+
let fp = get_context().get_files_equal_kb(kb).unwrap();
78+
let cmd = format!("cmp {} {}", fp.from, fp.to);
79+
let args = str_to_args(&cmd).into_iter();
80+
let matches =
81+
uudiff::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 2).unwrap();
82+
let params: Params = matches.try_into().unwrap();
83+
84+
bencher
85+
// .with_inputs(|| prepare::cmp_params_identical_testfiles(lines))
86+
.with_inputs(|| params.clone())
87+
.bench_refs(|params| uu_cmp::cmp_compare_mmap(params));
6888
}
6989

7090
// bench different; cmp exits on first difference

0 commit comments

Comments
 (0)