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
+5 -4
View File
@@ -50,7 +50,8 @@ impl Vim {
pub(crate) fn push_to_change_list(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some((new_positions, buffer)) = self.update_editor(cx, |vim, editor, cx| {
let (map, selections) = editor.selections.all_adjusted_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_adjusted_display(&display_map);
let buffer = editor.buffer().clone();
let pop_state = editor
@@ -59,7 +60,7 @@ impl Vim {
.map(|previous| {
previous.len() == selections.len()
&& previous.iter().enumerate().all(|(ix, p)| {
p.to_display_point(&map).row() == selections[ix].head().row()
p.to_display_point(&display_map).row() == selections[ix].head().row()
})
})
.unwrap_or(false);
@@ -68,11 +69,11 @@ impl Vim {
.into_iter()
.map(|s| {
let point = if vim.mode == Mode::Insert {
movement::saturating_left(&map, s.head())
movement::saturating_left(&display_map, s.head())
} else {
s.head()
};
map.display_point_to_anchor(point, Bias::Left)
display_map.display_point_to_anchor(point, Bias::Left)
})
.collect::<Vec<_>>();
+17 -5
View File
@@ -606,7 +606,9 @@ pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
let result = vim.update_editor(cx, |vim, editor, cx| {
let snapshot = editor.snapshot(window, cx);
let buffer_row = action.range.head().buffer_row(vim, editor, window, cx)?;
let current = editor.selections.newest::<Point>(cx);
let current = editor
.selections
.newest::<Point>(&editor.display_snapshot(cx));
let target = snapshot
.buffer_snapshot()
.clip_point(Point::new(buffer_row.0, current.head().column), Bias::Left);
@@ -1903,7 +1905,9 @@ impl OnMatchingLines {
});
window.dispatch_action(action, cx);
cx.defer_in(window, move |editor, window, cx| {
let newest = editor.selections.newest::<Point>(cx);
let newest = editor
.selections
.newest::<Point>(&editor.display_snapshot(cx));
editor.change_selections(
SelectionEffects::no_scroll(),
window,
@@ -2000,7 +2004,9 @@ impl Vim {
};
let command = self.update_editor(cx, |_, editor, cx| {
let snapshot = editor.snapshot(window, cx);
let start = editor.selections.newest_display(cx);
let start = editor
.selections
.newest_display(&editor.display_snapshot(cx));
let text_layout_details = editor.text_layout_details(window);
let (mut range, _) = motion
.range(
@@ -2047,7 +2053,9 @@ impl Vim {
};
let command = self.update_editor(cx, |_, editor, cx| {
let snapshot = editor.snapshot(window, cx);
let start = editor.selections.newest_display(cx);
let start = editor
.selections
.newest_display(&editor.display_snapshot(cx));
let range = object
.range(&snapshot, start.clone(), around, None)
.unwrap_or(start.range());
@@ -2156,7 +2164,11 @@ impl ShellExec {
Point::new(range.start.0, 0)
..snapshot.clip_point(Point::new(range.end.0 + 1, 0), Bias::Right)
} else {
let mut end = editor.selections.newest::<Point>(cx).range().end;
let mut end = editor
.selections
.newest::<Point>(&editor.display_snapshot(cx))
.range()
.end;
end = snapshot.clip_point(Point::new(end.row + 1, 0), Bias::Right);
needs_newline_prefix = end == snapshot.max_point();
end..end
+17 -11
View File
@@ -345,7 +345,7 @@ impl Vim {
self.update_editor(cx, |vim, editor, cx| {
let has_selection = editor
.selections
.all_adjusted(cx)
.all_adjusted(&editor.display_snapshot(cx))
.iter()
.any(|selection| !selection.is_empty());
@@ -478,19 +478,20 @@ impl Vim {
pub fn helix_replace(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) {
self.update_editor(cx, |_, editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
let (map, selections) = editor.selections.all_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_display(&display_map);
// Store selection info for positioning after edit
let selection_info: Vec<_> = selections
.iter()
.map(|selection| {
let range = selection.range();
let start_offset = range.start.to_offset(&map, Bias::Left);
let end_offset = range.end.to_offset(&map, Bias::Left);
let start_offset = range.start.to_offset(&display_map, Bias::Left);
let end_offset = range.end.to_offset(&display_map, Bias::Left);
let was_empty = range.is_empty();
let was_reversed = selection.reversed;
(
map.buffer_snapshot().anchor_before(start_offset),
display_map.buffer_snapshot().anchor_before(start_offset),
end_offset - start_offset,
was_empty,
was_reversed,
@@ -504,11 +505,11 @@ impl Vim {
// For empty selections, extend to replace one character
if range.is_empty() {
range.end = movement::saturating_right(&map, range.start);
range.end = movement::saturating_right(&display_map, range.start);
}
let byte_range = range.start.to_offset(&map, Bias::Left)
..range.end.to_offset(&map, Bias::Left);
let byte_range = range.start.to_offset(&display_map, Bias::Left)
..range.end.to_offset(&display_map, Bias::Left);
if !byte_range.is_empty() {
let replacement_text = text.repeat(byte_range.len());
@@ -568,7 +569,7 @@ impl Vim {
self.update_editor(cx, |_, editor, cx| {
editor.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
let display_map = editor.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = editor.selections.all::<Point>(cx);
let mut selections = editor.selections.all::<Point>(&display_map);
let max_point = display_map.buffer_snapshot().max_point();
let buffer_snapshot = &display_map.buffer_snapshot();
@@ -606,7 +607,9 @@ impl Vim {
cx: &mut Context<Self>,
) {
self.update_editor(cx, |_, editor, cx| {
let newest = editor.selections.newest::<usize>(cx);
let newest = editor
.selections
.newest::<usize>(&editor.display_snapshot(cx));
editor.change_selections(Default::default(), window, cx, |s| s.select(vec![newest]));
});
}
@@ -633,7 +636,10 @@ impl Vim {
if yank {
vim.copy_selections_content(editor, MotionKind::Exclusive, 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 -1
View File
@@ -56,7 +56,8 @@ impl Vim {
let times = times.unwrap_or(1);
self.update_editor(cx, |_, editor, cx| {
let mut selections = Vec::new();
let (map, mut original_selections) = editor.selections.all_display(cx);
let map = editor.display_snapshot(cx);
let mut original_selections = editor.selections.all_display(&map);
// The order matters, because it is recorded when the selections are added.
if above {
original_selections.reverse();
+2 -1
View File
@@ -44,7 +44,8 @@ impl Vim {
return;
};
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);
// The clipboard can have multiple selections, and there can
// be multiple selections. Helix zips them together, so the first
+1 -1
View File
@@ -84,7 +84,7 @@ impl Vim {
self.update_editor(cx, |_, editor, cx| {
let snapshot = editor.buffer().read(cx).snapshot(cx);
let mut edits = Vec::new();
for selection in editor.selections.all::<Point>(cx) {
for selection in editor.selections.all::<Point>(&editor.display_snapshot(cx)) {
let point = selection.head();
let new_row = match direction {
Direction::Next => point.row + 1,
+15 -13
View File
@@ -657,7 +657,7 @@ impl Vim {
self.switch_mode(Mode::Insert, false, window, cx);
self.update_editor(cx, |_, editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
let selections = editor.selections.all::<Point>(cx);
let selections = editor.selections.all::<Point>(&editor.display_snapshot(cx));
let snapshot = editor.buffer().read(cx).snapshot(cx);
let selection_start_rows: BTreeSet<u32> = selections
@@ -699,7 +699,7 @@ impl Vim {
self.update_editor(cx, |_, editor, cx| {
let text_layout_details = editor.text_layout_details(window);
editor.transact(window, cx, |editor, window, cx| {
let selections = editor.selections.all::<Point>(cx);
let selections = editor.selections.all::<Point>(&editor.display_snapshot(cx));
let snapshot = editor.buffer().read(cx).snapshot(cx);
let selection_end_rows: BTreeSet<u32> = selections
@@ -745,7 +745,7 @@ impl Vim {
Vim::take_forced_motion(cx);
self.update_editor(cx, |_, editor, cx| {
editor.transact(window, cx, |editor, _, cx| {
let selections = editor.selections.all::<Point>(cx);
let selections = editor.selections.all::<Point>(&editor.display_snapshot(cx));
let selection_start_rows: BTreeSet<u32> = selections
.into_iter()
@@ -774,9 +774,10 @@ impl Vim {
Vim::take_forced_motion(cx);
self.update_editor(cx, |_, editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
let selections = editor.selections.all::<Point>(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all::<Point>(&display_map);
let snapshot = editor.buffer().read(cx).snapshot(cx);
let (_map, display_selections) = editor.selections.all_display(cx);
let display_selections = editor.selections.all_display(&display_map);
let original_positions = display_selections
.iter()
.map(|s| (s.id, s.head()))
@@ -937,13 +938,14 @@ impl Vim {
self.update_editor(cx, |_, editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let (map, display_selections) = editor.selections.all_display(cx);
let display_map = editor.display_snapshot(cx);
let display_selections = editor.selections.all_display(&display_map);
let mut edits = Vec::new();
let mut edits = Vec::with_capacity(display_selections.len());
for selection in &display_selections {
let mut range = selection.range();
for _ in 0..count {
let new_point = movement::saturating_right(&map, range.end);
let new_point = movement::saturating_right(&display_map, range.end);
if range.end == new_point {
return;
}
@@ -951,8 +953,8 @@ impl Vim {
}
edits.push((
range.start.to_offset(&map, Bias::Left)
..range.end.to_offset(&map, Bias::Left),
range.start.to_offset(&display_map, Bias::Left)
..range.end.to_offset(&display_map, Bias::Left),
text.repeat(if is_return_char { 0 } else { count }),
));
}
@@ -976,16 +978,16 @@ impl Vim {
pub fn save_selection_starts(
&self,
editor: &Editor,
cx: &mut Context<Editor>,
) -> HashMap<usize, Anchor> {
let (map, selections) = editor.selections.all_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_display(&display_map);
selections
.iter()
.map(|selection| {
(
selection.id,
map.display_point_to_anchor(selection.start, Bias::Right),
display_map.display_point_to_anchor(selection.start, Bias::Right),
)
})
.collect::<HashMap<_, _>>()
+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(),
+8 -4
View File
@@ -53,7 +53,7 @@ impl Vim {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let map = editor.snapshot(window, cx);
let display_selections = editor.selections.all::<Point>(cx);
let display_selections = editor.selections.all::<Point>(&map.display_snapshot);
// Handles all string that require manipulation, including inserts and replaces
let edits = display_selections
@@ -98,7 +98,7 @@ impl Vim {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let map = editor.snapshot(window, cx);
let selections = editor.selections.all::<Point>(cx);
let selections = editor.selections.all::<Point>(&map.display_snapshot);
let mut new_selections = vec![];
let edits: Vec<(Range<Point>, String)> = selections
.into_iter()
@@ -150,7 +150,9 @@ impl Vim {
self.stop_recording(cx);
self.update_editor(cx, |vim, editor, cx| {
editor.set_clip_at_line_ends(false, cx);
let mut selection = editor.selections.newest_display(cx);
let mut selection = editor
.selections
.newest_display(&editor.display_snapshot(cx));
let snapshot = editor.snapshot(window, cx);
object.expand_selection(&snapshot, &mut selection, around, None);
let start = snapshot
@@ -196,7 +198,9 @@ impl Vim {
self.update_editor(cx, |vim, editor, cx| {
editor.set_clip_at_line_ends(false, cx);
let text_layout_details = editor.text_layout_details(window);
let mut selection = editor.selections.newest_display(cx);
let mut selection = editor
.selections
.newest_display(&editor.display_snapshot(cx));
let snapshot = editor.snapshot(window, cx);
motion.expand_selection(
&snapshot,
+3 -1
View File
@@ -863,7 +863,9 @@ impl VimGlobals {
}
}
'%' => editor.and_then(|editor| {
let selection = editor.selections.newest::<Point>(cx);
let selection = editor
.selections
.newest::<Point>(&editor.display_snapshot(cx));
if let Some((_, buffer, _)) = editor
.buffer()
.read(cx)
+10 -5
View File
@@ -45,7 +45,8 @@ impl Vim {
},
};
let surround = pair.end != surround_alias((*text).as_ref());
let (display_map, display_selections) = editor.selections.all_adjusted_display(cx);
let display_map = editor.display_snapshot(cx);
let display_selections = editor.selections.all_adjusted_display(&display_map);
let mut edits = Vec::new();
let mut anchors = Vec::new();
@@ -144,7 +145,8 @@ impl Vim {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let (display_map, display_selections) = editor.selections.all_display(cx);
let display_map = editor.display_snapshot(cx);
let display_selections = editor.selections.all_display(&display_map);
let mut edits = Vec::new();
let mut anchors = Vec::new();
@@ -256,7 +258,8 @@ impl Vim {
let preserve_space =
will_replace_pair.start == will_replace_pair.end || !opening;
let (display_map, selections) = editor.selections.all_adjusted_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_adjusted_display(&display_map);
let mut edits = Vec::new();
let mut anchors = Vec::new();
@@ -382,7 +385,8 @@ impl Vim {
self.update_editor(cx, |_, editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let (display_map, selections) = editor.selections.all_adjusted_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_adjusted_display(&display_map);
let mut anchors = Vec::new();
for selection in &selections {
@@ -500,7 +504,8 @@ impl Vim {
let mut min_range_size = usize::MAX;
let _ = self.editor.update(cx, |editor, cx| {
let (display_map, selections) = editor.selections.all_adjusted_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_adjusted_display(&display_map);
// Even if there's multiple cursors, we'll simply rely on
// the first one to understand what bracket pair to map to.
// I believe we could, if worth it, go one step above and
+4 -1
View File
@@ -2295,7 +2295,10 @@ async fn test_clipping_on_mode_change(cx: &mut gpui::TestAppContext) {
let mut pixel_position = cx.update_editor(|editor, window, cx| {
let snapshot = editor.snapshot(window, cx);
let current_head = editor.selections.newest_display(cx).end;
let current_head = editor
.selections
.newest_display(&snapshot.display_snapshot)
.end;
editor.last_bounds().unwrap().origin
+ editor
.display_to_pixel_point(current_head, &snapshot, window)
+12 -5
View File
@@ -1359,7 +1359,10 @@ impl Vim {
return;
};
let newest_selection_empty = editor.update(cx, |editor, cx| {
editor.selections.newest::<usize>(cx).is_empty()
editor
.selections
.newest::<usize>(&editor.display_snapshot(cx))
.is_empty()
});
let editor = editor.read(cx);
let editor_mode = editor.mode();
@@ -1455,9 +1458,11 @@ impl Vim {
cx: &mut Context<Self>,
) -> Option<String> {
self.update_editor(cx, |_, editor, cx| {
let selection = editor.selections.newest::<usize>(cx);
let snapshot = &editor.snapshot(window, cx);
let selection = editor
.selections
.newest::<usize>(&snapshot.display_snapshot);
let snapshot = editor.snapshot(window, cx);
let snapshot = snapshot.buffer_snapshot();
let (range, kind) =
snapshot.surrounding_word(selection.start, Some(CharScopeContext::Completion));
@@ -1484,9 +1489,11 @@ impl Vim {
let selections = self.editor().map(|editor| {
editor.update(cx, |editor, cx| {
let snapshot = editor.display_snapshot(cx);
(
editor.selections.oldest::<Point>(cx),
editor.selections.newest::<Point>(cx),
editor.selections.oldest::<Point>(&snapshot),
editor.selections.newest::<Point>(&snapshot),
)
})
});
+8 -3
View File
@@ -747,7 +747,8 @@ impl Vim {
self.stop_recording(cx);
self.update_editor(cx, |_, editor, cx| {
editor.transact(window, cx, |editor, window, cx| {
let (display_map, selections) = editor.selections.all_adjusted_display(cx);
let display_map = editor.display_snapshot(cx);
let selections = editor.selections.all_adjusted_display(&display_map);
// Selections are biased right at the start. So we need to store
// anchors that are biased left so that we can restore the selections
@@ -858,7 +859,9 @@ impl Vim {
});
}
self.update_editor(cx, |_, editor, cx| {
let latest = editor.selections.newest::<usize>(cx);
let latest = editor
.selections
.newest::<usize>(&editor.display_snapshot(cx));
start_selection = latest.start;
end_selection = latest.end;
});
@@ -879,7 +882,9 @@ impl Vim {
return;
}
self.update_editor(cx, |_, editor, cx| {
let latest = editor.selections.newest::<usize>(cx);
let latest = editor
.selections
.newest::<usize>(&editor.display_snapshot(cx));
if vim_is_normal {
start_selection = latest.start;
end_selection = latest.end;