Repository navigation
perf: remove directory trees in parallel on solid-state storage - #9
Merged
Merged
Conversation
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
reviewed
Nov 5, 2026
huacnlee
left a comment
Owner
There was a problem hiding this comment.
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_treedirectly on a freshly made tree with 50,000 files in 1,000 dirs plus a symlink to/etc, aftersync. Release build, main vs this PR. CPU comes fromgetrusage(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
src/scanner/media.rs:62-84: the btrfs subvolume fallback never matches. Each btrfs subvolume has its own anonymousst_dev(/homeis 0:55 and/is 0:32 here), but/proc/self/mountinfolists every subvolume under the filesystem's id (0:30). Somount_sourcereturnsNoneand an NVMe drive is reported as "rotational or unknown". Matching the mountinfo entry by the longest mount-point prefix of the path, instead of bymajor: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.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.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 asLeafand 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 fromscanner.rs.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.readdirgives, without astateach. Directories are still stated and checked.rmdoes, to save seeks. On SSD this measured no faster, so the parallel path does not sort.src/scanner/media.rs(new): tells solid-state from rotational storage, cached per device.Medium Typeof the disk under the volume.queue/rotationalof the block device, through the mount's source device when the volume has none (btrfs subvolumes).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:
Testing
cargo test(44 passing),cargo clippy --all-targets,cargo fmt --check.cargo test --libandcargo clippy --lib --tests.🤖 Generated with Claude Code