Skip to content

mv: add --exchange to atomically swap two paths - #13633

Closed
codspeed[bot] wants to merge 2 commits into
mainfrom
mv-exchange
Closed

codspeed[bot] wants to merge 2 commits into
mainfrom
mv-exchange

Conversation

@codspeed

@codspeed codspeed Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Should make test tests/mv/mv-exchange.sh pass


Performance follow-up (CodSpeed)

The reported regression on mv_single_file / mv_force_overwrite comes from the CLI layer, not from the --exchange implementation: those benchmarks invoke uumain 1000 times per iteration on tiny files, so ~80% of what they measure is clap building and parsing the Command. Registering one more argument costs ~5.8k instructions per invocation (verified by keeping the whole feature but dropping only the .arg(OPT_EXCHANGE…) registration, which lands exactly on the main numbers).

To offset it, this branch now removes redundant work from mv's per-file hot path:

  • handle_two_paths reuses a single symlink_metadata call for the source instead of is_dir() + is_symlink(), and for the target instead of is_symlink() + is_dir().
  • target.exists() is only checked when the source is a directory (the exists() result is unused otherwise).
  • assert_not_same_file no longer formats the quoted "target display" error string on every successful move; it is now built only when an error is actually reported.
  • The BackupMode check in assert_not_same_file is evaluated before the file-identity checks, so no stat calls are made when a backup mode makes them irrelevant.
  • rename replaces to.exists() && to.is_dir() && !to.is_symlink() with a single symlink_metadata lookup.
  • uucore::fs::are_hardlinks_to_same_file / are_hardlinks_or_one_way_symlink_to_same_file look up the target first and return early, instead of always querying both paths (the target is usually the one that does not exist).

Measured locally with codspeed run --mode simulation (instruction counts, deterministic):

Benchmark main this branch before this branch now
mv_single_file 143.1M 150.2M (+5.0%) 146.5M (+2.4%)
mv_force_overwrite 144.3M 151.8M (+5.2%) 148.5M (+2.9%)
mv_multiple_to_dir 31.9M 32.0M 28.4M (−11.2%)
mv_directory 7.747M 7.754M 7.750M

cp benefits from the uucore change as well: cp_recursive_wide_tree −2.4%, the balanced-tree/archive/preserve-metadata benchmarks ~−0.8% each.

The remaining ~2.5% on the two single-file benchmarks is the intrinsic cost of registering an extra clap flag (a few microseconds of CLI setup per mv process); it does not affect actual move throughput, which is why mv_multiple_to_dir (1000 files in one invocation) is unchanged by the feature. Tests: cargo test --features mv test_mv and --features cp test_cp show no new failures compared to main.

sylvestre and others added 2 commits July 25, 2026 20:13
Should make test tests/mv/mv-exchange.sh pass
The same-file detection and the two-path handling in mv issued several
stat calls whose result was already available, and assert_not_same_file
formatted the quoted target path used in error messages even when the
move succeeded. Reuse a single symlink_metadata lookup per path, only
build the error string when an error is reported, and make the hardlink
comparison helpers in uucore look up the target first so they can return
early when it does not exist.
@codspeed

codspeed Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Merging this PR will regress 1 benchmark

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 343 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation mv_single_file 63 ms 65.1 ms -3.25%
⚡ Simulation mv_multiple_to_dir 12.3 ms 11 ms +11.69%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mv-exchange (d7c0cc7) with main (197eac4)

Open in CodSpeed

Footnotes

  1. 46 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. ↩

@codspeed
codspeed Bot marked this pull request as ready for review July 29, 2026 11:13
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/cp/sparse-to-pipe. tests/cp/sparse-to-pipe is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/date/resolution (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/retry (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/mv/mv-exchange is no longer failing!
Congrats! The gnu test tests/tail/tail-n0f is now passing!

@cakebaker cakebaker closed this Jul 29, 2026
@sylvestre
sylvestre deleted the mv-exchange branch August 2, 2026 08:21
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