Skip to content

perf: remove directory trees in parallel on solid-state storage - #9

Merged
huacnlee merged 1 commit into
huacnlee:mainfrom
sunfuze:perf/parallel-removal
Nov 5, 2026
Merged

huacnlee merged 1 commit into
huacnlee:mainfrom
sunfuze:perf/parallel-removal

Conversation

@sunfuze

@sunfuze sunfuze commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Permanent removal now runs on several threads on solid-state storage. Removing 100k files on an M4's internal SSD takes about 3.1s instead of 5.2s (median of 5 runs, files synced before each run).

Changes

  • src/scanner/remove.rs (new): the permanent removal moves here from scanner.rs.
    • On solid-state volumes, min(cores, 8) workers remove separate subtrees at once. Each directory is removed by the worker that finishes its last child. The first error stops every worker and is reported, as before.
    • Files are unlinked using the type readdir gives, without a stat each. Directories are still stated and checked.
    • The sequential path (rotational or unknown storage) unlinks each directory in inode order, as GNU rm does, to save seeks. On SSD this measured no faster, so the parallel path does not sort.
    • The open-file soft limit is raised to 4096 so workers can hold a deep tree's directories open (GUI apps on macOS start with 256).
  • src/scanner/media.rs (new): tells solid-state from rotational storage, cached per device.
    • macOS: IOKit Medium Type of the disk under the volume.
    • Linux: queue/rotational of the block device, through the mount's source device when the volume has none (btrfs subvolumes).
    • Storage that cannot be identified counts as rotational and keeps the sequential removal.
  • Progress: reported every 256 files under a lock the workers share. Between items, freed bytes are estimated from the item's average file size. Each completed item corrects them to the scanned value. File counts stay exact.

The safety checks are unchanged: O_NOFOLLOW, per-directory device and inode checks, and no crossing into other volumes.

Benchmark

100k files in 2,000 directories, internal SSD, Apple M4:

median range
1 worker 5.20s 5.04–5.54s
8 workers 3.07s 2.94–4.03s

Testing

  • macOS: cargo test (44 passing), cargo clippy --all-targets, cargo fmt --check.
  • Linux (Ubuntu 24.04 container): cargo test --lib and cargo clippy --lib --tests.
  • New tests cover removal on 1 and 4 workers (file counts, symlink targets kept), refusal across volumes, inode ordering, and storage detection.
  • Not tested on a rotational disk.

🤖 Generated with Claude Code

Permanent removal moves to src/scanner/remove.rs:

- On solid-state volumes, up to min(cores, 8) workers remove separate
  subtrees at once; a directory is removed by whichever worker finishes
  its last child, and the first error stops every worker. Removing 100k
  files on an M4's internal SSD drops from about 5.2s to 3.1s.
- src/scanner/media.rs tells solid-state from rotational storage: IOKit
  "Medium Type" on macOS, queue/rotational on Linux (through the mount's
  source device for btrfs). Unknown storage counts as rotational and keeps
  the sequential removal.
- Files are unlinked using the readdir type, without a stat each;
  directories are still stated and checked by device and inode.
- The sequential removal unlinks each directory in inode order, as GNU rm
  does, to save seeks on rotational disks.
- Progress is reported every 256 files under a lock shared by the
  workers; bytes between items are estimated from each item's average
  file size and corrected when the item completes.

The O_NOFOLLOW, device and inode checks are unchanged.

Co-Authored-By: Claude Opus 5 <[email protected]>

@huacnlee huacnlee left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. The PR is focused, the safety model holds up, and the macOS numbers in the description look plausible. On Linux, though, I measured the opposite result, and the Linux storage detection misses the btrfs case it says it handles.

What I ran (Arch Linux, 24 cores, NVMe, btrfs subvolume on LUKS /dev/mapper/root, head 59bdc2f)

  • cargo fmt --check, cargo clippy --all-targets: clean. cargo test: 37 passed, 2 ignored.
  • Benchmark: a temporary ignored test calls remove_tree directly on a freshly made tree with 50,000 files in 1,000 dirs plus a symlink to /etc, after sync. Release build, main vs this PR. CPU comes from getrusage(RUSAGE_CHILDREN) and includes creating the tree (about 0.35 s sys).
wall, median sys CPU
main ~355 ms ~0.68 s
PR, 1 worker ~345 ms ~0.68 s
PR, 8 workers (forced) ~438 ms ~2.75 s

All runs removed all 50,001 entries and left the /etc symlink target alone. On btrfs, 8 workers are about 25% slower and use about 4x the CPU. The sequential inode-ordered path is slightly faster than main.

Findings

  1. src/scanner/media.rs:62-84: the btrfs subvolume fallback never matches. Each btrfs subvolume has its own anonymous st_dev (/home is 0:55 and / is 0:32 here), but /proc/self/mountinfo lists every subvolume under the filesystem's id (0:30). So mount_source returns None and an NVMe drive is reported as "rotational or unknown". Matching the mountinfo entry by the longest mount-point prefix of the path, instead of by major:minor, would fix it (the dm-0 lookup after that works: queue/rotational = 0). As written, the bug happens to hide finding 2 on this machine.
  2. src/scanner/remove.rs:417: parallel removal is a regression on Linux btrfs, both in wall time and CPU. I could not test ext4 or XFS. Unless Linux numbers show a gain, please gate the parallel path to macOS, or to filesystems where it has been measured, rather than to "solid-state" alone.
  3. solid_state_detection_answers_for_any_path (src/scanner/tests.rs:1194) only checks that the answer is consistent, so it can't catch finding 1. A Linux test for the mountinfo parsing on a sample btrfs line would.

Safety review (no blockers found)

  • Every directory is opened with openat(O_NOFOLLOW|O_DIRECTORY) relative to its parent, then device and inode are checked (remove.rs:78-107). Symlinks and other non-directories come back as Leaf and are unlinked, never entered (remove.rs:154-173). A swap race fails safely with "Directory changed".
  • On partial failure, the first error clears the queue and stops all workers (remove.rs:347-353). Directories are removed only once all their children are gone (release). Files already unlinked stay removed, as before.
  • raise_open_file_limit (remove.rs:401) changes the process-wide soft limit and leaves it raised. That's fine for this app.
  • No new public API. The new items are pub(super) only.

Not verified: macOS (no Mac run in this pass), rotational disks, ext4/XFS.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@huacnlee
huacnlee merged commit ab0efdb into huacnlee:main Nov 5, 2026
2 checks passed
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