Skip to content

Commit

Permalink
cmp: completely avoid Rust fmt in verbose mode
Browse files Browse the repository at this point in the history
This makes the code less readable, but gets us a massive improvement
to performance. Comparing ~36M completely different files goes from
~5s to ~2.8s using -l and -b. Compared to GNU cmp, we now run the
same comparison in ~50% of the time.

This also improves comparing binary files. A comparison of chromium
and libxul now takes ~13s on my M1 Max, compared to ~23s before. We
also beat GNU cmp, which does it in ~25s.
  • Loading branch information
kov committed Sep 25, 2024
1 parent 8bc85fe commit 0a06127
Showing 1 changed file with 69 additions and 21 deletions.
90 changes: 69 additions & 21 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ fn format_byte(byte: u8) -> String {
unsafe { String::from_utf8_unchecked(quoted) }
}

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

Expand All @@ -453,41 +456,86 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) {
let mut from_oct = [0u8; 3]; // for octal conversions
let mut to_oct = [0u8; 3];

// Capacity calc: at_byte width + 2 x 3-byte octal numbers + 4-byte value + up to 2 byte value + 4 spaces
let mut output = Vec::<u8>::with_capacity(width + 3 * 2 + 4 + 2 + 4);

if params.print_bytes {
for (at_byte, from_byte, to_byte) in diffs {
output.clear();

// "{:>width$} {:>3o} {:4} {:>3o} {}",
let at_byte_str = at_byte_buf.format(at_byte);
writeln!(
stdout,
"{:>width$} {} {:4} {} {}",
at_byte_str,
format_octal(from_byte, &mut from_oct),
format_byte(from_byte),
format_octal(to_byte, &mut to_oct),
format_byte(to_byte),
)
.unwrap_or_else(|e| {
let at_byte_padding = width - at_byte_str.len();

for _ in 0..at_byte_padding {
output.push(b' ')
}

output.extend_from_slice(at_byte_str.as_bytes());

output.push(b' ');

output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());

output.push(b' ');

let from_byte_str = format_byte(from_byte);
let from_byte_padding = 4 - from_byte_str.len();

output.extend_from_slice(from_byte_str.as_bytes());

for _ in 0..from_byte_padding {
output.push(b' ')
}

output.push(b' ');

output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());

output.push(b' ');

output.extend_from_slice(format_byte(to_byte).as_bytes());

output.push(b'\n');

stdout.write_all(output.as_slice()).unwrap_or_else(|e| {
eprintln!(
"{}: error printing output: {e}",
params.executable.to_string_lossy()
)
);
std::process::exit(2);

Check warning on line 506 in src/cmp.rs

View check run for this annotation

Codecov / codecov/patch

src/cmp.rs#L502-L506

Added lines #L502 - L506 were not covered by tests
});
}
} else {
for (at_byte, from_byte, to_byte) in diffs {
output.clear();

// "{:>width$} {:>3o} {:>3o}"
let at_byte_str = at_byte_buf.format(at_byte);
writeln!(
stdout,
"{:>width$} {} {}",
at_byte_str,
format_octal(from_byte, &mut from_oct),
format_octal(to_byte, &mut to_oct),
width = width
)
.unwrap_or_else(|e| {
let at_byte_padding = width - at_byte_str.len();

for _ in 0..at_byte_padding {
output.push(b' ')

Check warning on line 518 in src/cmp.rs

View check run for this annotation

Codecov / codecov/patch

src/cmp.rs#L518

Added line #L518 was not covered by tests
}

output.extend_from_slice(at_byte_str.as_bytes());

output.push(b' ');

output.extend_from_slice(format_octal(from_byte, &mut from_oct).as_bytes());

output.push(b' ');

output.extend_from_slice(format_octal(to_byte, &mut to_oct).as_bytes());

output.push(b'\n');

stdout.write_all(output.as_slice()).unwrap_or_else(|e| {
eprintln!(
"{}: error printing output: {e}",
params.executable.to_string_lossy()
)
);
std::process::exit(2);

Check warning on line 538 in src/cmp.rs

View check run for this annotation

Codecov / codecov/patch

src/cmp.rs#L534-L538

Added lines #L534 - L538 were not covered by tests
});
}
}
Expand Down

0 comments on commit 0a06127

Please sign in to comment.