Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a534480

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 d22282d commit a534480

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

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

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

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

0 commit comments

Comments
 (0)