Files
zed/crates/sum_tree/src
Parker Loflin f614e6ed9a sum_tree: Build an empty tree in one pass when an edit fills it (#67374)
## Summary

Follow-up to #67332.

#67332 made `SumTree::edit` insert items in place, which made a large
edit that fills an empty tree slower. `edit` now builds an empty tree
with `from_iter` straight from the sorted edits. That skips the two
checks that guard the in-place path and the copy of the items into a
`Vec`.

One `edit` of 1,000,000 items with `u64` keys into an empty tree, time
per item. Release build, the first edit of a new process, median of 9
alternating rounds (15 on the 9800X3D, pinned to one core):

| Machine | Before #67332 | Main | This PR |
|---|---|---|---|
| Ryzen 7 9800X3D | 19.6 ns | 21.8 ns | 13.5 ns |
| `ubuntu-latest`, Xeon Platinum 8573C | 61.2 ns | 69.8 ns | 40.7 ns |
| `ubuntu-24.04-arm`, Neoverse-N2 | 53.6 ns | 59.8 ns | 35.2 ns |
| `windows-latest` | 55.3 ns | 59.9 ns | 39.3 ns |
| `macos-15`, Apple M1 | 28.5 ns | 29.1 ns | 19.4 ns |
| `macos-15-intel`, Core i7-8700B | 105.2 ns | 112.1 ns | 62.5 ns |

With `Arc<str>` keys, the same edit takes 35–44% less time than on main.

Smaller edits of an empty tree take the same path. Against main, edits
of 12, 13, and 48 items take 22–53% less time on every machine, and
edits of 1 and 4 items take 4–34% less on Linux. On Windows and macOS,
edits of 1 and 4 items take 5–24% less with `Arc<str>` keys. With `u64`
keys they take between 12% less and 2% more on Windows, and between 3%
less and 10% more on macOS.

## Testing

- `test_edit` now checks that an edit of an empty tree packs its leaves
as `from_iter` does. It fails if that edit inserts in place.
- `test_inserts_in_place` starts each tree with one item, so its inserts
still take the in-place path. It still fails for each of 19 mutations of
that code.
- `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.
- `./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:

- N/A
2026-11-24 19:40:24 +00:00
..
…