Reduce display_map snapshot creation (#39354)

Re-applies https://github.com/zed-industries/zed/pull/30840

This PR re-applies the initial
[PR](https://github.com/zed-industries/zed/pull/30840). As it was closed
because it was hard to land, because of the many conflicts. This PR
re-applies the changes for it.

In several cases we were creating multiple display_map
snapshots within the same root-level function call.
Creating a display_map snapshot is quite slow, and in some
cases we were creating the snapshot multiple times.

Release Notes:

- N/A
This commit is contained in:
Remco Smits
2025-10-17 21:56:57 +02:00
committed by GitHub
parent ef5b8c6fed
commit 7e97fcaacb
60 changed files with 853 additions and 426 deletions
+1 -1
View File
@@ -199,7 +199,7 @@ impl Vim {
let mut ranges = Vec::new();
let mut cursor_positions = Vec::new();
let snapshot = editor.buffer().read(cx).snapshot(cx);
for selection in editor.selections.all_adjusted(cx) {
for selection in editor.selections.all_adjusted(&editor.display_snapshot(cx)) {
match vim.mode {
Mode::Visual | Mode::VisualLine => {
ranges.push(selection.start..selection.end);
+1 -1
View File
@@ -58,7 +58,7 @@ impl Vim {
let mut new_anchors = Vec::new();
let snapshot = editor.buffer().read(cx).snapshot(cx);
for selection in editor.selections.all_adjusted(cx) {
for selection in editor.selections.all_adjusted(&editor.display_snapshot(cx)) {
if !selection.is_empty()
&& (vim.mode != Mode::VisualBlock || new_anchors.is_empty())
{
+18 -13
View File
@@ -50,16 +50,19 @@ impl Vim {
let mut reversed = vec![];
self.update_editor(cx, |vim, editor, cx| {
let (map, selections) = editor.selections.all_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_display(&display_map);
for selection in selections {
let end = movement::saturating_left(&map, selection.end);
let end = movement::saturating_left(&display_map, selection.end);
ends.push(
map.buffer_snapshot()
.anchor_before(end.to_offset(&map, Bias::Left)),
display_map
.buffer_snapshot()
.anchor_before(end.to_offset(&display_map, Bias::Left)),
);
starts.push(
map.buffer_snapshot()
.anchor_before(selection.start.to_offset(&map, Bias::Left)),
display_map
.buffer_snapshot()
.anchor_before(selection.start.to_offset(&display_map, Bias::Left)),
);
reversed.push(selection.reversed)
}
@@ -301,19 +304,21 @@ impl Vim {
name = "'";
}
if matches!(name, "{" | "}" | "(" | ")") {
let (map, selections) = editor.selections.all_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_display(&display_map);
let anchors = selections
.into_iter()
.map(|selection| {
let point = match name {
"{" => movement::start_of_paragraph(&map, selection.head(), 1),
"}" => movement::end_of_paragraph(&map, selection.head(), 1),
"(" => motion::sentence_backwards(&map, selection.head(), 1),
")" => motion::sentence_forwards(&map, selection.head(), 1),
"{" => movement::start_of_paragraph(&display_map, selection.head(), 1),
"}" => movement::end_of_paragraph(&display_map, selection.head(), 1),
"(" => motion::sentence_backwards(&display_map, selection.head(), 1),
")" => motion::sentence_forwards(&display_map, selection.head(), 1),
_ => unreachable!(),
};
map.buffer_snapshot()
.anchor_before(point.to_offset(&map, Bias::Left))
display_map
.buffer_snapshot()
.anchor_before(point.to_offset(&display_map, Bias::Left))
})
.collect::<Vec<Anchor>>();
return Some(Mark::Local(anchors));
+3 -2
View File
@@ -56,7 +56,8 @@ impl Vim {
vim.copy_selections_content(editor, MotionKind::for_mode(vim.mode), window, cx);
}
let (display_map, current_selections) = editor.selections.all_adjusted_display(cx);
let display_map = editor.display_snapshot(cx);
let current_selections = editor.selections.all_adjusted_display(&display_map);
// unlike zed, if you have a multi-cursor selection from vim block mode,
// pasting it will paste it on subsequent lines, even if you don't yet
@@ -173,7 +174,7 @@ impl Vim {
original_indent_columns.push(original_indent_column);
}
let cursor_offset = editor.selections.last::<usize>(cx).head();
let cursor_offset = editor.selections.last::<usize>(&display_map).head();
if editor
.buffer()
.read(cx)
+8 -2
View File
@@ -363,7 +363,10 @@ mod test {
point(0., 3.0)
);
assert_eq!(
editor.selections.newest(cx).range(),
editor
.selections
.newest(&editor.display_snapshot(cx))
.range(),
Point::new(6, 0)..Point::new(6, 0)
)
});
@@ -380,7 +383,10 @@ mod test {
point(0., 3.0)
);
assert_eq!(
editor.selections.newest(cx).range(),
editor
.selections
.newest(&editor.display_snapshot(cx))
.range(),
Point::new(0, 0)..Point::new(6, 1)
)
});
+4 -1
View File
@@ -94,7 +94,10 @@ impl Vim {
MotionKind::Exclusive
};
vim.copy_selections_content(editor, kind, window, cx);
let selections = editor.selections.all::<Point>(cx).into_iter();
let selections = editor
.selections
.all::<Point>(&editor.display_snapshot(cx))
.into_iter();
let edits = selections.map(|selection| (selection.start..selection.end, ""));
editor.edit(edits, cx);
});
+2 -2
View File
@@ -106,7 +106,7 @@ impl Vim {
true,
editor
.selections
.all_adjusted(cx)
.all_adjusted(&editor.display_snapshot(cx))
.iter()
.map(|s| s.range())
.collect(),
@@ -128,7 +128,7 @@ impl Vim {
false,
editor
.selections
.all_adjusted(cx)
.all_adjusted(&editor.display_snapshot(cx))
.iter()
.map(|s| s.range())
.collect(),