Skip to content

Commit f748530

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 80c4485 commit f748530

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
@@ -499,6 +499,9 @@ fn format_byte(byte: u8) -> String {
499499
unsafe { String::from_utf8_unchecked(quoted) }
500500
}
501501

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

@@ -511,19 +514,49 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
511514
let mut from_oct = [0u8; 3]; // for octal conversions
512515
let mut to_oct = [0u8; 3];
513516

517+
// Capacity calc: at_byte width + 2 x 3-byte octal numbers + 4-byte value + up to 2 byte value + 4 spaces
518+
let mut output = Vec::<u8>::with_capacity(width + 3 * 2 + 4 + 2 + 4);
519+
514520
if params.print_bytes {
515521
for (at_byte, from_byte, to_byte) in diffs {
522+
output.clear();
523+
524+
// "{:>width$} {:>3o} {:4} {:>3o} {}",
516525
let at_byte_str = at_byte_buf.format(at_byte);
517-
writeln!(
518-
stdout,
519-
"{:>width$} {} {:4} {} {}",
520-
at_byte_str,
521-
format_octal(from_byte, &mut from_oct),
522-
format_byte(from_byte),
523-
format_octal(to_byte, &mut to_oct),
524-
format_byte(to_byte),
525-
)
526-
.map_err(|e| {
526+
let at_byte_padding = width - at_byte_str.len();
527+
528+
for _ in 0..at_byte_padding {
529+
output.push(b' ')
530+
}
531+
532+
output.extend_from_slice(at_byte_str.as_bytes());
533+
534+
output.push(b' ');
535+
536+
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
537+
538+
output.push(b' ');
539+
540+
let from_byte_str = format_byte(from_byte);
541+
let from_byte_padding = 4 - from_byte_str.len();
542+
543+
output.extend_from_slice(from_byte_str.as_bytes());
544+
545+
for _ in 0..from_byte_padding {
546+
output.push(b' ')
547+
}
548+
549+
output.push(b' ');
550+
551+
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
552+
553+
output.push(b' ');
554+
555+
output.extend_from_slice(format_byte(to_byte).as_bytes());
556+
557+
output.push(b'\n');
558+
559+
stdout.write_all(output.as_slice()).map_err(|e| {
527560
format!(
528561
"{}: error printing output: {e}",
529562
params.executable.to_string_lossy()
@@ -532,16 +565,29 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
532565
}
533566
} else {
534567
for (at_byte, from_byte, to_byte) in diffs {
568+
output.clear();
569+
570+
// "{:>width$} {:>3o} {:>3o}"
535571
let at_byte_str = at_byte_buf.format(at_byte);
536-
writeln!(
537-
stdout,
538-
"{:>width$} {} {}",
539-
at_byte_str,
540-
format_octal(from_byte, &mut from_oct),
541-
format_octal(to_byte, &mut to_oct),
542-
width = width
543-
)
544-
.map_err(|e| {
572+
let at_byte_padding = width - at_byte_str.len();
573+
574+
for _ in 0..at_byte_padding {
575+
output.push(b' ')
576+
}
577+
578+
output.extend_from_slice(at_byte_str.as_bytes());
579+
580+
output.push(b' ');
581+
582+
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
583+
584+
output.push(b' ');
585+
586+
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
587+
588+
output.push(b'\n');
589+
590+
stdout.write_all(output.as_slice()).map_err(|e| {
545591
format!(
546592
"{}: error printing output: {e}",
547593
params.executable.to_string_lossy()

0 commit comments

Comments
 (0)