Skip to content

Commit

Permalink
Cache time string of the last mtime
Browse files Browse the repository at this point in the history
Files can have the same mtime (especially when using
`SOURCE_DATE_EPOCH`). Cache the time string of the last mtime.

Benchmark results on a Raspberry Pi Zero 2W running Ubuntu 24.04 (noble)
arm64:

```
$ sudo 3cpio -x /boot/initrd.img -C /var/tmp/initrd
$ ( cd /var/tmp/initrd && find . | LC_ALL=C sort | sudo cpio --reproducible --quiet -o -H newc ) > initrd.img
$ hyperfine -N --warmup 3 --runs 100 "target/release/3cpio -tv initrd.img" "3cpio-0.3.0+9 -tv initrd.img"
Benchmark 1: target/release/3cpio -tv initrd.img
  Time (mean ± σ):     108.8 ms ±   1.1 ms    [User: 46.3 ms, System: 61.3 ms]
  Range (min … max):   107.1 ms … 111.5 ms    100 runs

Benchmark 2: 3cpio-0.3.0+9 -tv initrd.img
  Time (mean ± σ):     113.2 ms ±   1.0 ms    [User: 51.2 ms, System: 60.8 ms]
  Range (min … max):   111.4 ms … 115.7 ms    100 runs

Summary
  target/release/3cpio -tv initrd.img ran
    1.04 ± 0.01 times faster than 3cpio-0.3.0+9 -tv initrd.img
```

Signed-off-by: Benjamin Drung <bdrung@posteo.de>
  • Loading branch information
bdrung committed Aug 6, 2024
1 parent afa4d9f commit 0c116cf
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ fn read_cpio_and_print_long_format<R: Read + SeekForward, W: Write>(
now: i64,
user_group_cache: &mut UserGroupCache,
) -> Result<()> {
// Files can have the same mtime (especially when using SOURCE_DATE_EPOCH).
// Cache the time string of the last mtime.
let mut last_mtime = 0;
let mut time_string: String = "".into();
loop {
let header = match read_cpio_header(file) {
Ok(header) => {
Expand All @@ -497,6 +501,10 @@ fn read_cpio_and_print_long_format<R: Read + SeekForward, W: Write>(
None => header.gid.to_string(),
};
let mode_string = header.mode_string();
if header.mtime != last_mtime || time_string.is_empty() {
last_mtime = header.mtime;
time_string = format_time(header.mtime, now)?;
};

if header.mode & MODE_FILETYPE_MASK == FILETYPE_SYMLINK {
let target = header.read_symlink_target(file)?;
Expand All @@ -508,7 +516,7 @@ fn read_cpio_and_print_long_format<R: Read + SeekForward, W: Write>(
user,
group,
header.filesize,
format_time(header.mtime, now)?,
time_string,
header.filename,
target
)?;
Expand All @@ -522,7 +530,7 @@ fn read_cpio_and_print_long_format<R: Read + SeekForward, W: Write>(
user,
group,
header.filesize,
format_time(header.mtime, now)?,
time_string,
header.filename
)?;
};
Expand Down

0 comments on commit 0c116cf

Please sign in to comment.