Skip to content

Conversation

@mattsu2020
Copy link
Contributor

feat: optimize directory copy by caching file checks and refactoring calls

  • Add target_is_file field to Context to avoid repeated stat calls on target
  • Refactor copy_direntry to accept &Entry and additional bool params for symlink/directory handling
  • Use cached value and local variables for cleaner access to entry properties
  • Pass created_parent_dirs to copy_file for improved directory creation tracking

This reduces filesystem overhead in cp operations when copying directories.

@sylvestre
Copy link
Contributor

please add a benchmark testing this in a separate PR
See src/uu//benches/ for other examples

@github-actions
Copy link

github-actions bot commented Oct 4, 2025

GNU testsuite comparison:

Skip an intermittent issue tests/misc/tee (fails in this run but passes in the 'main' branch)

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 4, 2025

CodSpeed Performance Report

Merging #8805 will improve performances by 4.06%

Comparing mattsu2020:cp_tuning (42c5356) with main (1331ff1)1

Summary

⚡ 5 improvements
✅ 70 untouched
🆕 2 new
⏩ 73 skipped2

Benchmarks breakdown

Benchmark BASE HEAD Change
cp_archive_balanced_tree[(5, 4, 10)] 79.7 ms 76.9 ms +3.62%
cp_preserve_metadata[(5, 4, 10)] 76.9 ms 74.1 ms +3.77%
cp_recursive_balanced_tree[(5, 4, 10)] 74.4 ms 71.5 ms +4.06%
cp_recursive_deep_tree[(120, 4)] 13.7 ms 13.2 ms +3.74%
cp_recursive_wide_tree[(6000, 800)] 192.7 ms 187.8 ms +2.61%
🆕 nl_large_file[10] N/A 128.1 ms N/A
🆕 nl_many_lines[100000] N/A 101.6 ms N/A

Footnotes

  1. No successful run was found on main (03f160c) during the generation of this report, so 1331ff1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 73 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.

@mattsu2020
Copy link
Contributor Author

add #8807

@mattsu2020
Copy link
Contributor Author

Environment: hyperfine 1.19.0, binaries built via cargo build --release (multicall at target/release/coreutils); benchmark inputs copied from docs into target/tmp/cp_bench_src.

Command setup: hyperfine --warmup 5 --runs 50 --prepare "rm -rf target/tmp/cp_bench_dst" "/bin/cp -R target/tmp/cp_bench_src target/tmp/cp_bench_dst" "target/release/coreutils cp -R target/tmp/cp_bench_src target/tmp/cp_bench_dst" --export-json target/tmp/cp_benchmark.json.

Results: /bin/cp mean 17.2 ms (σ 0.9 ms, min 15.3 ms, max 20.2 ms); coreutils cp mean 10.1 ms (σ 0.5 ms, min 9.24 ms, max 11.3 ms); both commands exited with status 0 in all 50 runs.

Relative performance: target/release/coreutils cp ran 1.71 ± 0.12 times faster than /bin/cp on this workload.
Uploading cp_benchmark.json…

…calls

- Add `target_is_file` field to Context to avoid repeated `stat` calls on target
- Refactor `copy_direntry` to accept `&Entry` and additional bool params for symlink/directory handling
- Use cached value and local variables for cleaner access to entry properties
- Pass `created_parent_dirs` to `copy_file` for improved directory creation tracking

This reduces filesystem overhead in cp operations when copying directories.
@github-actions
Copy link

github-actions bot commented Oct 4, 2025

GNU testsuite comparison:

Skip an intermittent issue tests/tail/overlay-headers (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/misc/tee (passes in this run but fails in the 'main' branch)

@sylvestre
Copy link
Contributor

nice:
image

@sylvestre sylvestre merged commit 013a385 into uutils:main Oct 4, 2025
97 checks passed
@mattsu2020 mattsu2020 deleted the cp_tuning branch October 4, 2025 23:55
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.

2 participants