Skip to content

cp: stat each copied file fewer times - #16631

Open
KrushnXGIT wants to merge 4 commits into
uutils:mainfrom
KrushnXGIT:cp-fewer-stats
Open

KrushnXGIT wants to merge 4 commits into
uutils:mainfrom
KrushnXGIT:cp-fewer-stats

Conversation

@KrushnXGIT

@KrushnXGIT KrushnXGIT commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

cp -R makes 26 syscalls per copied file on Linux, 11 of them statx. GNU 9.12 makes 12 in total. Most of the extra calls look up something already known:

  • The walk lstated every entry, but walkdir already has its type (the lstat came in with feat(cp): optimize directory copy by caching file checks and refactoring calls #8805).
  • copy_file checked whether a destination symlink's target exists, and whether the source and destination are hard links, for every file. Those answers are only used when the destination is a symlink, or with --remove-destination.
  • The destination was lstated a second time even when it didn't exist the first time.
  • copy_attributes lstated the source even when no attribute is preserved, as with plain cp -R.
  • On Linux, the opened source was fstated twice in a row.

The second commit reads the umask once for the whole copy instead of with 2 umask calls per file.

100 dirs × 1000 files on tmpfs, 15 runs each (GNU 9.12 is the reference build from CONTRIBUTING.md):

syscalls/file empty files 100-byte files
GNU 9.12 12 960 ± 53 ms 711 ± 3 ms
main 26 1561 ± 43 ms 1756 ± 11 ms
this PR 18 1103 ± 11 ms 1295 ± 43 ms

Output, errors and exit codes match main for -R, -a, -RL, -RH, -Rp, --preserve=links, -Rl, --sparse=always/never, --reflink=never, --no-preserve=mode, -v and --attributes-only. The tree had symlinks (some dangling), hard links, a FIFO and a sparse file. They also match when copying onto an existing copy with -R, -f, --remove-destination, -n, -u, --backup=numbered and -a. The outcome can only differ when a file appears or disappears during the copy.

Comment thread src/uu/cp/src/cp.rs Resolved
@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.49 MB (+108 KB, +0.07%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 13.36%

2 warnings

⚠️ 5 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 6 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 6 improved benchmarks
✅ 5 untouched benchmarks
⏩ 472 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ CPU Simulation cp_recursive_wide_tree[(6000, 800)] 228 ms 189.1 ms +20.58%
⚡ CPU Simulation cp_recursive_deep_tree[(120, 4)] 16.7 ms 14.1 ms +18.4%
⚡ Memory cp_recursive_deep_tree[(120, 4)] 788.5 KB 670.5 KB +17.6%
⚡ CPU Simulation cp_recursive_balanced_tree[(5, 4, 10)] 91.4 ms 78 ms +17.2%
⚡ CPU Simulation cp_preserve_metadata[(5, 4, 10)] 97.9 ms 94.2 ms +3.96%
⚡ CPU Simulation cp_archive_balanced_tree[(5, 4, 10)] 105.2 ms 101.5 ms +3.71%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing KrushnXGIT:cp-fewer-stats (cfb09bd) with main (0df6594)2

Open in CodSpeed

Footnotes

  1. 472 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (85dd1ff) during the generation of this report, so 0df6594 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!

@xtqqczze

Copy link
Copy Markdown
Collaborator

Please check against GNU coreutils 9.12, or master branch as per CONTRIBUTING.md.

@KrushnXGIT

Copy link
Copy Markdown
Contributor Author

Please check against GNU coreutils 9.12, or master branch as per CONTRIBUTING.md.

Thanks, re-measured against the GNU 9.12 reference build from CONTRIBUTING.md and updated the description: 9.12 makes 12 syscalls per copied file, main 26, this PR 18.

@xtqqczze

Copy link
Copy Markdown
Collaborator

Please drop the merge commit and consider squashing the branch history unless each commit is intended to stand alone.

@sylvestre

Copy link
Copy Markdown
Contributor

@codspeedbot fix this regression

@sylvestre

Copy link
Copy Markdown
Contributor

Well done.
I am curious if you could add a test to make sure it doesn't regress?
In a bash script, it might be easier

@codspeed

codspeed Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

@codspeedbot fix this regression

I didn't push a fix, because this PR does not cause the regression. The 4 Memory regressions come from a benchmark harness upgrade on main, not from the cp changes. The CPU Simulation gains are real.

Why the memory numbers moved

  • The report compares against main at 12953e7, which used codspeed-divan-compat 5.0. Your merge commit brought in c71d716, which upgrades it to 6.1. The report flags this: "9 benchmarks ... ran on a different major version of their CodSpeed integration". The base and head also ran on different CPUs (AMD EPYC vs Intel Xeon).
  • main shows the same jump without any cp change. From 12953e7 to dded1b5 (the commit you merged), CodSpeed measured cp_large_file[16] going from 64.2 KB to 153.6 KB, and cp_archive_balanced_tree and cp_preserve_metadata going from 1.3 MB to 1.4 MB. These are the same values your PR shows.
  • Your last commit before the merge (ef36757) still used the old harness. On that run, all 4 Memory benchmarks matched main, and the Simulation gains were already there (cp_recursive_wide_tree -20.6%, cp_recursive_deep_tree -18.5%, cp_recursive_balanced_tree -17.4%).
  • cp_recursive_deep_tree is the only one whose CI number differs from new main: 788.5 KB here vs 670.5 KB on dded1b5. I checked it locally. I built cp from dded1b5 and from this PR, copied the same trees the benchmarks use, and measured peak heap with valgrind --tool=dhat ("At t-gmax"). The peaks match within 33 bytes:
case main (dded1b5) this PR
-R, deep tree (120 levels, 4 files each) 671,375 B 671,397 B
-a, balanced tree (5, 4, 10) 1,398,364 B 1,398,386 B
-R --preserve=mode,timestamps, balanced tree 1,398,970 B 1,398,992 B
single 16 MB file 205,744 B 205,777 B

I could not reproduce the 788.5 KB in CodSpeed Memory mode, because macro runners are not enabled for this organization. Since the plain cp peak does not change, I treat it as a measurement shift from the new harness version, not something this diff does.

What you can do

  • When you drop the merge commit and rebase on main, as xtqqczze asked, the next report should compare against a main run that already uses divan 6.1. The fake memory regressions should then go away.
  • If they stay, acknowledge them on the CodSpeed page of this branch.

If the next report still shows a memory regression after the rebase, comment this and I'll look into it:

@codspeedbot explain the cp memory results on the latest run
Summary
  • Warming up the performance wizard...
  • Read the CodSpeed report and runs — The memory jumps also appear on main after the codspeed-divan-compat 6.1 upgrade
  • Measure base and head in the same environment — Peak heap of cp is identical for main and this PR (dhat)
  • Reply with findings — Reply with findings
  • Handling comment — Comment handling finished successfully

Learnings updated. See what CodSpeed remembers about this repository.

Was this response helpful? React with 👍 or 👎 to help us improve CodSpeedBot.

@KrushnXGIT

Copy link
Copy Markdown
Contributor Author

Well done. I am curious if you could add a test to make sure it doesn't regress? In a bash script, it might be easier

Added one to util/check-open-once.sh (the Redundant Open Check job): it copies 10 and then 20 empty files with cp -R under strace, and fails if each extra file costs more than 7 stat calls, or any umask call. Against main it fails with "cp -R made 13 stat calls per copied file, expected at most 7".

@KrushnXGIT

KrushnXGIT commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Please drop the merge commit and consider squashing the branch history unless each commit is intended to stand alone.

Thanks @xtqqczze , sir sylvestre rebased it. I'll rebase my branches instead of merging main from now on.

@KrushnXGIT

Copy link
Copy Markdown
Contributor Author

Rebased onto main to resolve the conflict with 0df6594: the --remove-destination hard link check keeps this PR's order (the option first, then the stats) and uses main's new paths_are_distinct_entries.

The new check is in util/check-open-once.sh, which #16123 rewrites in Rust. @oech3, if yours lands first, I'll port the check there.

@KrushnXGIT

Copy link
Copy Markdown
Contributor Author

/rerun

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants