Skip to content

Commit 16eacb1

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 52e47d6 commit 16eacb1

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

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

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

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

0 commit comments

Comments
 (0)