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

Skip to content

Commit d22282d

Browse files
committed
cmp: avoid using advanced rust formatting for -l
Octal conversion and simple integer to string both show up in profiling. This change improves comparing ~36M completely different files wth both -l and -b by ~11-13%.
1 parent cf79004 commit d22282d

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ path = "src/main.rs"
1717
[dependencies]
1818
chrono = "0.4.38"
1919
diff = "0.1.13"
20+
itoa = "1.0.11"
2021
regex = "1.10.4"
2122
same-file = "1.0.6"
2223
unicode-width = "0.2.0"

src/cmp.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,24 @@ fn is_ascii_printable(byte: u8) -> bool {
482482
c.is_ascii() && !c.is_ascii_control()
483483
}
484484

485+
#[inline]
486+
fn format_octal(byte: u8, buf: &mut [u8; 3]) -> &str {
487+
*buf = [b' ', b' ', b'0'];
488+
489+
let mut num = byte;
490+
let mut idx = 2; // Start at the last position in the buffer
491+
492+
// Generate octal digits
493+
while num > 0 {
494+
buf[idx] = b'0' + num % 8;
495+
num /= 8;
496+
idx = idx.saturating_sub(1);
497+
}
498+
499+
// SAFETY: the operations we do above always land within ascii range.
500+
unsafe { std::str::from_utf8_unchecked(&buf[..]) }
501+
}
502+
485503
#[inline]
486504
fn format_byte(byte: u8) -> String {
487505
let mut byte = byte;
@@ -519,15 +537,20 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
519537
// Obtain the width of the first column from the last byte offset.
520538
let width = format!("{}", offset).len();
521539

540+
let mut at_byte_buf = itoa::Buffer::new();
541+
let mut from_oct = [0u8; 3]; // for octal conversions
542+
let mut to_oct = [0u8; 3];
543+
522544
if params.print_bytes {
523545
for (at_byte, from_byte, to_byte) in diffs {
546+
let at_byte_str = at_byte_buf.format(at_byte);
524547
writeln!(
525548
stdout,
526-
"{:>width$} {:>3o} {:4} {:>3o} {}",
527-
at_byte,
528-
from_byte,
549+
"{:>width$} {} {:4} {} {}",
550+
at_byte_str,
551+
format_octal(from_byte, &mut from_oct),
529552
format_byte(from_byte),
530-
to_byte,
553+
format_octal(to_byte, &mut to_oct),
531554
format_byte(to_byte),
532555
)
533556
.map_err(|e| {
@@ -539,12 +562,13 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
539562
}
540563
} else {
541564
for (at_byte, from_byte, to_byte) in diffs {
565+
let at_byte_str = at_byte_buf.format(at_byte);
542566
writeln!(
543567
stdout,
544-
"{:>width$} {:>3o} {:>3o}",
545-
at_byte,
546-
from_byte,
547-
to_byte,
568+
"{:>width$} {} {}",
569+
at_byte_str,
570+
format_octal(from_byte, &mut from_oct),
571+
format_octal(to_byte, &mut to_oct),
548572
width = width
549573
)
550574
.map_err(|e| {

0 commit comments

Comments
 (0)