Skip to content

Speed up macOS directory scans when bulk reads stall - #401

Merged
Byron merged 1 commit into
mainfrom
faster-bulk-traversal
Sep 11, 2026
Merged

Speed up macOS directory scans when bulk reads stall#401
Byron merged 1 commit into
mainfrom
faster-bulk-traversal

Conversation

@Byron

@Byron Byron commented Sep 11, 2026

Copy link
Copy Markdown
Owner

Tasks

This section is for Byron only. Models continuing this PR must not add, remove, check, uncheck, rename, or reorder checkboxes here.

  • refackiew

Everything below this line was generated by Codex GPT-6.

Created by Codex on behalf of Byron. Byron will review before this is ready to merge.

Change

Large macOS directories could leave one worker waiting inside getattrlistbulk at about 15% of a CPU core. Native traversal also collected an entire parent directory before publishing its entries and child jobs.

Native traversal now streams batches and uses a bounded initial bulk probe to choose how to collect metadata. If the first refill is CPU-bound, bulk enumeration continues with its cached metadata. If two initial refills each spend more time waiting than executing, multiworker walks reopen with ordinary directory enumeration and distribute stat calls through the existing worker pool. The CPU measurement includes kernel time; there is no absolute latency or entry-count cutoff.

The probe holds at most two 64 KiB bulk buffers worth of entries and is discarded only after reopening succeeds, before any entry or child job has been published. This avoids duplicate entries and mixing incompatible directory cursors. Metadata queues are bounded; a full local queue makes the producer process its next batch inline. Parent ordering and optional APFS clone metadata remain supported.

This initial decision deliberately trades continuous adaptation for bounded state: it can miss a later cache transition, and a temporary cold prefix can select stat even if later bulk reads would be fast. CPU waiting also includes descheduling, so the signal is a heuristic rather than a universal crossover guarantee.

Measurements

On the reported live target, alternating final/original/final interactive runs took 18.05 / 34.60 / 17.63 seconds. The final run averaged about 1.24 CPU cores, versus 0.29 for the original whole scan; peak memory fell from 404 MB to 205 MB. The target was changing during investigation, so these timings are not a controlled benchmark ratio.

A stable 218,432-file corpus retained cached performance: three alternating cached runs had medians of 0.402 s original and 0.399 s final. Isolated cached bulk refills used about 99.5–99.8% CPU, while the slow real directory used about 14%, supporting a relative waiting/execution signal. Restricting bulk calls to listing attributes still spent 40.2 of 43.2 seconds inside listing calls, which is why the implementation reopens with ordinary enumeration after the unpublished probe.

Validation

  • Regression tests cover timing decisions at different clock scales, bounded probe/backlog memory, streamed child work, parent ordering, and native/stat metadata parity for APFS clones, resource forks, hard links and symlinks.
  • make check, make unit-tests, and make journey-tests.
  • cargo clippy --workspace --all-targets --all-features -- -D warnings and cargo fmt --check.

Reported issue

$issue-full-auto When using `dua i /Users/byron/dev/github.com/GitoxideLabs/gitoxide.worktree-create/
  target` it scans the folder very slowly with just 15%⠂of a CPU core. I recorded a ⠂
  trace of the final seconds, that includes the first screen refresh as well.      ⠈ ⢀
 ⠈Investigate what's going⢀on and if this can be faster. /Users/byron/Desktop/dua-slow-⠐
  scan.trace

@Byron
Byron force-pushed the faster-bulk-traversal branch 2 times, most recently from f27ef37 to 521a07a Compare September 11, 2026 13:41
@Byron
Byron marked this pull request as ready for review September 11, 2026 13:43
This was an issue I encountered on APFS, a directory created by rustc with
more than 1m files in it. In that case, bulk reading is slow (while efficient),
and it turned out to be better to detect this and switch over to stat-based traversal.

While looking at the filesystem probing code more closely (and how it's not dependent
on CPU performance by differntiating wall time from kernel time), I basically
rubber-stamped all the other code. Too much to look at, too foreign by now.
But it did look cleaned up, so 👍.

<!-- agent -->
The reported target-directory scan left one worker waiting inside
`getattrlistbulk` at about 15% of a CPU core. Native directory collection also
held an entire parent directory before publishing entries and child jobs.

Probe at most two initial bulk buffers before publishing them. Compare
calling-thread user and kernel CPU time with elapsed time, and select the
existing parallel stat queue when both refills spend more time waiting than
executing. Reopen through ordinary enumeration only after that decision,
discarding the unpublished probe to avoid mixed cursors or duplicate entries.
Keep bulk reads when the probe is CPU-bound. The initial probe deliberately
does not adapt to later cache changes.

Share streaming native traversal between ordering modes, preserve parent
ordering and APFS metadata, and bound queued metadata jobs by processing
new batches inline when the queue fills.

Regression coverage checks scale-independent timing decisions, bounded
probing and metadata backlogs, streamed child jobs, parent ordering, and
stat/native parity for clones, resource forks, hard links and symlinks.
The timing test failed against the provisional fixed latency policy; the
queue-bound test failed without backpressure.

Assisted-by: GPT 6.0
Co-authored-by: GPT 6.0 <codex@openai.com>
@Byron
Byron force-pushed the faster-bulk-traversal branch from 521a07a to 249bdf3 Compare September 11, 2026 13:44
@Byron
Byron merged commit 46b8556 into main Sep 11, 2026
3 checks passed
@Byron
Byron deleted the faster-bulk-traversal branch September 11, 2026 13:54
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.

1 participant