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

Skip to content

Commit 8847635

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 cfa7aaf commit 8847635

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
@@ -522,6 +522,9 @@ fn format_byte(byte: u8) -> String {
522522
unsafe { String::from_utf8_unchecked(quoted) }
523523
}
524524

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

@@ -534,19 +537,49 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
534537
let mut from_oct = [0u8; 3]; // for octal conversions
535538
let mut to_oct = [0u8; 3];
536539

540+
// Capacity calc: at_byte width + 2 x 3-byte octal numbers + 4-byte value + up to 2 byte value + 4 spaces
541+
let mut output = Vec::<u8>::with_capacity(width + 3 * 2 + 4 + 2 + 4);
542+
537543
if params.print_bytes {
538544
for (at_byte, from_byte, to_byte) in diffs {
545+
output.clear();
546+
547+
// "{:>width$} {:>3o} {:4} {:>3o} {}",
539548
let at_byte_str = at_byte_buf.format(at_byte);
540-
writeln!(
541-
stdout,
542-
"{:>width$} {} {:4} {} {}",
543-
at_byte_str,
544-
format_octal(from_byte, &mut from_oct),
545-
format_byte(from_byte),
546-
format_octal(to_byte, &mut to_oct),
547-
format_byte(to_byte),
548-
)
549-
.map_err(|e| {
549+
let at_byte_padding = width - at_byte_str.len();
550+
551+
for _ in 0..at_byte_padding {
552+
output.push(b' ')
553+
}
554+
555+
output.extend_from_slice(at_byte_str.as_bytes());
556+
557+
output.push(b' ');
558+
559+
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
560+
561+
output.push(b' ');
562+
563+
let from_byte_str = format_byte(from_byte);
564+
let from_byte_padding = 4 - from_byte_str.len();
565+
566+
output.extend_from_slice(from_byte_str.as_bytes());
567+
568+
for _ in 0..from_byte_padding {
569+
output.push(b' ')
570+
}
571+
572+
output.push(b' ');
573+
574+
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
575+
576+
output.push(b' ');
577+
578+
output.extend_from_slice(format_byte(to_byte).as_bytes());
579+
580+
output.push(b'\n');
581+
582+
stdout.write_all(output.as_slice()).map_err(|e| {
550583
format!(
551584
"{}: error printing output: {e}",
552585
params.executable.to_string_lossy()
@@ -555,16 +588,29 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
555588
}
556589
} else {
557590
for (at_byte, from_byte, to_byte) in diffs {
591+
output.clear();
592+
593+
// "{:>width$} {:>3o} {:>3o}"
558594
let at_byte_str = at_byte_buf.format(at_byte);
559-
writeln!(
560-
stdout,
561-
"{:>width$} {} {}",
562-
at_byte_str,
563-
format_octal(from_byte, &mut from_oct),
564-
format_octal(to_byte, &mut to_oct),
565-
width = width
566-
)
567-
.map_err(|e| {
595+
let at_byte_padding = width - at_byte_str.len();
596+
597+
for _ in 0..at_byte_padding {
598+
output.push(b' ')
599+
}
600+
601+
output.extend_from_slice(at_byte_str.as_bytes());
602+
603+
output.push(b' ');
604+
605+
output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());
606+
607+
output.push(b' ');
608+
609+
output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());
610+
611+
output.push(b'\n');
612+
613+
stdout.write_all(output.as_slice()).map_err(|e| {
568614
format!(
569615
"{}: error printing output: {e}",
570616
params.executable.to_string_lossy()

0 commit comments

Comments
 (0)