mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-27 11:02:33 +01:00
c671f77ffd
## Summary `SumTree::edit` and `insert_or_replace` built a new tree on every call. They sliced off the items before the first key, appended the new items, then appended the rest. A slice re-appends whole subtrees one sibling at a time, on every level, and clones their summaries. When an edit only inserts and its keys are distinct, it now descends to the leaves that take the items and changes them in place, splitting the nodes that overflow. It clones a node only if another tree shares it. `insert_or_replace` takes the same path. Edits that remove items or repeat a key still slice. The worktree scanner calls `edit` twice per directory while it holds `BackgroundScanner::state`. 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 41 alternating runs per variant (31 for Firefox): | Tree | Entries | Before | After | |---|---|---|---| | Linux checkout | 120,916 | 263.0 ms | 150.4 ms | | Firefox checkout | 532,906 | 1.78 s | 871.3 ms | | Zed checkout | 5,339 | 31.7 ms | 21.5 ms | After was faster in every run. CPU time fell 12–13% on Linux and Firefox and stayed within 2% on Zed. Pinned to 4 cores, the Linux scan takes 227.1 ms instead of 311.7 ms. Both versions found the same entries in every run, and peak memory didn't grow. GitHub-hosted runners (4 vCPUs, 3 on macOS), shallow clones, same harness, before → after, with the runs where after was faster in parentheses: | Tree | `ubuntu-latest` | `ubuntu-24.04-arm` | `macos-latest` | `windows-latest` | |---|---|---|---|---| | Linux | 810.5 → 698.3 ms (30/31) | 1.04 → 0.75 s (31/31) | 1.43 → 1.24 s (46/51) | 7.64 → 7.48 s (21/21) | | Firefox | 5.18 → 4.22 s (15/15) | 7.03 → 4.58 s (15/15) | 16.02 → 13.92 s (16/21) | 34.88 → 33.47 s (9/9) | | Zed | 75.4 → 64.9 ms (30/31) | 92.3 → 65.5 ms (31/31) | 85.5 → 74.2 ms (41/51) | 587.4 → 577.0 ms (16/21) | The scanner starts one worker per CPU, so the runners have less lock contention to remove. On `windows-latest`, file system calls dominate. Time per inserted item, before → after. Release build pinned to one core, median of 7 alternating rounds: | Call | Time per item | |---|---| | `edit`, 19 adjacent items into a tree of 120,000 | 340 → 41 ns | | `edit`, 1 item into 100,000 | 5,809 → 314 ns | | `edit`, 1 item at the end | 3,152 → 259 ns | | `insert_or_replace` into 1,000,000 | 5,148 → 541 ns | | `edit`, 100,000 scattered items into 1,000,000 | 598 → 181 ns | | `edit`, 1,000,000 items into an empty tree | 17.3 → 16.0 ns | When a snapshot shares every node, so each insert clones the nodes on its path, the first four take 63, 793, 733, and 1,128 ns. On the four runners, x86_64 and aarch64, the first four take 80–94% less time and the scattered batch 58–73% less. The load into an empty tree ranges from 7% faster to 13% slower (`windows-latest`). [Harness](https://github.com/ptweezy/zed/tree/bench-sum-tree-insert-in-place/bench/sum_tree_insert_bench). ## Testing - `test_inserts_in_place` is new. It applies random batches of inserts, single `insert_or_replace` calls, and removals to a tree and a `BTreeSet`. It checks the items, the replaced items, every node's summary and height, and that earlier clones of the tree don't change. Without removals, it also checks that no node underflows. - It fails for each of 19 mutations of the new code, such as a stale summary, a wrong bound for a child, a split inserted out of order, or a shared node changed without a clone. - `test_edit` now repeats a key whose two items are in different leaves. It fails if that batch takes the in-place path. - `cargo test -p sum_tree -p text -p multi_buffer -p language -p git -p editor`, `cargo test -p worktree --features test-support`, and `cargo test -p project --features test-support --test integration` pass. - The randomized `text` and `worktree` tests also pass with `ITERATIONS=300` and `ITERATIONS=100`. - `./script/clippy -p sum_tree` 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 how fast large projects are scanned when they are opened