Files
Parker Loflin 5cffb90247 project: Load diff bases for many buffers in one git process (#67026)
## Summary

Opening a diff loads its base text with
`Repository::load_committed_text` (HEAD and index, for uncommitted
diffs) or `Repository::load_staged_text` (index, for unstaged diffs).
Each call was a separate git job with its own `git cat-file --batch`
process. A repository runs its git jobs one at a time, so opening many
diffs at once queued one process per buffer, and other git work (status,
staging, blame) waited behind them. Project search results open an
uncommitted diff for every buffer they show, and the Uncommitted Changes
view one per changed file. The branch diff view and the `default_branch`
diff base open an unstaged diff per file. Each process reads the index
to resolve `:path`, so the cost grows with the repository.

Loads requested while their job is queued join it and share one process.
Only the most recently queued job takes new loads, which keeps their
order relative to other git jobs. The job owns its batch, so a job
dropped before it runs cancels its loads. If the batch fails, each load
retries in its own process, so an unreadable object fails only its own
diff. The batch decodes its texts on a background thread. Remote
repositories still send one request per buffer.

Diff base reloads already loaded every open buffer's bases in one batch,
and a failed batch cleared the bases of every buffer. Reloads retry the
same way, and a buffer whose own load fails keeps its bases.

`load_revisions` discards git's stderr, which it never read. When a
`refs/tags/HEAD` ref exists, git writes an ambiguity warning for each
`HEAD` lookup, and about 1,700 lookups filled a 64 KiB pipe and blocked
git mid-batch, in opens and reloads alike.

Benchmark: open the first N files of a checkout as buffers in a headless
app with `RealFs`, wait 5 s for the initial git status, then time
opening all of their diffs at once. Binary files don't open as buffers,
hence 983 and 996. Warm cache, Ryzen 7 9800X3D, git 2.53, median of 5
alternating runs with and without this change:

| Checkout | Index | Diff | Diffs | Before | After | Speedup |
|---|---|---|---|---|---|---|
| Zed | 0.5 MB | uncommitted | 100 | 95.0 ms | 29.6 ms | 3.2× |
| Zed | 0.5 MB | uncommitted | 983 | 980 ms | 313 ms | 3.1× |
| Zed | 0.5 MB | unstaged | 100 | 81.8 ms | 17.2 ms | 4.8× |
| Zed | 0.5 MB | unstaged | 983 | 793 ms | 169 ms | 4.7× |
| Linux | 10.5 MB | uncommitted | 100 | 418 ms | 32.6 ms | 13× |
| Linux | 10.5 MB | uncommitted | 1,000 | 4.31 s | 281 ms | 15× |
| Linux | 10.5 MB | unstaged | 100 | 398 ms | 20.0 ms | 20× |
| Linux | 10.5 MB | unstaged | 1,000 | 4.08 s | 155 ms | 26× |
| Firefox | 72 MB | uncommitted | 100 | 2.43 s | 56.1 ms | 43× |
| Firefox | 72 MB | uncommitted | 996 | 25.4 s | 314 ms | 81× |
| Firefox | 72 MB | unstaged | 100 | 2.52 s | 43.5 ms | 58× |
| Firefox | 72 MB | unstaged | 996 | 24.5 s | 183 ms | 134× |

git's CPU time drops by a similar factor, for example from 25.1 s to 95
ms for 996 uncommitted diffs in Firefox.

GitHub-hosted runners, 1,000 diffs, shallow clones, one build that
switches to the old code with an environment variable, median of 5
alternating runs:

| Runner | Checkout | Diff | Before | After | Speedup |
|---|---|---|---|---|---|
| `ubuntu-latest` | Linux | uncommitted | 13.3 s | 598 ms | 22× |
| `ubuntu-latest` | Linux | unstaged | 13.2 s | 294 ms | 45× |
| `ubuntu-latest` | rust-lang/rust | uncommitted | 10.1 s | 638 ms | 16×
|
| `ubuntu-latest` | rust-lang/rust | unstaged | 10.0 s | 354 ms | 28× |
| `windows-latest` | rust-lang/rust | uncommitted | 45.2 s | 1.05 s |
43× |
| `windows-latest` | rust-lang/rust | unstaged | 44.5 s | 444 ms | 100×
|

## Testing

- `test_diffs_opened_together` opens three uncommitted diffs (index
equal to HEAD, index differing from HEAD, untracked) and one unstaged
diff in one batch, and checks each buffer's HEAD and index text.
- `test_diff_base_loads_fail_when_their_job_is_dropped` drops a queued
job and closes the job channel, and checks that its loads and later ones
fail instead of hanging.
- `test_diff_base_loads_join_only_the_newest_queued_job` checks that two
loads share one job, and that a load queued after an index write gets
its own job and sees the new index text.
- `test_diff_base_load_with_unreadable_revision_fails_alone` makes one
HEAD revision unreadable in the fake repository and checks that the
other loads, including the index text of the same file, succeed.
- `test_diff_base_reload_with_unreadable_revision_fails_alone` reloads
three diffs while one HEAD revision is unreadable, and checks that only
that file keeps its old bases.
- `test_load_revisions_with_ambiguous_head` loads 4,000 `HEAD:`
revisions with a `refs/tags/HEAD` ref. Without the stderr change, the
test scheduler reports a deadlock after 15 seconds.
- `cargo test -p project --features test-support`, `cargo test -p git`,
`cargo test -p fs`, `cargo test -p remote_server --lib`, and
`./script/clippy -p project -p git -p fs` pass.

## 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 git diff loading when many files open at once, such as in
project search results and the Uncommitted Changes and branch diff views
2026-11-18 17:19:54 +00:00
..
…