From 535c949a1ac92901135ed8ea1b691162c6b7d218 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Wed, 5 Mar 2025 12:41:37 -0500 Subject: [PATCH] Fix performance regression in multibuffer diff syncing (cherry-pick #26137) (#26139) Cherry-picked Fix performance regression in multibuffer diff syncing (#26137) This fixes a performance problem introduced in #25906 and caused by calling `BufferDiff::snapshot` too frequently. Release Notes: - Fixed a performance regression related to buffer diffs Co-authored-by: Conrad Co-authored-by: Cole Miller Co-authored-by: Conrad --- crates/multi_buffer/src/multi_buffer.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs index dc754cfcd51f6861edcf0187b452b565a677c482..c8efc4ac7274a2f6f696f2e99eb044a9f0329c7e 100644 --- a/crates/multi_buffer/src/multi_buffer.rs +++ b/crates/multi_buffer/src/multi_buffer.rs @@ -2045,6 +2045,7 @@ impl MultiBuffer { .cursor::<(Option<&Locator>, ExcerptOffset)>(&()); let mut edits = Vec::new(); let mut excerpt_ids = ids.iter().copied().peekable(); + let mut removed_buffer_ids = Vec::new(); while let Some(excerpt_id) = excerpt_ids.next() { // Seek to the next excerpt to remove, preserving any preceding excerpts. @@ -2067,7 +2068,7 @@ impl MultiBuffer { excerpt.buffer_id ); buffers.remove(&excerpt.buffer_id); - self.diffs.remove(&excerpt.buffer_id); + removed_buffer_ids.push(excerpt.buffer_id); } } cursor.next(&()); @@ -2108,6 +2109,10 @@ impl MultiBuffer { new_excerpts.append(suffix, &()); drop(cursor); snapshot.excerpts = new_excerpts; + for buffer_id in removed_buffer_ids { + self.diffs.remove(&buffer_id); + snapshot.diffs.remove(&buffer_id); + } if changed_trailing_excerpt { snapshot.trailing_excerpt_update_count += 1; @@ -2721,9 +2726,10 @@ impl MultiBuffer { snapshot.has_deleted_file = has_deleted_file; snapshot.has_conflict = has_conflict; - snapshot.diffs.retain(|_, _| false); for (id, diff) in self.diffs.iter() { - snapshot.diffs.insert(*id, diff.diff.read(cx).snapshot(cx)); + if snapshot.diffs.get(&id).is_none() { + snapshot.diffs.insert(*id, diff.diff.read(cx).snapshot(cx)); + } } excerpts_to_edit.sort_unstable_by_key(|(locator, _, _)| *locator);