1
0
зеркало из https://github.com/glebtv/tui-textarea.git synced 2026-09-03 14:26:17 +03:00

reduce overhead to build textarea instance using .clone() in benchmarks

Этот коммит содержится в:
rhysd
2022-07-13 09:27:59 +09:00
родитель 421691d602
Коммит 59d5d473d4
2 изменённых файлов: 105 добавлений и 51 удалений

Просмотреть файл

@@ -21,13 +21,20 @@ impl Restore {
}
}
fn run(moves: &[CursorMove], restore: Restore, repeat: usize) -> (usize, usize) {
fn prepare_textarea() -> TextArea<'static> {
let mut lines = Vec::with_capacity(LOREM.len() * 2 + 1);
lines.extend(LOREM.iter().map(|s| s.to_string()));
lines.push("".to_string());
lines.extend(LOREM.iter().map(|s| s.to_string()));
TextArea::new(lines)
}
let mut textarea = TextArea::new(lines);
fn run(
mut textarea: TextArea<'_>,
moves: &[CursorMove],
restore: Restore,
repeat: usize,
) -> (usize, usize) {
let mut term = dummy_terminal();
let mut prev = textarea.cursor();
@@ -48,39 +55,100 @@ fn run(moves: &[CursorMove], restore: Restore, repeat: usize) -> (usize, usize)
}
fn move_char(c: &mut Criterion) {
let textarea = prepare_textarea();
c.bench_function("cursor::char::forward", |b| {
b.iter(|| black_box(run(&[CursorMove::Forward], Restore::TopLeft, 1000)))
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::Forward],
Restore::TopLeft,
1000,
))
})
});
c.bench_function("cursor::char::back", |b| {
b.iter(|| black_box(run(&[CursorMove::Back], Restore::BottomRight, 1000)))
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::Back],
Restore::BottomRight,
1000,
))
})
});
c.bench_function("cursor::char::down", |b| {
b.iter(|| black_box(run(&[CursorMove::Down], Restore::TopLeft, 1000)))
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::Down],
Restore::TopLeft,
1000,
))
})
});
c.bench_function("cursor::char::up", |b| {
b.iter(|| black_box(run(&[CursorMove::Up], Restore::BottomLeft, 1000)))
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::Up],
Restore::BottomLeft,
1000,
))
})
});
}
fn move_word(c: &mut Criterion) {
let textarea = prepare_textarea();
c.bench_function("cursor::word::forward", |b| {
b.iter(|| black_box(run(&[CursorMove::WordForward], Restore::TopLeft, 1000)))
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::WordForward],
Restore::TopLeft,
1000,
))
})
});
c.bench_function("cursor::word::back", |b| {
b.iter(|| black_box(run(&[CursorMove::WordBack], Restore::BottomRight, 1000)))
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::WordBack],
Restore::BottomRight,
1000,
))
})
});
}
fn move_paragraph(c: &mut Criterion) {
let textarea = prepare_textarea();
c.bench_function("cursor::paragraph::down", |b| {
b.iter(|| black_box(run(&[CursorMove::ParagraphForward], Restore::TopLeft, 1000)))
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::ParagraphForward],
Restore::TopLeft,
1000,
))
})
});
c.bench_function("cursor::paragraph::up", |b| {
b.iter(|| black_box(run(&[CursorMove::ParagraphBack], Restore::BottomLeft, 1000)))
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::ParagraphBack],
Restore::BottomLeft,
1000,
))
})
});
}
fn move_edge(c: &mut Criterion) {
let textarea = prepare_textarea();
c.bench_function("cursor::edge::head_end", |b| {
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::End, CursorMove::Head],
Restore::None,
500,
@@ -90,6 +158,7 @@ fn move_edge(c: &mut Criterion) {
c.bench_function("cursor::edge::top_bottom", |b| {
b.iter(|| {
black_box(run(
textarea.clone(),
&[CursorMove::Bottom, CursorMove::Top],
Restore::None,
500,

Просмотреть файл

@@ -2,61 +2,46 @@ use criterion::{criterion_group, criterion_main, Criterion};
use tui_textarea::TextArea;
use tui_textarea_bench::{dummy_terminal, TerminalExt, LOREM};
fn forward(c: &mut Criterion) {
#[inline]
fn run(pat: &str, lines: Vec<String>) {
let mut term = dummy_terminal();
let mut textarea: TextArea = TextArea::new(lines);
textarea.set_search_pattern(pat).unwrap();
term.draw_textarea(&textarea);
for _ in 0..100 {
#[inline]
fn run(pat: &str, mut textarea: TextArea<'_>, forward: bool) {
let mut term = dummy_terminal();
textarea.set_search_pattern(pat).unwrap();
term.draw_textarea(&textarea);
for _ in 0..100 {
if forward {
textarea.search_forward(false);
term.draw_textarea(&textarea);
} else {
textarea.search_back(false);
}
textarea.set_search_pattern(r"").unwrap();
term.draw_textarea(&textarea);
}
textarea.set_search_pattern(r"").unwrap();
term.draw_textarea(&textarea);
}
fn short(c: &mut Criterion) {
let textarea = TextArea::from(LOREM.iter().map(|s| s.to_string()));
c.bench_function("search::forward_short", |b| {
let lines: Vec<_> = LOREM.iter().map(|s| s.to_string()).collect();
b.iter(move || run(r"\w*i\w*", lines.clone()))
b.iter(|| run(r"\w*i\w*", textarea.clone(), true))
});
c.bench_function("search::forward_long", |b| {
let mut lines = vec![];
for _ in 0..10 {
lines.extend(LOREM.iter().map(|s| s.to_string()));
}
b.iter(move || run(r"[A-Z]\w*", lines.clone()))
c.bench_function("search::back_short", |b| {
b.iter(|| run(r"\w*i\w*", textarea.clone(), false))
});
}
fn back(c: &mut Criterion) {
#[inline]
fn run(pat: &str, lines: Vec<String>) {
let mut term = dummy_terminal();
let mut textarea: TextArea = TextArea::new(lines);
textarea.set_search_pattern(pat).unwrap();
term.draw_textarea(&textarea);
for _ in 0..100 {
textarea.search_back(false);
term.draw_textarea(&textarea);
}
textarea.set_search_pattern(r"").unwrap();
term.draw_textarea(&textarea);
fn long(c: &mut Criterion) {
let mut lines = vec![];
for _ in 0..10 {
lines.extend(LOREM.iter().map(|s| s.to_string()));
}
c.bench_function("search::back_short", |b| {
let lines: Vec<_> = LOREM.iter().map(|s| s.to_string()).collect();
b.iter(move || run(r"\w*i\w*", lines.clone()))
let textarea = TextArea::new(lines);
c.bench_function("search::forward_long", |b| {
b.iter(|| run(r"[A-Z]\w*", textarea.clone(), true))
});
c.bench_function("search::back_long", |b| {
let mut lines = vec![];
for _ in 0..10 {
lines.extend(LOREM.iter().map(|s| s.to_string()));
}
b.iter(move || run(r"[A-Z]\w*", lines.clone()))
b.iter(|| run(r"[A-Z]\w*", textarea.clone(), false))
});
}
criterion_group!(search, forward, back);
criterion_group!(search, short, long);
criterion_main!(search);