Skip to content

Commit 082cc4d

Browse files
committed
cmp: completely avoid Rust fmt in verbose mode
This makes the code less readable, but gets us a massive improvement to performance. Comparing ~36M completely different files now takes ~40% of the time. Compared to GNU cmp, we now run the same comparison in ~26% of the time. This also improves comparing binary files. A comparison of chromium and libxul now takes ~60% of the time. We also beat GNU cmpi by about the same margin. Before: > hyperfine --warmup 1 -i --output=pipe \ '../target/release/diffutils cmp -l huge huge.3' Benchmark 1: ../target/release/diffutils cmp -l huge huge.3 Time (mean ± σ): 2.000 s ± 0.016 s [User: 1.603 s, System: 0.392 s] Range (min … max): 1.989 s … 2.043 s 10 runs Warning: Ignoring non-zero exit code. > hyperfine --warmup 1 -i --output=pipe \ '../target/release/diffutils cmp -l -b \ /usr/lib64/chromium-browser/chromium-browser \ /usr/lib64/firefox/libxul.so' Benchmark 1: ../target/release/diffutils cmp -l -b /usr/lib64/chromium-browser/chromium-browser /usr/lib64/firefox/libxul.so Time (mean ± σ): 24.704 s ± 0.162 s [User: 21.948 s, System: 2.700 s] Range (min … max): 24.359 s … 24.889 s 10 runs Warning: Ignoring non-zero exit code. After: > hyperfine --warmup 1 -i --output=pipe \ '../target/release/diffutils cmp -l huge huge.3' Benchmark 1: ../target/release/diffutils cmp -l huge huge.3 Time (mean ± σ): 849.5 ms ± 6.2 ms [User: 538.3 ms, System: 306.8 ms] Range (min … max): 839.4 ms … 857.7 ms 10 runs Warning: Ignoring non-zero exit code. > hyperfine --warmup 1 -i --output=pipe \ '../target/release/diffutils cmp -l -b \ /usr/lib64/chromium-browser/chromium-browser \ /usr/lib64/firefox/libxul.so' Benchmark 1: ../target/release/diffutils cmp -l -b /usr/lib64/chromium-browser/chromium-browser /usr/lib64/firefox/libxul.so Time (mean ± σ): 14.646 s ± 0.040 s [User: 12.328 s, System: 2.286 s] Range (min … max): 14.585 s … 14.702 s 10 runs Warning: Ignoring non-zero exit code.
1 parent 9b23fb6 commit 082cc4d

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed

src/cmp.rs

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ fn format_byte(byte: u8) -> String {
533533
unsafe { String::from_utf8_unchecked(quoted) }
534534
}
535535

536+
// This function has been optimized to not use the Rust fmt system, which
537+
// leads to a massive speed up when processing large files: cuts the time
538+
// for comparing 2 ~36MB completely different files in half on an M1 Max.
536539
fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<(), String> {
537540
assert!(!params.quiet);
538541

@@ -545,19 +548,49 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
545548
let mut from_oct = [0u8; 3]; // for octal conversions
546549
let mut to_oct = [0u8; 3];
547550

551+
// Capacity calc: at_byte width + 2 x 3-byte octal numbers + 4-byte value + up to 2 byte value + 4 spaces
552+
let mut output = Vec::<u8>::with_capacity(width + 3 * 2 + 4 + 2 + 4);
553+
548554
if params.print_bytes {
549555
for (at_byte, from_byte, to_byte) in diffs {
556+
output.clear();
557+
558+
// "{:>width$} {:>3o} {:4} {:>3o} {}",
550559
let at_byte_str = at_byte_buf.format(at_byte);
551-
writeln!(
552-
stdout,
553-
"{:>width$} {} {:4} {} {}",
554-
at_byte_str,
555-
format_octal(from_byte, &mut from_oct),
556-
format_byte(from_byte),
557-
format_octal(to_byte, &mut to_oct),
558-
format_byte(to_byte),
559-
)
560-
.map_err(|e| {
560+
let at_byte_padding = width - at_byte_str.len();
561+
562+
for _ in 0..at_byte_padding {
563+
output.push(b' ')
564+
}
565+
566+
output.extend_from_slice(at_byte_str.as_bytes());
567+
568+
output.push(b' ');
569+
570+
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
571+
572+
output.push(b' ');
573+
574+
let from_byte_str = format_byte(from_byte);
575+
let from_byte_padding = 4 - from_byte_str.len();
576+
577+
output.extend_from_slice(from_byte_str.as_bytes());
578+
579+
for _ in 0..from_byte_padding {
580+
output.push(b' ')
581+
}
582+
583+
output.push(b' ');
584+
585+
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
586+
587+
output.push(b' ');
588+
589+
output.extend_from_slice(format_byte(to_byte).as_bytes());
590+
591+
output.push(b'\n');
592+
593+
stdout.write_all(output.as_slice()).map_err(|e| {
561594
format!(
562595
"{}: error printing output: {e}",
563596
params.executable.to_string_lossy()
@@ -566,16 +599,29 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
566599
}
567600
} else {
568601
for (at_byte, from_byte, to_byte) in diffs {
602+
output.clear();
603+
604+
// "{:>width$} {:>3o} {:>3o}"
569605
let at_byte_str = at_byte_buf.format(at_byte);
570-
writeln!(
571-
stdout,
572-
"{:>width$} {} {}",
573-
at_byte_str,
574-
format_octal(from_byte, &mut from_oct),
575-
format_octal(to_byte, &mut to_oct),
576-
width = width
577-
)
578-
.map_err(|e| {
606+
let at_byte_padding = width - at_byte_str.len();
607+
608+
for _ in 0..at_byte_padding {
609+
output.push(b' ')
610+
}
611+
612+
output.extend_from_slice(at_byte_str.as_bytes());
613+
614+
output.push(b' ');
615+
616+
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
617+
618+
output.push(b' ');
619+
620+
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
621+
622+
output.push(b'\n');
623+
624+
stdout.write_all(output.as_slice()).map_err(|e| {
579625
format!(
580626
"{}: error printing output: {e}",
581627
params.executable.to_string_lossy()

0 commit comments

Comments
 (0)