Files
zed/crates/worktree
Parker Loflin 87e6a02e1e worktree: Do less work while scan_dir holds the state lock (#67226)
## Summary

`BackgroundScanner` workers hold the `state` lock for about 85% of an
initial scan, so work under the lock sets the scan time. `scan_dir` now
does less of it:

- It reserves one block of entry IDs per directory after `read_dir`.
Per-child IDs interleaved with other workers' IDs, so each
`entries_by_id` edit inserted in about 12 places instead of one.
- It skips `reuse_entry_id` when there are no removed entries and the
directory has no loaded children, as during an initial scan. These
lookups accounted for 58% of all path comparisons on a Linux checkout.
- It sorts children by name before it takes the lock, so `SumTree::edit`
gets them nearly in order.
- It checks `is_path_excluded` before it takes the first lock.

Initial scan with `Worktree::local` and `RealFs` in a headless app, one
scan per process, warm file cache, Ryzen 7 9800X3D (16 threads), median
of alternating runs (21 per variant, 7 for Firefox):

| Tree | Entries | Before | After |
|---|---|---|---|
| Linux checkout | 120,916 | 354.9 ms | 274.0 ms |
| Firefox checkout | 532,906 | 2.27 s | 1.85 s |
| Zed checkout | 5,339 | 37.9 ms | 35.8 ms |

After was faster in every Linux and Firefox run, and in 19 of 21 Zed
runs. CPU time is unchanged. Both versions found the same entries in
every run.

GitHub-hosted runners (4 vCPUs, 3 on macOS), shallow clones, same
harness, median of alternating runs (21 per variant, 11 for Firefox; 101
and 41 on macOS):

| Runner | Tree | Before | After | After faster |
|---|---|---|---|---|
| `ubuntu-latest` | Linux | 874.9 ms | 818.0 ms | 20/21 |
| `ubuntu-latest` | Firefox | 5.52 s | 5.23 s | 11/11 |
| `ubuntu-latest` | Zed | 75.3 ms | 74.9 ms | 13/21 |
| `macos-latest` | Linux | 1.13 s | 1.10 s | 61/101 |
| `macos-latest` | Firefox | 9.32 s | 9.24 s | 24/41 |
| `macos-latest` | Zed | 85.0 ms | 80.3 ms | 58/101 |
| `windows-latest` | Linux | 7.93 s | 7.83 s | 15/21 |
| `windows-latest` | Firefox | 36.22 s | 35.81 s | 11/11 |
| `windows-latest` | Zed | 629.7 ms | 629.0 ms | 12/21 |

The scanner starts one worker per CPU, so the runners have less lock
contention to remove. Pinned to 4 cores, the local Linux scan improves
by 11% instead of 23%. macOS runner times vary by up to 2× between runs.

## Testing

- `cargo test -p worktree --features test-support` and `cargo test -p
project --features test-support --test integration` pass.
- Without the `removed_entries` check, four of these tests fail,
including `test_rescan_and_remote_updates`, which checks that renamed
files keep their IDs. Without the loaded-children check, 22 fail.
- `./script/clippy -p worktree` passes.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Improved the time to scan large projects when they are opened
2026-11-20 09:11:01 +00:00
..