Add editor bench mark for multi cursor edits

This commit is contained in:
Anthony 2025-07-14 12:00:04 -04:00
parent 63d01c299d
commit 5fe05007a4
3 changed files with 86 additions and 19 deletions

View file

@ -1,5 +1,8 @@
use criterion::{Bencher, BenchmarkId}; use criterion::{Bencher, BenchmarkId};
use editor::{Editor, EditorMode, MultiBuffer, actions::MoveDown}; use editor::{
Editor, EditorMode, MultiBuffer,
actions::{DeleteToPreviousWordStart, SelectAll, SplitSelectionIntoLines},
};
use gpui::{AppContext, Focusable as _, TestAppContext, TestDispatcher}; use gpui::{AppContext, Focusable as _, TestAppContext, TestDispatcher};
use project::Project; use project::Project;
use rand::{Rng as _, SeedableRng as _, rngs::StdRng}; use rand::{Rng as _, SeedableRng as _, rngs::StdRng};
@ -7,17 +10,65 @@ use settings::SettingsStore;
use ui::IntoElement; use ui::IntoElement;
use util::RandomCharIter; use util::RandomCharIter;
fn editor_with_one_long_line(_bencher: &mut Bencher<'_>, args: &(String, TestAppContext)) { fn editor_input_with_1000_cursors(bencher: &mut Bencher<'_>, cx: &TestAppContext) {
let (text, cx) = args;
let mut cx = cx.clone(); let mut cx = cx.clone();
let text = String::from_iter(["line:\n"; 500]);
let buffer = cx.update(|cx| MultiBuffer::build_simple(&text, cx)); let buffer = cx.update(|cx| MultiBuffer::build_simple(&text, cx));
let cx = cx.add_empty_window(); let cx = cx.add_empty_window();
let _editor = cx.update(|window, cx| { let editor = cx.update(|window, cx| {
let editor = cx.new(|cx| Editor::new(EditorMode::full(), buffer, None, window, cx)); let editor = cx.new(|cx| {
let mut editor = Editor::new(EditorMode::full(), buffer, None, window, cx);
editor.set_style(editor::EditorStyle::default(), window, cx);
editor.select_all(&SelectAll, window, cx);
editor.split_selection_into_lines(&SplitSelectionIntoLines, window, cx);
editor
});
window.focus(&editor.focus_handle(cx)); window.focus(&editor.focus_handle(cx));
editor editor
}); });
bencher.iter(|| {
cx.update(|window, cx| {
editor.update(cx, |editor, cx| {
editor.handle_input("hello world", window, cx);
editor.delete_to_previous_word_start(
&DeleteToPreviousWordStart {
ignore_newlines: false,
},
window,
cx,
);
editor.delete_to_previous_word_start(
&DeleteToPreviousWordStart {
ignore_newlines: false,
},
window,
cx,
);
});
})
});
}
fn open_editor_with_one_long_line(bencher: &mut Bencher<'_>, args: &(String, TestAppContext)) {
let (text, cx) = args;
let mut cx = cx.clone();
bencher.iter(|| {
let buffer = cx.update(|cx| MultiBuffer::build_simple(&text, cx));
let cx = cx.add_empty_window();
let _ = cx.update(|window, cx| {
let editor = cx.new(|cx| {
let mut editor = Editor::new(EditorMode::full(), buffer, None, window, cx);
editor.set_style(editor::EditorStyle::default(), window, cx);
editor
});
window.focus(&editor.focus_handle(cx));
editor
});
});
} }
fn editor_render(bencher: &mut Bencher<'_>, cx: &TestAppContext) { fn editor_render(bencher: &mut Bencher<'_>, cx: &TestAppContext) {
@ -37,14 +88,18 @@ fn editor_render(bencher: &mut Bencher<'_>, cx: &TestAppContext) {
let cx = cx.add_empty_window(); let cx = cx.add_empty_window();
let editor = cx.update(|window, cx| { let editor = cx.update(|window, cx| {
let editor = cx.new(|cx| Editor::new(EditorMode::full(), buffer, None, window, cx)); let editor = cx.new(|cx| {
let mut editor = Editor::new(EditorMode::full(), buffer, None, window, cx);
editor.set_style(editor::EditorStyle::default(), window, cx);
editor
});
window.focus(&editor.focus_handle(cx)); window.focus(&editor.focus_handle(cx));
editor editor
}); });
bencher.iter(|| { bencher.iter(|| {
cx.update(|window, cx| { cx.update(|window, cx| {
editor.update(cx, |editor, cx| editor.move_down(&MoveDown, window, cx)); // editor.update(cx, |editor, cx| editor.move_down(&MoveDown, window, cx));
let mut view = editor.clone().into_any_element(); let mut view = editor.clone().into_any_element();
let _ = view.request_layout(window, cx); let _ = view.request_layout(window, cx);
let _ = view.prepaint(window, cx); let _ = view.prepaint(window, cx);
@ -73,18 +128,32 @@ pub fn benches() {
(criterion::Criterion::default()).configure_from_args(); (criterion::Criterion::default()).configure_from_args();
// setup app context // setup app context
criterion.bench_with_input( let mut group = criterion.benchmark_group("Time to render");
group.bench_with_input(
BenchmarkId::new("editor_render", "TestAppContext"), BenchmarkId::new("editor_render", "TestAppContext"),
&cx, &cx,
editor_render, editor_render,
); );
let text = String::from_iter(["char"; 100000]); group.finish();
criterion.bench_with_input(
let text = String::from_iter(["char"; 1000]);
let mut group = criterion.benchmark_group("Build buffer with one long line");
group.bench_with_input(
BenchmarkId::new("editor_with_one_long_line", "(String, TestAppContext )"), BenchmarkId::new("editor_with_one_long_line", "(String, TestAppContext )"),
&(text, cx), &(text, cx.clone()),
editor_with_one_long_line, open_editor_with_one_long_line,
); );
group.finish();
let mut group = criterion.benchmark_group("multi cursor edits");
group.bench_with_input(
BenchmarkId::new("editor_input_with_1000_cursors", "TestAppContext"),
&cx,
editor_input_with_1000_cursors,
);
group.finish();
} }
fn main() { fn main() {

View file

@ -748,6 +748,7 @@ mod tests {
) )
} }
} }
#[gpui::test] #[gpui::test]
fn test_expand_tabs(cx: &mut gpui::App) { fn test_expand_tabs(cx: &mut gpui::App) {
let test_values = [ let test_values = [
@ -1461,9 +1462,11 @@ impl<'a> TabStopCursor<'a> {
} }
fn is_char_boundary(&self) -> bool { fn is_char_boundary(&self) -> bool {
// FIXME: if idx is 128 should we return false or be at the next chunk?
// idx might also be 1-indexed instead of 0-indexed, need to double check
self.current_chunk self.current_chunk
.as_ref() .as_ref()
.is_some_and(|(chunk, idx)| (chunk.chars & (1 << idx)) != 0) .is_some_and(|(chunk, idx)| (chunk.chars & (1 << *idx.min(&127))) != 0)
} }
/// distance: length to move forward while searching for the next tab stop /// distance: length to move forward while searching for the next tab stop

View file

@ -18090,12 +18090,7 @@ impl Editor {
} }
/// called by the Element so we know what style we were most recently rendered with. /// called by the Element so we know what style we were most recently rendered with.
pub(crate) fn set_style( pub fn set_style(&mut self, style: EditorStyle, window: &mut Window, cx: &mut Context<Self>) {
&mut self,
style: EditorStyle,
window: &mut Window,
cx: &mut Context<Self>,
) {
// We intentionally do not inform the display map about the minimap style // We intentionally do not inform the display map about the minimap style
// so that wrapping is not recalculated and stays consistent for the editor // so that wrapping is not recalculated and stays consistent for the editor
// and its linked minimap. // and its linked minimap.