git: Implement branch diff line counts efficiently (#52582)

Previously we were iterating over all hunks across all diffs on every
frame. Now we can read off the required information as a `SumTree`
summary in constant time.

Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [ ] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- git: Fixed bad performance in large branch diffs.
This commit is contained in:
Cole Miller authored and GitHub committed 2026-03-30 14:23:02 +00:00
1 parent 2db358d559
commit 7c19d7cbf8
5 files changed
+153 -116

No files matched your search

+1 -32
View File
@@ -414,38 +414,7 @@ impl CommitView {
}
fn calculate_changed_lines(&self, cx: &App) -> (u32, u32) {
let snapshot = self.multibuffer.read(cx).snapshot(cx);
let mut total_additions = 0u32;
let mut total_deletions = 0u32;
let mut seen_buffers = std::collections::HashSet::new();
for (_, buffer, _) in snapshot.excerpts() {
let buffer_id = buffer.remote_id();
if !seen_buffers.insert(buffer_id) {
continue;
}
let Some(diff) = snapshot.diff_for_buffer_id(buffer_id) else {
continue;
};
let base_text = diff.base_text();
for hunk in diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, buffer) {
let added_rows = hunk.range.end.row.saturating_sub(hunk.range.start.row);
total_additions += added_rows;
let base_start = base_text
.offset_to_point(hunk.diff_base_byte_range.start)
.row;
let base_end = base_text.offset_to_point(hunk.diff_base_byte_range.end).row;
let deleted_rows = base_end.saturating_sub(base_start);
total_deletions += deleted_rows;
}
}
(total_additions, total_deletions)
self.multibuffer.read(cx).snapshot(cx).total_changed_lines()
}
fn render_header(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {