Skip to content

Commit fac8dab

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 2e68130 commit fac8dab

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
@@ -530,6 +530,9 @@ fn format_byte(byte: u8) -> String {
530530
unsafe { String::from_utf8_unchecked(quoted) }
531531
}
532532

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

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

548+
// Capacity calc: at_byte width + 2 x 3-byte octal numbers + 4-byte value + up to 2 byte value + 4 spaces
549+
let mut output = Vec::<u8>::with_capacity(width + 3 * 2 + 4 + 2 + 4);
550+
545551
if params.print_bytes {
546552
for (at_byte, from_byte, to_byte) in diffs {
553+
output.clear();
554+
555+
// "{:>width$} {:>3o} {:4} {:>3o} {}",
547556
let at_byte_str = at_byte_buf.format(at_byte);
548-
writeln!(
549-
stdout,
550-
"{:>width$} {} {:4} {} {}",
551-
at_byte_str,
552-
format_octal(from_byte, &mut from_oct),
553-
format_byte(from_byte),
554-
format_octal(to_byte, &mut to_oct),
555-
format_byte(to_byte),
556-
)
557-
.map_err(|e| {
557+
let at_byte_padding = width - at_byte_str.len();
558+
559+
for _ in 0..at_byte_padding {
560+
output.push(b' ')
561+
}
562+
563+
output.extend_from_slice(at_byte_str.as_bytes());
564+
565+
output.push(b' ');
566+
567+
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
568+
569+
output.push(b' ');
570+
571+
let from_byte_str = format_byte(from_byte);
572+
let from_byte_padding = 4 - from_byte_str.len();
573+
574+
output.extend_from_slice(from_byte_str.as_bytes());
575+
576+
for _ in 0..from_byte_padding {
577+
output.push(b' ')
578+
}
579+
580+
output.push(b' ');
581+
582+
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
583+
584+
output.push(b' ');
585+
586+
output.extend_from_slice(format_byte(to_byte).as_bytes());
587+
588+
output.push(b'\n');
589+
590+
stdout.write_all(output.as_slice()).map_err(|e| {
558591
format!(
559592
"{}: error printing output: {e}",
560593
params.executable.to_string_lossy()
@@ -563,16 +596,29 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
563596
}
564597
} else {
565598
for (at_byte, from_byte, to_byte) in diffs {
599+
output.clear();
600+
601+
// "{:>width$} {:>3o} {:>3o}"
566602
let at_byte_str = at_byte_buf.format(at_byte);
567-
writeln!(
568-
stdout,
569-
"{:>width$} {} {}",
570-
at_byte_str,
571-
format_octal(from_byte, &mut from_oct),
572-
format_octal(to_byte, &mut to_oct),
573-
width = width
574-
)
575-
.map_err(|e| {
603+
let at_byte_padding = width - at_byte_str.len();
604+
605+
for _ in 0..at_byte_padding {
606+
output.push(b' ')
607+
}
608+
609+
output.extend_from_slice(at_byte_str.as_bytes());
610+
611+
output.push(b' ');
612+
613+
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
614+
615+
output.push(b' ');
616+
617+
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
618+
619+
output.push(b'\n');
620+
621+
stdout.write_all(output.as_slice()).map_err(|e| {
576622
format!(
577623
"{}: error printing output: {e}",
578624
params.executable.to_string_lossy()

0 commit comments

Comments
 (0)