History

multi_buffer: Optimize a bunch of things (#48519)

Release Notes: - N/A *or* Added/Fixed/Improved ... ---------

Lukas Wirth committed 9 months ago

Commit 74746aa1 parent 7dcff2112 files · +256 −157Browse files

Showing the changes to crates/buffer_diff/src/buffer_diff.rs (1 of 12 files). Show all

Unified · Split

crates/buffer_diff/src/buffer_diff.rs+34−35
@@ -30,7 +30,7 @@ pub struct BufferDiff { · 29 unmodified lines
3030 #[derive(Clone)]
3131 pub struct BufferDiffSnapshot {
3232 inner: BufferDiffInner<language::BufferSnapshot>,
33− secondary_diff: Option<Box<BufferDiffSnapshot>>,
33+ secondary_diff: Option<Arc<BufferDiffSnapshot>>,
3434 }
3535
3636 impl std::fmt::Debug for BufferDiffSnapshot {
@@ -222,11 +222,11 @@ impl std::fmt::Debug for BufferDiffInner<language::BufferSnapshot> { · 185 unmodified lines
222222 impl BufferDiffSnapshot {
223223 #[cfg(test)]
224224 fn new_sync(
225− buffer: text::BufferSnapshot,
225+ buffer: &text::BufferSnapshot,
226226 diff_base: String,
227227 cx: &mut gpui::TestAppContext,
228228 ) -> BufferDiffSnapshot {
229− let buffer_diff = cx.new(|cx| BufferDiff::new_with_base_text(&diff_base, &buffer, cx));
229+ let buffer_diff = cx.new(|cx| BufferDiff::new_with_base_text(&diff_base, buffer, cx));
230230 buffer_diff.update(cx, |buffer_diff, cx| buffer_diff.snapshot(cx))
231231 }
232232
@@ -950,10 +950,10 @@ fn build_diff_options( · 717 unmodified lines
950950
951951 fn compute_hunks(
952952 diff_base: Option<(Arc<str>, Rope)>,
953− buffer: text::BufferSnapshot,
953+ buffer: &text::BufferSnapshot,
954954 diff_options: Option<DiffOptions>,
955955 ) -> SumTree<InternalDiffHunk> {
956− let mut tree = SumTree::new(&buffer);
956+ let mut tree = SumTree::new(buffer);
957957
958958 if let Some((diff_base, diff_base_rope)) = diff_base {
959959 let buffer_text = buffer.as_rope().to_string();
@@ -980,7 +980,7 @@ fn compute_hunks( · 20 unmodified lines
980980 base_word_diffs: Vec::default(),
981981 buffer_word_diffs: Vec::default(),
982982 },
983− &buffer,
983+ buffer,
984984 );
985985 return tree;
986986 }
@@ -992,11 +992,11 @@ fn compute_hunks( · 5 unmodified lines
992992 &patch,
993993 hunk_index,
994994 &diff_base_rope,
995− &buffer,
995+ buffer,
996996 &mut divergence,
997997 diff_options.as_ref(),
998998 );
999− tree.push(hunk, &buffer);
999+ tree.push(hunk, buffer);
10001000 }
10011001 }
10021002 } else {
@@ -1007,7 +1007,7 @@ fn compute_hunks( · 4 unmodified lines
10071007 base_word_diffs: Vec::default(),
10081008 buffer_word_diffs: Vec::default(),
10091009 },
1010− &buffer,
1010+ buffer,
10111011 );
10121012 }
10131013
@@ -1544,7 +1544,7 @@ impl BufferDiff { · 530 unmodified lines
15441544 base_text
15451545 .clone()
15461546 .map(|base_text| (base_text, base_text_rope.clone())),
1547− buffer.clone(),
1547+ &buffer,
15481548 diff_options,
15491549 );
15501550 let base_text = base_text.unwrap_or_default();
@@ -1785,10 +1785,10 @@ impl BufferDiff { · 234 unmodified lines
17851785 base_text_exists: self.inner.base_text_exists,
17861786 buffer_snapshot: self.inner.buffer_snapshot.clone(),
17871787 },
1788− secondary_diff: self
1789− .secondary_diff
1790− .as_ref()
1791− .map(|diff| Box::new(diff.read(cx).snapshot(cx))),
1788+ secondary_diff: self.secondary_diff.as_ref().map(|diff| {
1789+ debug_assert!(diff.read(cx).secondary_diff.is_none());
1790+ Arc::new(diff.read(cx).snapshot(cx))
1791+ }),
17921792 }
17931793 }
17941794
@@ -2018,7 +2018,7 @@ mod tests { · 223 unmodified lines
20182018 .unindent();
20192019
20202020 let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
2021− let mut diff = BufferDiffSnapshot::new_sync(buffer.clone(), diff_base.clone(), cx);
2021+ let mut diff = BufferDiffSnapshot::new_sync(&buffer, diff_base.clone(), cx);
20222022 assert_hunks(
20232023 diff.hunks_intersecting_range(
20242024 Anchor::min_max_range_for_buffer(buffer.remote_id()),
@@ -2030,7 +2030,7 @@ mod tests { · 5 unmodified lines
20302030 );
20312031
20322032 buffer.edit([(0..0, "point five\n")]);
2033− diff = BufferDiffSnapshot::new_sync(buffer.clone(), diff_base.clone(), cx);
2033+ diff = BufferDiffSnapshot::new_sync(&buffer, diff_base.clone(), cx);
20342034 assert_hunks(
20352035 diff.hunks_intersecting_range(
20362036 Anchor::min_max_range_for_buffer(buffer.remote_id()),
@@ -2101,10 +2101,9 @@ mod tests { · 64 unmodified lines
21012101 .unindent();
21022102
21032103 let buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
2104− let unstaged_diff = BufferDiffSnapshot::new_sync(buffer.clone(), index_text, cx);
2105− let mut uncommitted_diff =
2106− BufferDiffSnapshot::new_sync(buffer.clone(), head_text.clone(), cx);
2107− uncommitted_diff.secondary_diff = Some(Box::new(unstaged_diff));
2104+ let unstaged_diff = BufferDiffSnapshot::new_sync(&buffer, index_text, cx);
2105+ let mut uncommitted_diff = BufferDiffSnapshot::new_sync(&buffer, head_text.clone(), cx);
2106+ uncommitted_diff.secondary_diff = Some(Arc::new(unstaged_diff));
21082107
21092108 let expected_hunks = vec![
21102109 (2..3, "two\n", "TWO\n", DiffHunkStatus::modified_none()),
@@ -2631,7 +2630,7 @@ mod tests { · 520 unmodified lines
26312630 let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text_1);
26322631
26332632 let empty_diff = cx.update(|cx| BufferDiff::new(&buffer, cx).snapshot(cx));
2634− let diff_1 = BufferDiffSnapshot::new_sync(buffer.clone(), base_text.clone(), cx);
2633+ let diff_1 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
26352634 let DiffChanged {
26362635 changed_range,
26372636 base_text_changed_range,
@@ -2666,7 +2665,7 @@ mod tests { · 28 unmodified lines
26662665 "
26672666 .unindent(),
26682667 );
2669− let diff_2 = BufferDiffSnapshot::new_sync(buffer.clone(), base_text.clone(), cx);
2668+ let diff_2 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
26702669 let DiffChanged {
26712670 changed_range,
26722671 base_text_changed_range,
@@ -2704,7 +2703,7 @@ mod tests { · 31 unmodified lines
27042703 "
27052704 .unindent(),
27062705 );
2707− let diff_3 = BufferDiffSnapshot::new_sync(buffer.clone(), base_text.clone(), cx);
2706+ let diff_3 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
27082707 let DiffChanged {
27092708 changed_range,
27102709 base_text_changed_range,
@@ -2738,7 +2737,7 @@ mod tests { · 27 unmodified lines
27382737 "
27392738 .unindent(),
27402739 );
2741− let diff_4 = BufferDiffSnapshot::new_sync(buffer.clone(), base_text.clone(), cx);
2740+ let diff_4 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
27422741 let DiffChanged {
27432742 changed_range,
27442743 base_text_changed_range,
@@ -3172,11 +3171,11 @@ mod tests { · 427 unmodified lines
31723171 .unindent();
31733172
31743173 let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
3175− let old_buffer = buffer.snapshot();
3176− let diff_a = BufferDiffSnapshot::new_sync(buffer.clone(), base_text.clone(), cx);
3174+ let old_buffer = buffer.snapshot().clone();
3175+ let diff_a = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
31773176
31783177 buffer.edit([(Point::new(1, 3)..Point::new(1, 3), "\n")]);
3179− let diff_b = BufferDiffSnapshot::new_sync(buffer.clone(), base_text, cx);
3178+ let diff_b = BufferDiffSnapshot::new_sync(&buffer, base_text, cx);
31803179
31813180 let DiffChanged {
31823181 changed_range,
@@ -3235,11 +3234,11 @@ mod tests { · 52 unmodified lines
32353234 .unindent();
32363235
32373236 let mut buffer_2 = Buffer::new(ReplicaId::LOCAL, BufferId::new(2).unwrap(), buffer_text_2);
3238− let old_buffer_2 = buffer_2.snapshot();
3239− let diff_2a = BufferDiffSnapshot::new_sync(buffer_2.clone(), base_text_2.clone(), cx);
3237+ let old_buffer_2 = buffer_2.snapshot().clone();
3238+ let diff_2a = BufferDiffSnapshot::new_sync(&buffer_2, base_text_2.clone(), cx);
32403239
32413240 buffer_2.edit([(Point::new(4, 0)..Point::new(4, 4), "FIVE_CHANGED")]);
3242− let diff_2b = BufferDiffSnapshot::new_sync(buffer_2.clone(), base_text_2, cx);
3241+ let diff_2b = BufferDiffSnapshot::new_sync(&buffer_2, base_text_2, cx);
32433242
32443243 let DiffChanged {
32453244 changed_range,
@@ -3293,7 +3292,7 @@ mod tests { · 47 unmodified lines
32933292 buffer_text_1.to_string(),
32943293 );
32953294
3296− let old_base_snapshot_1 = base_text_buffer.snapshot();
3295+ let old_base_snapshot_1 = base_text_buffer.snapshot().clone();
32973296 let old_hunks_1 = compute_hunks(
32983297 Some((Arc::from(initial_base), Rope::from(initial_base))),
32993298 buffer.snapshot(),
@@ -3352,7 +3351,7 @@ mod tests { · 52 unmodified lines
33523351 buffer_text_2.to_string(),
33533352 );
33543353
3355− let old_base_snapshot_2 = base_buf_2.snapshot();
3354+ let old_base_snapshot_2 = base_buf_2.snapshot().clone();
33563355 let old_hunks_2 = compute_hunks(
33573356 Some((Arc::from(simple_base), Rope::from(simple_base))),
33583357 buffer_2.snapshot(),
@@ -3419,7 +3418,7 @@ mod tests { · 60 unmodified lines
34193418 buffer_text_3.to_string(),
34203419 );
34213420
3422− let old_base_snapshot_3 = base_buf_3.snapshot();
3421+ let old_base_snapshot_3 = base_buf_3.snapshot().clone();
34233422 let old_hunks_3 = compute_hunks(
34243423 Some((Arc::from(base_3), Rope::from(base_3))),
34253424 buffer_3.snapshot(),
@@ -3485,8 +3484,8 @@ mod tests { · 59 unmodified lines
34853484 buffer_text_4.to_string(),
34863485 );
34873486
3488− let old_base_snapshot_4 = base_buf_4.snapshot();
3489− let old_buffer_snapshot_4 = buffer_4.snapshot();
3487+ let old_base_snapshot_4 = base_buf_4.snapshot().clone();
3488+ let old_buffer_snapshot_4 = buffer_4.snapshot().clone();
34903489 let old_hunks_4 = compute_hunks(
34913490 Some((Arc::from(base_4), Rope::from(base_4))),
34923491 buffer_4.snapshot(),