Files
zed/crates/multi_buffer
Parker Loflin 705b429e78 multi_buffer: Sync only changed buffers (#67053)
## Summary

A change to any buffer made the multibuffer's next sync read every
buffer and diff, and adding a buffer counted as a change. With N
buffers, each change cost O(N). Inserting excerpts syncs first, so
building a multibuffer one path at a time took O(N²) time on the main
thread. Project search, project diagnostics, multi-file git diffs, and
find references all build their multibuffers this way.

The multibuffer keeps a set of the buffers that changed or were added
since the last sync, and a sync reads only those buffers and their
diffs. `Buffer::record_changes` takes this set in place of a changed
flag, and each buffer adds its ID when it changes. The snapshot stores
each buffer's dirty, deleted-file, and conflict flags and counts how
many buffers have each one, so `is_dirty`, `has_deleted_file`, and
`has_conflict` don't scan every buffer.

This also fixes three cases where the multibuffer stayed out of date
until another buffer changed:

- The `Buffer` methods `did_reload`, `set_capability`, `set_conflict`,
and `set_agent_selections` changed dirty, conflict, or selection state
without marking the buffer as changed.
- Removing a buffer didn't update `is_dirty`, `has_deleted_file`, or
`has_conflict`.
- `MultiBuffer::clone` dropped changes that the original hadn't synced
yet.

Benchmark: a test searches an in-memory project of N files, with two
matches in each, in a `ProjectSearchView`. Both versions call
`MultiBuffer::sync_from_buffer_changes` N + 1 times, and sync time is
the total of those calls. Release build, single-threaded GPUI test
executor, multibuffer and worktree invariant checks off, Ryzen 7
9800X3D, median of five alternating runs of `main` and this branch:

| Files | Sync before | Sync after | Sync speedup | Search before |
Search after | Search speedup |
|---|---|---|---|---|---|---|
| 1,000 | 49.4 ms | 2.4 ms | 21× | 4.21 s | 4.13 s | 1.02× |
| 2,000 | 216.3 ms | 5.2 ms | 42× | 10.59 s | 10.24 s | 1.03× |
| 4,000 | 928.5 ms | 9.8 ms | 95× | 30.17 s | 28.83 s | 1.05× |

## Testing

- `test_dirty_state_across_buffers` checks `MultiBuffer::is_dirty`
across three buffers through edits, saves, a reload, a capability
change, and an excerpt removal. It fails on `main` at the reload.
- `test_deleted_file_and_conflict_state_across_buffers` marks two
buffers deleted, then conflicted, without editing them, and clears or
removes them one at a time. Each flag stays set until its last buffer is
cleared or removed. It fails on `main` at the first conflict check.
- `test_clone_with_pending_changes` clones a multibuffer before it syncs
a buffer edit and checks that both copies show the edit. It fails on
`main`, where the clone misses the edit.
- After each mutation, the randomized multibuffer tests check that each
buffer's snapshot matches the buffer's version and flags, and that the
counts agree with the flags.
- `cargo test -p multi_buffer`, `cargo test -p editor -p search -p
language`, and `./script/clippy -p multi_buffer -p language` pass, as do
the randomized multibuffer tests with `ITERATIONS=300`.

## 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 performance of multibuffers with many files, such as large
project search results
2026-11-18 09:34:07 +00:00
..