worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

copy-ignored speed

Open max-sixty opened this issue 8 months ago • 5 comments

@worktrunk-bot how do we do relative to this tweet?

image

max-sixty avatar Mar 25 '26 07:03 max-sixty

The tweet describes three challenges with copying gitignored files across worktrees:

  1. Build cache loss — wt step copy-ignored solves this directly. It's designed for exactly this use case.
  2. Reflink / cp -c — worktrunk uses reflink_copy::reflink_or_copy(), which does per-file COW on APFS (macOS), Btrfs, and XFS. For a 14GB target/ directory, this takes ~20s vs ~2m for a full copy.
  3. Deep-walk + parallelization + fd exhaustion — this is where there's room to improve:

What we do well:

  • git ls-files --ignored --directory avoids deep-walking into directories like target/ — it reports target/ as a single entry, and we clone the whole tree recursively
  • Reflink means no actual data copying, just metadata ops

Where the tweet identifies gaps we have:

  • No parallelization — the top-level copy loop (step_commands.rs:725) is sequential. For repos with many top-level gitignored entries (e.g., target/, node_modules/, .cache/, etc.), parallel entry copying could help. Inside copy_dir_recursive the walk is also sequential.
  • No fd management — we don't limit concurrent open file descriptors. The sequential approach avoids fd exhaustion by accident, but a parallel version would need to respect ulimit -n (256 on macOS by default, as the tweet notes).

Potential improvement: Parallelizing the recursive directory walk with a bounded thread/task pool (e.g., rayon or crossbeam with a semaphore for fd limits) could meaningfully speed up large copies. The top-level entries are already independent, so they're trivially parallelizable.

TL;DR: worktrunk already handles challenges 1 and 2 from the tweet. Challenge 3 (parallel walk with fd management) is a real optimization opportunity — the current implementation is sequential.

worktrunk-bot avatar Mar 25 '26 07:03 worktrunk-bot

how does the parallelization compare to the approach we had at one point (for just a moment), which had the downside of locking up the Mac (and I later found was not recommended)

max-sixty avatar Mar 25 '26 08:03 max-sixty

That was the atomic clonefile() on directories approach from #602 — macOS/APFS can clone an entire directory tree as a single O(1) metadata operation via the clonefile() syscall, which was ~12-15x faster than file-by-file copying.

The problem was that it saturated disk I/O (~45K ops/sec), blocking interactive processes like shell startup — that's the "locking up the Mac" you experienced. Apple's own docs explicitly discourage it for large hierarchies. It was reverted in #640 to per-file reflink, which is slower but keeps the system responsive.

The parallelization the tweet describes is a middle ground between those two extremes:

Approach Speed Responsiveness fd pressure
Sequential per-file reflink (current) Baseline Great Low
Parallel per-file reflink (tweet's idea) ~2-4x faster Good (tunable) Needs management
Atomic directory clonefile() (#602) ~12-15x faster Bad (saturates I/O) N/A

A bounded thread pool (e.g., rayon with a semaphore) would give us meaningful speedup without the I/O saturation problem — we control concurrency instead of handing the entire tree to the kernel in one shot. The tweet's point about fd limits (256 default on macOS) is the main constraint to respect.

worktrunk-bot avatar Mar 25 '26 08:03 worktrunk-bot

ok, draft a PR that uses a rayon pool to run a few threads

also draft a PR to add a command that would warm the cache, if that's an atomic piece of work / step func

max-sixty avatar Mar 25 '26 08:03 max-sixty

Drafted both PRs:

  1. #1721 — perf: parallelize copy-ignored with rayon — Parallelizes both the top-level entry loop and the recursive directory walk using into_par_iter().try_for_each(). Uses the existing global rayon pool (max 8 threads). No fd management needed since the bounded pool limits concurrency naturally.

  2. #1722 — feat: add wt step warm as shorthand for copy-ignored — Thin wrapper that always copies from primary to current worktree. Omits --from/--to (use copy-ignored directly for custom paths). Includes hook configuration example in the help text.

worktrunk-bot avatar Mar 25 '26 08:03 worktrunk-bot