Files
zed/crates/fs
Remco Smits 99c890d39d fs: Optimize filesystem watcher path matching (#66973)
## Summary

While working on https://github.com/zed-industries/zed/pull/66835 and
https://github.com/zed-industries/zed/discussions/66325 I noticed some
`ERROR [fs::fs_watcher] Native filesystem watcher requested rescan:
reason="rescan: user dropped"` error messages. While looking into the
root cause which is hard to find because there is no known reason the
`user dropped` message occurs, I noticed with some profiling code & the
`dev: debug filesystem watching` panel that while you are doing a clean
`cargo build` the **FS** queue builds up to around **68.000** events
which is bad, because Zed should be able to keep up with those FS events
that are produced by **cargo**.

So I went ahead and looked into this performance issue, and found that
one major bottleneck was filesystem event routing repeatedly normalizing
and lowercasing the same path prefixes for every ancestor lookup. This
was particularly expensive for deeply nested build output paths, that
cargo produces when you do a cargo build for the Zed repo.

By processing each event path once and reusing its ancestors for
case-insensitive lookups, average routing time per event dropped by
approximately **76%** across three before-and-after clean `cargo build`
runs, substantially reducing the event backlog :).

| Metric | Before 1 | Before 2 | Before 3 | After 1 | After 2 | After 3
| Average performance gain |
|---|---:|---:|---:|---:|---:|---:|---:|
| Notifications received | 213,209 | 213,237 | 213,222 | 213,613 |
213,507 | 213,603 | Comparable (+0.16%) |
| Average routing/event | 562 µs | 559 µs | 565 µs | 143 µs | 130 µs |
135 µs | **75.8% lower** |
| Total routing wall time | 119.85 s | 119.16 s | 120.46 s | 30.48 s |
27.66 s | 28.90 s | **75.8% lower** |
| Total dispatch wall time | 123.30 s | 122.56 s | 124.13 s | 33.34 s |
30.32 s | 31.71 s | **74.2% lower** |
| Observed queue peak | 67,122 | 68,058 | 67,933 | 6,269 | 1,566 | 4,653
| **93.9% lower** |
| Average queue wait | 16.58 s | 16.96 s | 17.35 s | 132 ms | 23 ms |
104 ms | **99.5% lower** |
| Maximum queue wait | 47.57 s | 46.95 s | 50.43 s | 1.45 s | 339 ms |
983 ms | **98.1% lower** |

## Testing

**Note**: I didn't add a test since there is already tests covering this
part and there should be no behavior changes, just a pure performance
gain :).

You can test the performance gain by opening Zed via a second a Zed repo
clone, remove the **target** folder & its files and run cargo build
inside the build in terminal, and open the `dev: debug filesystem
watching` notice the **before** and **after** peak queue size for
example.

## 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 responsiveness during heavy filesystem activity by reducing
filesystem watcher path-matching overhead.
2026-11-14 13:43:29 +00:00
..